Search in sources :

Example 6 with PKCS12PBEParams

use of org.bouncycastle.asn1.pkcs.PKCS12PBEParams in project XobotOS by xamarin.

the class JDKPKCS12KeyStore method cryptData.

protected byte[] cryptData(boolean forEncryption, AlgorithmIdentifier algId, char[] password, boolean wrongPKCS12Zero, byte[] data) throws IOException {
    String algorithm = algId.getObjectId().getId();
    PKCS12PBEParams pbeParams = new PKCS12PBEParams((ASN1Sequence) algId.getParameters());
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    try {
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
        PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
        JCEPBEKey key = (JCEPBEKey) keyFact.generateSecret(pbeSpec);
        key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
        Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
        int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
        cipher.init(mode, key, defParams);
        return cipher.doFinal(data);
    } catch (Exception e) {
        throw new IOException("exception decrypting data - " + e.toString());
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS12PBEParams(org.bouncycastle.asn1.pkcs.PKCS12PBEParams) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERBMPString(org.bouncycastle.asn1.DERBMPString) BERConstructedOctetString(org.bouncycastle.asn1.BERConstructedOctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

Aggregations

IOException (java.io.IOException)6 KeyStoreException (java.security.KeyStoreException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 UnrecoverableKeyException (java.security.UnrecoverableKeyException)6 CertificateEncodingException (java.security.cert.CertificateEncodingException)6 CertificateException (java.security.cert.CertificateException)6 PKCS12PBEParams (org.bouncycastle.asn1.pkcs.PKCS12PBEParams)6 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)5 DERBMPString (org.bouncycastle.asn1.DERBMPString)5 DEROctetString (org.bouncycastle.asn1.DEROctetString)5 PrivateKey (java.security.PrivateKey)4 Cipher (javax.crypto.Cipher)4 SecretKeyFactory (javax.crypto.SecretKeyFactory)4 PBEKeySpec (javax.crypto.spec.PBEKeySpec)4 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)4 BERConstructedOctetString (org.bouncycastle.asn1.BERConstructedOctetString)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Certificate (java.security.cert.Certificate)2 X509Certificate (java.security.cert.X509Certificate)2 Enumeration (java.util.Enumeration)2