当前位置: 首页>>代码示例>>Java>>正文


Java KeyStore.load方法代码示例

本文整理汇总了Java中java.security.KeyStore.load方法的典型用法代码示例。如果您正苦于以下问题:Java KeyStore.load方法的具体用法?Java KeyStore.load怎么用?Java KeyStore.load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.security.KeyStore的用法示例。


在下文中一共展示了KeyStore.load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createTrustManagerFactory

import java.security.KeyStore; //导入方法依赖的package包/类
private static TrustManagerFactory createTrustManagerFactory(
	final String caCertificateFileName) 
	throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException 
{
	// Creates a trust manager factory
	// Load CA certificate
	final X509Certificate caCertificate = (X509Certificate) createX509CertificateFromFile(caCertificateFileName);
	// CA certificate is used to authenticate server
	final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
	keyStore.load(null, null);
	keyStore.setCertificateEntry("ca-certificate", caCertificate);
	final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
	trustManagerFactory.init(keyStore);

	return trustManagerFactory;
}
 
开发者ID:PacktPublishing,项目名称:MQTT-Essentials-A-Lightweight-IoT-Protocol,代码行数:17,代码来源:SecurityHelper.java

示例2: loadCertificate

import java.security.KeyStore; //导入方法依赖的package包/类
public static CertificateInfo loadCertificate(KeystoreConfiguration configuration)
    throws GeneralSecurityException, IOException {
  try {
    KeyStore keyStore = KeyStore.getInstance(configuration.getType());
    keyStore.load(getResourceAsStream(configuration.getLocation()), configuration.getPassword().toCharArray());

    Key key = keyStore.getKey(configuration.getAlias(), configuration.getKeyPassword().toCharArray());
    if (key instanceof PrivateKey) {
      X509Certificate certificate = (X509Certificate) keyStore.getCertificate(configuration.getAlias());
      PublicKey publicKey = certificate.getPublicKey();
      KeyPair keyPair = new KeyPair(publicKey, (PrivateKey) key);
      return new CertificateInfo(certificate, keyPair);
    } else {
      throw new GeneralSecurityException(configuration.getAlias() + " is not a private key!");
    }
  } catch (IOException | GeneralSecurityException e) {
    log.error("Keystore configuration: [{}] is invalid!", configuration, e);
    throw e;
  }
}
 
开发者ID:osswangxining,项目名称:iot-edge-greengrass,代码行数:21,代码来源:ConfigurationTools.java

示例3: makeSSLSocketFactory

import java.security.KeyStore; //导入方法依赖的package包/类
/**
     * Creates an SSLSocketFactory for HTTPS. Pass a KeyStore resource with your
     * certificate and passphrase
     */
    public static SSLServerSocketFactory makeSSLSocketFactory(String keyAndTrustStoreClasspathPath, char[] passphrase)
    {
        try {
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            File keystrorefile = new File(keyAndTrustStoreClasspathPath);
            System.out.println(keystrorefile.getAbsolutePath());
            InputStream keystoreStream = new FileInputStream(keystrorefile);//NanoHTTPD.class.getResourceAsStream(keyAndTrustStoreClasspathPath);

//            if (keystoreStream == null) 
//            {
//            	System.out.println("Unable to load keystore from classpath: " + keyAndTrustStoreClasspathPath);
//                //throw new IOException("Unable to load keystore from classpath: " + keyAndTrustStoreClasspathPath);
//            	return null;
//            }

            keystore.load(keystoreStream, passphrase);
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keystore, passphrase);
            return makeSSLSocketFactory(keystore, keyManagerFactory);
        } catch (Exception e) {
        	System.out.println(e.toString());
            //throw new IOException(e.getMessage());
        }
        return null;
    }
 
开发者ID:johndavidbustard,项目名称:RoughWorld,代码行数:30,代码来源:WebInterfaceSSL.java

示例4: createSSLContext

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * Lifted from ACCUMULO-3318 - Lifted from TSSLTransportFactory in Thrift-0.9.1.
 * The method to create a client socket with an SSLContextFactory object is not visible to us.
 * Have to use * SslConnectionParams instead of TSSLTransportParameters because no getters exist
 * on TSSLTransportParameters.
 */
