本文整理汇总了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;
}
}
示例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;
}
示例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;
}
示例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());
}
}
示例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;
}
示例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);
}
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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());
}
}
示例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;
}
示例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;
}
示例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);
}
}