org.bouncycastle.openssl.PEMEncryptedKeyPair Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpkix-jdk15to18 Show documentation
Show all versions of bcpkix-jdk15to18 Show documentation
The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.5 to JDK 1.8. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.
package org.bouncycastle.openssl;
import java.io.IOException;
import org.bouncycastle.operator.OperatorCreationException;
public class PEMEncryptedKeyPair
{
private final String dekAlgName;
private final byte[] iv;
private final byte[] keyBytes;
private final PEMKeyPairParser parser;
PEMEncryptedKeyPair(String dekAlgName, byte[] iv, byte[] keyBytes, PEMKeyPairParser parser)
{
this.dekAlgName = dekAlgName;
this.iv = iv;
this.keyBytes = keyBytes;
this.parser = parser;
}
/**
* Return the key encryption algorithm name that was in the original DEK-Info field.
*
* @return the key encryption algorithm name.
*/
public String getDekAlgName()
{
return dekAlgName;
}
public PEMKeyPair decryptKeyPair(PEMDecryptorProvider keyDecryptorProvider)
throws IOException
{
try
{
PEMDecryptor keyDecryptor = keyDecryptorProvider.get(dekAlgName);
return parser.parse(keyDecryptor.decrypt(keyBytes, iv));
}
catch (IOException e)
{
throw e;
}
catch (OperatorCreationException e)
{
throw new PEMException("cannot create extraction operator: " + e.getMessage(), e);
}
catch (Exception e)
{
throw new PEMException("exception processing key pair: " + e.getMessage(), e);
}
}
}