private static SSLContext createSSLContext(String truststore,
                                           String truststorePassword,
                                           String truststoreType) throws FlumeException {
  SSLContext ctx;
  try {
    ctx = SSLContext.getInstance("TLS");
    TrustManagerFactory tmf;
    tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore ts = null;
    if (truststore != null && truststoreType != null) {
      ts = KeyStore.getInstance(truststoreType);
      ts.load(new FileInputStream(truststore), truststorePassword.toCharArray());
      tmf.init(ts);
    }

    tmf.init(ts);
    ctx.init(null, tmf.getTrustManagers(), null);

  } catch (Exception e) {
    throw new FlumeException("Error creating the transport", e);
  }
  return ctx;
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:30,代码来源:ThriftRpcClient.java

示例5: SSLContextPinner

import java.security.KeyStore; //导入方法依赖的package包/类
public SSLContextPinner(String pemAssetName) {
    try {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        InputStream certInputStream = getAssets().open(pemAssetName);
        BufferedInputStream bis = new BufferedInputStream(certInputStream);
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        int idx = -1;
        while (bis.available() > 0) {
            Certificate cert = certificateFactory.generateCertificate(bis);
            keyStore.setCertificateEntry("" + ++idx, cert);
            Log.i("App", "pinned " + idx + ": " + ((X509Certificate) cert).getSubjectDN());
        }
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        trustManager = trustManagers[0];
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagers, null);
    } catch(Exception e) {
        sslContext = null;
        trustManager = null;
        Log.e("App", e.toString());
    }
}
 
开发者ID:approov,项目名称:hands-on-api-proxy,代码行数:26,代码来源:App.java

示例6: createServerKeyStore

import java.security.KeyStore; //导入方法依赖的package包/类
private static KeyStore createServerKeyStore(String publicKey,
        String keySpecStr) throws KeyStoreException, IOException,
        NoSuchAlgorithmException, CertificateException,
        InvalidKeySpecException {

    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, null);
    if (publicKey == null || keySpecStr == null) {
        throw new IllegalArgumentException("publicKey or "
                + "keySpecStr cannot be null");
    }
    String strippedPrivateKey = keySpecStr.substring(
            keySpecStr.indexOf("\n"), keySpecStr.lastIndexOf("\n"));

    // generate the private key.
    PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
            Base64.getMimeDecoder().decode(strippedPrivateKey));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKey priKey
            = (RSAPrivateKey) kf.generatePrivate(priKeySpec);

    // generate certificate chain
    try (InputStream is = new ByteArrayInputStream(publicKey.getBytes())) {
        // generate certificate from cert string
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate keyCert = cf.generateCertificate(is);
        Certificate[] chain = {keyCert};
        ks.setKeyEntry("TestEntry", priKey, PASSWORD, chain);
    }

    return ks;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:CipherTestUtils.java

示例7: getSslHandler

import java.security.KeyStore; //导入方法依赖的package包/类
public SslHandler getSslHandler() {
    try {
        URL ksUrl = Resources.getResource(keyStoreFile);
        File ksFile = new File(ksUrl.toURI());
        URL tsUrl = Resources.getResource(keyStoreFile);
        File tsFile = new File(tsUrl.toURI());

        TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        KeyStore trustStore = KeyStore.getInstance(keyStoreType);
        trustStore.load(new FileInputStream(tsFile), keyStorePassword.toCharArray());
        tmFactory.init(trustStore);

        KeyStore ks = KeyStore.getInstance(keyStoreType);

        ks.load(new FileInputStream(ksFile), keyStorePassword.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, keyPassword.toCharArray());

        KeyManager[] km = kmf.getKeyManagers();
        TrustManager x509wrapped = getX509TrustManager(tmFactory);
        TrustManager[] tm = {x509wrapped};
        SSLContext sslContext = SSLContext.getInstance(TLS);
        sslContext.init(km, tm, null);
        SSLEngine sslEngine = sslContext.createSSLEngine();
        sslEngine.setUseClientMode(false);
        sslEngine.setNeedClientAuth(false);
        sslEngine.setWantClientAuth(true);
        sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols());
        sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites());
        sslEngine.setEnableSessionCreation(true);
        return new SslHandler(sslEngine);
    } catch (Exception e) {
        log.error("Unable to set up SSL context. Reason: " + e.getMessage(), e);
        throw new RuntimeException("Failed to get SSL handler", e);
    }
}
 
开发者ID:osswangxining,项目名称:iotplatform,代码行数:37,代码来源:MqttSslHandlerProvider.java

示例8: createSSLSocketFactory

import java.security.KeyStore; //导入方法依赖的package包/类
/**
     * create ssl socket factory. by target crt file.
     * @param context the context.
     * @param assetsFilePath the crt file path in assets.
     * @return an instance of SSLSocketFactory.
     */
    public static SSLSocketFactory createSSLSocketFactory(Context context, String assetsFilePath) {
        SSLContext sslContext = null;
        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            InputStream certificates = new BufferedInputStream(context.getAssets().open(assetsFilePath));
            Certificate ca;
            try {
                ca = certificateFactory.generateCertificate(certificates);
                System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
            } finally {
                certificates.close();
            }

            // Create a KeyStore containing our trusted CAs
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);

            // Create a TrustManager that trusts the CAs in our KeyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);

            // Create an SSLContext that uses our TrustManager
            sslContext = SSLContext.getInstance("TLSv1", "AndroidOpenSSL");
