load Keystore - Java Security

Java examples for Security:Key

Description

load Keystore

Demo Code

/*//from   www.  j a v a2  s.  c om
 * #%L
 * asio ssl-fix
 * %%
 * Copyright (C) 2013 - 2015 Research Group Scientific Computing, University of Vienna
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class Main{
    public static KeyStore loadKeystore(final String path,
            final char[] password) throws InitializationFailure {
        final InputStream source = SslUtils.class.getResourceAsStream(path);
        if (source == null) {
            throw new InitializationFailure(new FileNotFoundException(path
                    + " (keystore source not found on classpath)"));
        }
        try {
            final KeyStore keystore = KeyStore.getInstance("JKS");
            keystore.load(source, password);
            return keystore;
        } catch (GeneralSecurityException | IOException e) {
            throw new InitializationFailure(e);
        }
    }
}

Related Tutorials