本文整理汇总了Java中net.oauth.signature.pem.PKCS1EncodedKeySpec类的典型用法代码示例。如果您正苦于以下问题:Java PKCS1EncodedKeySpec类的具体用法?Java PKCS1EncodedKeySpec怎么用?Java PKCS1EncodedKeySpec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PKCS1EncodedKeySpec类属于net.oauth.signature.pem包,在下文中一共展示了PKCS1EncodedKeySpec类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPrivateKeyFromPem
import net.oauth.signature.pem.PKCS1EncodedKeySpec; //导入依赖的package包/类
private PrivateKey getPrivateKeyFromPem(String pem)
throws GeneralSecurityException, IOException {
InputStream stream = new ByteArrayInputStream(
pem.getBytes("UTF-8"));
PEMReader reader = new PEMReader(stream);
byte[] bytes = reader.getDerBytes();
KeySpec keySpec;
if (PEMReader.PRIVATE_PKCS1_MARKER.equals(reader.getBeginMarker())) {
keySpec = (new PKCS1EncodedKeySpec(bytes)).getKeySpec();
}
else if (PEMReader.PRIVATE_PKCS8_MARKER.equals(reader.getBeginMarker())) {
keySpec = new PKCS8EncodedKeySpec(bytes);
}
else {
throw new IOException("Invalid PEM file: Unknown marker " +
"for private key " + reader.getBeginMarker());
}
KeyFactory fac = KeyFactory.getInstance("RSA");
return fac.generatePrivate(keySpec);
}
示例2: getPrivateKeyFromPem
import net.oauth.signature.pem.PKCS1EncodedKeySpec; //导入依赖的package包/类
private PrivateKey getPrivateKeyFromPem(String pem)
throws GeneralSecurityException, IOException {
InputStream stream = new ByteArrayInputStream(
pem.getBytes("UTF-8"));
PEMReader reader = new PEMReader(stream);
byte[] bytes = reader.getDerBytes();
KeySpec keySpec;
if (PEMReader.PRIVATE_PKCS1_MARKER.equals(reader.getBeginMarker())) {
keySpec = (new PKCS1EncodedKeySpec(bytes)).getKeySpec();
} else if (PEMReader.PRIVATE_PKCS8_MARKER.equals(reader.getBeginMarker())) {
keySpec = new PKCS8EncodedKeySpec(bytes);
} else {
throw new IOException("Invalid PEM file: Unknown marker " +
"for private key " + reader.getBeginMarker());
}
KeyFactory fac = KeyFactory.getInstance("RSA");
return fac.generatePrivate(keySpec);
}
示例3: configureClientCert
import net.oauth.signature.pem.PKCS1EncodedKeySpec; //导入依赖的package包/类
private static KeyManager[] configureClientCert(String clientCertFile, String clientKeyFile, char[] clientKeyPassword, String clientKeyAlgo) throws Exception {
try {
InputStream certInputStream = openFile(clientCertFile);
CertificateFactory certFactory = CertificateFactory.getInstance("X509");
X509Certificate cert = (X509Certificate)certFactory.generateCertificate(certInputStream);
InputStream keyInputStream = openFile(clientKeyFile);
PEMReader reader = new PEMReader(keyInputStream);
RSAPrivateCrtKeySpec keySpec = new PKCS1EncodedKeySpec(reader.getDerBytes()).getKeySpec();
KeyFactory kf = KeyFactory.getInstance(clientKeyAlgo);
RSAPrivateKey privKey = (RSAPrivateKey)kf.generatePrivate(keySpec);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(null);
String alias = cert.getSubjectX500Principal().getName();
keyStore.setKeyEntry(alias, privKey, clientKeyPassword, new Certificate[]{cert});
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, clientKeyPassword);
return keyManagerFactory.getKeyManagers();
} catch (Exception e) {
log.log(Level.SEVERE, "Could not create key manager for " + clientCertFile + " (" + clientKeyFile + ")", e);
throw e;
}
}
示例4: configureClientCert
import net.oauth.signature.pem.PKCS1EncodedKeySpec; //导入依赖的package包/类
private KeyManager[] configureClientCert(String clientCertFile, String clientKeyFile, char[] clientKeyPassword, String clientKeyAlgo) throws Exception {
try {
InputStream certInputStream = openFile(clientCertFile);
CertificateFactory certFactory = CertificateFactory.getInstance("X509");
X509Certificate cert = (X509Certificate)certFactory.generateCertificate(certInputStream);
InputStream keyInputStream = openFile(clientKeyFile);
PEMReader reader = new PEMReader(keyInputStream);
RSAPrivateCrtKeySpec keySpec = new PKCS1EncodedKeySpec(reader.getDerBytes()).getKeySpec();
KeyFactory kf = KeyFactory.getInstance(clientKeyAlgo);
RSAPrivateKey privKey = (RSAPrivateKey)kf.generatePrivate(keySpec);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(null);
String alias = cert.getSubjectX500Principal().getName();
keyStore.setKeyEntry(alias, privKey, clientKeyPassword, new Certificate[]{cert});
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, clientKeyPassword);
return keyManagerFactory.getKeyManagers();
} catch (Exception e) {
log.log(Level.SEVERE, "Could not create key manager for " + clientCertFile + " (" + clientKeyFile + ")", e);
throw e;
}
}