//            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tmf.getTrustManagers(), null);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return sslContext != null ? sslContext.getSocketFactory() : null;
    }
 
开发者ID:LightSun,项目名称:android-util2,代码行数:42,代码来源:HttpsHelper.java

示例9: getKeystore

import java.security.KeyStore; //导入方法依赖的package包/类
public static KeyStore getKeystore() {
    KeyStore trustStore = null;
    try {
        trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return trustStore;
}
 
开发者ID:LanguidSheep,项目名称:sealtalk-android-master,代码行数:11,代码来源:MySSLSocketFactory.java

示例10: getKeyStore

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * 获得KeyStore
 * 
 * @param keyStorePath 密钥库路径
 * @param password 密码
 * @return KeyStore 密钥库
 * @throws Exception
 */
private static KeyStore getKeyStore(String keyStorePath, String password) throws Exception {
	// 实例化密钥库
	KeyStore ks = KeyStore.getInstance("PKCS12");
	// KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
	// 获得密钥库文件流
	FileInputStream is = new FileInputStream(keyStorePath);
	// 加载密钥库
	ks.load(is, password.toCharArray());
	// 关闭密钥库文件流
	is.close();
	return ks;
}
 
开发者ID:youngMen1,项目名称:JAVA-,代码行数:21,代码来源:HTTPSPKCSCoder.java

示例11: decrypt

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * Decrypt the encrypted secret.
 *
 * @param encrypted the encrypted secret.
 * @return the decrypted secret.
 * @throws Exception
 */
public String decrypt(String encrypted) throws Exception {
    byte[] encryptedBytes = Base64.decode(encrypted, Base64.DEFAULT);
    KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
    keyStore.load(null);
    KeyStore.PrivateKeyEntry privateKeyEntry =
        (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
    Cipher output = Cipher.getInstance(RSA_ALGORITHM);
    output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());
    CipherInputStream cipherInputStream = new CipherInputStream(
        new ByteArrayInputStream(encryptedBytes), output);
    ArrayList<Byte> values = new ArrayList<>();
    int nextByte;
    while ((nextByte = cipherInputStream.read()) != -1) {
        values.add((byte) nextByte);
    }

    byte[] bytes = new byte[values.size()];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = values.get(i);
    }
    return new String(bytes);
}
 
开发者ID:drakeet,项目名称:rebase-android,代码行数:30,代码来源:BlackBox.java

示例12: main

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * @param args {cacerts keystore, cert chain}
 */
public static void main(String[] args) throws Exception {

    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(new FileInputStream(args[0]), "changeit".toCharArray());
    Validator v = Validator.getInstance
        (Validator.TYPE_PKIX, Validator.VAR_GENERIC, ks);
    X509Certificate[] chain = createPath(args[1]);
    System.out.println("Chain: ");
    for (X509Certificate c: v.validate(chain)) {
        System.out.println("   " + c.getSubjectX500Principal() +
                " issued by " + c.getIssuerX500Principal());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:CertReplace.java

示例13: getKeystore

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * Gets a Default KeyStore
 *
 * @return KeyStore
 */
public static KeyStore getKeystore() {
    KeyStore trustStore = null;
    try {
        trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return trustStore;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:MySSLSocketFactory.java

示例14: loadStore

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * Helper method, load a keystore
 * @param file file for keystore, null or "NONE" for PKCS11
 * @pass password for the keystore
 * @type keystore type
 * @returns the KeyStore object
 * @exception Exception if anything goes wrong
 */
KeyStore loadStore(String file, String pass, String type) throws Exception {
    KeyStore ks = KeyStore.getInstance(type);
    FileInputStream is = null;
    if (file != null && !file.equals("NONE")) {
        is = new FileInputStream(file);
    }
    ks.load(is, pass.toCharArray());
    is.close();
    return ks;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:KeyToolTest.java

示例15: loadPrivateKeyFromKeyStore

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * 从KeyStore获取私钥
 * @param location
 * @param alias
 * @param storeType
 * @param storePass
 * @param keyPass
 * @return
 */
public static PrivateKey loadPrivateKeyFromKeyStore(String location, String alias, String storeType, String storePass, String keyPass) {
    try {
        storeType = null == storeType ? KeyStore.getDefaultType() : storeType;
        keyPass = keyPass == null ? storePass : keyPass;
        KeyStore keyStore = KeyStore.getInstance(storeType);
        InputStream is = new FileInputStream(location);
        keyStore.load(is, storePass.toCharArray());
        // 由密钥库获取密钥的两种方式
        return (PrivateKey) keyStore.getKey(alias, keyPass.toCharArray());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:23,代码来源:RSA.java


注:本文中的java.security.KeyStore.load方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。