Search in sources :

Example 1 with PKCS8EncryptedPrivateKeyInfoBuilder

use of org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfoBuilder in project xipki by xipki.

the class PrivateKeyCryptor method encrypt.

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);
    }
}
Also used : PKCS8EncryptedPrivateKeyInfoBuilder(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfoBuilder) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 2 with PKCS8EncryptedPrivateKeyInfoBuilder

use of org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfoBuilder in project certmgr by hdecarne.

the class DERCertReaderWriter method encryptKey.

private static byte[] encryptKey(KeyPair key, String resource, PasswordCallback newPassword) throws IOException {
    char[] passwordChars = newPassword.queryPassword(resource);
    if (passwordChars == null) {
        throw new PasswordRequiredException(resource);
    }
    byte[] encoded;
    try {
        PKCS8EncryptedPrivateKeyInfoBuilder encryptedPrivateKeyInfoBuilder = new PKCS8EncryptedPrivateKeyInfoBuilder(KeyHelper.encodePrivateKey(key.getPrivate()));
        OutputEncryptor encryptor = OUTPUT_ENCRYPTOR_BUILDER.build(passwordChars);
        encoded = encryptedPrivateKeyInfoBuilder.build(encryptor).getEncoded();
    } catch (OperatorCreationException e) {
        throw new CertProviderException(e);
    }
    return encoded;
}
Also used : PKCS8EncryptedPrivateKeyInfoBuilder(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfoBuilder) PasswordRequiredException(de.carne.certmgr.certs.PasswordRequiredException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) CertProviderException(de.carne.certmgr.certs.CertProviderException) OutputEncryptor(org.bouncycastle.operator.OutputEncryptor)

Aggregations

PKCS8EncryptedPrivateKeyInfoBuilder (org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfoBuilder)2 CertProviderException (de.carne.certmgr.certs.CertProviderException)1 PasswordRequiredException (de.carne.certmgr.certs.PasswordRequiredException)1 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)1 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)1 OutputEncryptor (org.bouncycastle.operator.OutputEncryptor)1 PKCS8EncryptedPrivateKeyInfo (org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)1