本文整理汇总了Java中org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo类的典型用法代码示例。如果您正苦于以下问题:Java PKCS8EncryptedPrivateKeyInfo类的具体用法?Java PKCS8EncryptedPrivateKeyInfo怎么用?Java PKCS8EncryptedPrivateKeyInfo使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PKCS8EncryptedPrivateKeyInfo类属于org.bouncycastle.pkcs包,在下文中一共展示了PKCS8EncryptedPrivateKeyInfo类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPrivateKey
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
public static PrivateKey getPrivateKey(String keyString) {
PrivateKey privateKey = null;
try {
BufferedReader br = new BufferedReader(new StringReader(keyString));
PEMParser pp = new PEMParser(br);
PKCS8EncryptedPrivateKeyInfo pemPrivateKeyInfo = (PKCS8EncryptedPrivateKeyInfo) pp.readObject();
pp.close();
InputDecryptorProvider pkcs8Prov = new JceOpenSSLPKCS8DecryptorProviderBuilder().build("1234".toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
privateKey = converter.getPrivateKey(pemPrivateKeyInfo.decryptPrivateKeyInfo(pkcs8Prov));
} catch (Exception e) {
ProtoLogger.error("!!! Error importing private key !!!");
}
return privateKey;
}
示例2: parseObject
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
/**
* Reads in an EncryptedPrivateKeyInfo
*
* @return the X509Certificate
* @throws java.io.IOException if an I/O error occured
*/
public Object parseObject(PemObject obj)
throws IOException
{
try
{
return new PKCS8EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfo.getInstance(obj.getContent()));
}
catch (Exception e)
{
throw new PEMException("problem parsing ENCRYPTED PRIVATE KEY: " + e.toString(), e);
}
}
示例3: savePkcs11PrivateKey
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
private void savePkcs11PrivateKey(byte[] id, String label, PrivateKey privateKey)
throws P11TokenException {
PKCS8EncryptedPrivateKeyInfo encryptedPrivKeyInfo = privateKeyCryptor.encrypt(privateKey);
byte[] encoded;
try {
encoded = encryptedPrivKeyInfo.getEncoded();
} catch (IOException ex) {
LogUtil.error(LOG, ex);
throw new P11TokenException("could not encode PrivateKey");
}
savePkcs11Entry(privKeyDir, id, label, encoded);
}
示例4: encrypt
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
PKCS8EncryptedPrivateKeyInfo encrypt(PrivateKey privateKey) {
ParamUtil.requireNonNull("privateKey", privateKey);
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(privateKey.getEncoded());
PKCS8EncryptedPrivateKeyInfoBuilder builder = new PKCS8EncryptedPrivateKeyInfoBuilder(
privateKeyInfo);
synchronized (encryptor) {
return builder.build(encryptor);
}
}
示例5: convertPrivateKey
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
private static PrivateKey convertPrivateKey(PKCS8EncryptedPrivateKeyInfo safeBagValue, String resource,
PasswordCallback password) throws IOException {
PrivateKeyInfo decryptedSafeBagValue = null;
PKCSException decryptException = null;
while (decryptedSafeBagValue == null) {
try {
decryptedSafeBagValue = safeBagValue
.decryptPrivateKeyInfo(buildInputDecryptorProvider(resource, password, decryptException));
} catch (PKCSException e) {
decryptException = e;
}
}
return convertPrivateKey(decryptedSafeBagValue);
}
示例6: testBcEncryptedPrivateKeyInfo
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
public void testBcEncryptedPrivateKeyInfo()
throws Exception
{
KeyFactory fact = KeyFactory.getInstance("RSA", BC);
PrivateKey privKey = fact.generatePrivate(privKeySpec);
PKCS8EncryptedPrivateKeyInfoBuilder builder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privKey);
PKCS8EncryptedPrivateKeyInfo priv = builder.build(new BcPKCS12PBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC, new CBCBlockCipher(new DESedeEngine())).build(passwd));
PrivateKeyInfo info = priv.decryptPrivateKeyInfo(new BcPKCS12PBEInputDecryptorProviderBuilder().build(passwd));
assertTrue(Arrays.areEqual(info.getEncoded(), privKey.getEncoded()));
}
示例7: testEncryptedPrivateKeyInfo
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
public void testEncryptedPrivateKeyInfo()
throws Exception
{
KeyFactory fact = KeyFactory.getInstance("RSA", BC);
PrivateKey privKey = fact.generatePrivate(privKeySpec);
PKCS8EncryptedPrivateKeyInfoBuilder builder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privKey);
PKCS8EncryptedPrivateKeyInfo priv = builder.build(new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC).build(passwd));
PrivateKeyInfo info = priv.decryptPrivateKeyInfo(new JcePKCSPBEInputDecryptorProviderBuilder().build(passwd));
assertTrue(Arrays.areEqual(info.getEncoded(), privKey.getEncoded()));
}
示例8: testEncryptedPrivateKeyInfoPKCS5
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
public void testEncryptedPrivateKeyInfoPKCS5()
throws Exception
{
KeyFactory fact = KeyFactory.getInstance("RSA", BC);
PrivateKey privKey = fact.generatePrivate(privKeySpec);
PKCS8EncryptedPrivateKeyInfoBuilder builder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privKey);
PKCS8EncryptedPrivateKeyInfo priv = builder.build(new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes256_CBC).build(passwd));
PrivateKeyInfo info = priv.decryptPrivateKeyInfo(new JcePKCSPBEInputDecryptorProviderBuilder().build(passwd));
assertTrue(Arrays.areEqual(info.getEncoded(), privKey.getEncoded()));
}
示例9: doApply
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
private Key doApply(Resource resource) throws IOException, CertificateException {
try (Reader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), "UTF-8"))) {
PEMParser pemParser = new PEMParser(reader);
Object o;
boolean encryptedPrivateFound = false;
while ((o = pemParser.readObject()) != null) {
if (o instanceof PKCS8EncryptedPrivateKeyInfo) {
encryptedPrivateFound = true;
}
if (o instanceof PEMKeyPair) {
PEMKeyPair pemKeyPair = (PEMKeyPair) o;
return findPrivate ?
pemKeyConverter.getPrivateKey(pemKeyPair.getPrivateKeyInfo()) :
pemKeyConverter.getPublicKey(pemKeyPair.getPublicKeyInfo());
}
if (o instanceof PrivateKeyInfo && findPrivate) {
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) o;
return pemKeyConverter.getPrivateKey(privateKeyInfo);
}
if (o instanceof SubjectPublicKeyInfo && !findPrivate) {
SubjectPublicKeyInfo info = (SubjectPublicKeyInfo) o;
return pemKeyConverter.getPublicKey(info);
}
if (o instanceof X509CertificateHolder && !findPrivate) {
X509CertificateHolder holder = (X509CertificateHolder) o;
X509Certificate cert = x509Converter.getCertificate(holder);
return cert.getPublicKey();
}
}
//if we haven't returned yet, we couldn't find a key based on our preferences.
String msg;
if (encryptedPrivateFound && findPrivate) {
msg = "Key resource [" + resource + "] contains a PKCS8 Encrypted PrivateKey. Only unencrypted " +
"private keys are supported.";
} else {
msg = "Key resource [" + resource + "] did not contain a " + (findPrivate ? "private " : "public ") +
"key.";
}
throw new IllegalArgumentException(msg);
}
}
示例10: parsePrivateKey
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; //导入依赖的package包/类
/**
* Parses a PrivateKey instance from a PEM representation.
*
* When the provided key is encrypted, the provided pass phrase is applied.
*
* @param pemRepresentation a PEM representation of a private key (cannot be null or empty)
* @param passPhrase optional pass phrase (must be present if the private key is encrypted).
* @return a PrivateKey instance (never null)
*/
public static PrivateKey parsePrivateKey(InputStream pemRepresentation, String passPhrase) throws IOException {
if ( passPhrase == null ) {
passPhrase = "";
}
try (Reader reader = new InputStreamReader(pemRepresentation); //
PEMParser pemParser = new PEMParser(reader)) {
final Object object = pemParser.readObject();
final JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider( "BC" );
final KeyPair kp;
if ( object instanceof PEMEncryptedKeyPair )
{
// Encrypted key - we will use provided password
final PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build( passPhrase.toCharArray() );
kp = converter.getKeyPair( ( (PEMEncryptedKeyPair) object ).decryptKeyPair( decProv ) );
}
else if ( object instanceof PKCS8EncryptedPrivateKeyInfo )
{
// Encrypted key - we will use provided password
try
{
final PKCS8EncryptedPrivateKeyInfo encryptedInfo = (PKCS8EncryptedPrivateKeyInfo) object;
final InputDecryptorProvider provider = new JceOpenSSLPKCS8DecryptorProviderBuilder().build( passPhrase.toCharArray() );
final PrivateKeyInfo privateKeyInfo = encryptedInfo.decryptPrivateKeyInfo( provider );
return converter.getPrivateKey( privateKeyInfo );
}
catch ( PKCSException | OperatorCreationException e )
{
throw new IOException( "Unable to decrypt private key.", e );
}
}
else if ( object instanceof PrivateKeyInfo )
{
return converter.getPrivateKey( (PrivateKeyInfo) object );
}
else
{
// Unencrypted key - no password needed
kp = converter.getKeyPair( (PEMKeyPair) object );
}
return kp.getPrivate();
}
}