Example usage for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers id_PBES2

List of usage examples for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers id_PBES2

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers id_PBES2.

Prototype

ASN1ObjectIdentifier id_PBES2

To view the source code for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers id_PBES2.

Click Source Link

Document

PKCS#5: 1.2.840.113549.1.5.13

Usage

From source file:edu.vt.middleware.crypt.io.PrivateKeyCredentialReader.java

License:Open Source License

/**
 * Decrypts a DER-encoded private key in PKCS#8 format.
 *
 * @param  encrypted  Bytes of DER-encoded encrypted private key.
 * @param  password  Password to decrypt private key.
 *
 * @return  ASN.1 encoded bytes of decrypted key.
 *
 * @throws  CryptException  On key decryption errors.
 *///from  w ww  .j av a2s . c om
private byte[] decryptPKCS8Key(final byte[] encrypted, final char[] password) throws CryptException {
    final EncryptionScheme scheme;
    try {
        final EncryptedPrivateKeyInfo ki = EncryptedPrivateKeyInfo
                .getInstance(ASN1Object.fromByteArray(encrypted));
        final AlgorithmIdentifier alg = ki.getEncryptionAlgorithm();
        if (PKCSObjectIdentifiers.id_PBES2.equals(alg.getObjectId())) {
            // PBES2 has following parameters:
            // {
            // {id-PBKDF2, {salt, iterationCount, keyLength (optional)}}
            // {encryptionAlgorithmOid, iv}
            // }
            final DERSequence pbeSeq = (DERSequence) alg.getParameters();
            final PBKDF2Parameters kdfParms = PBKDF2Parameters.decode((DERSequence) pbeSeq.getObjectAt(0));
            final PBES2CipherGenerator cipherGen = new PBES2CipherGenerator(
                    (DERSequence) pbeSeq.getObjectAt(1));
            if (kdfParms.getLength() == 0) {
                kdfParms.setLength(cipherGen.getKeySize() / 8);
            }
            scheme = new PBES2EncryptionScheme(cipherGen.generate(), kdfParms);
        } else {
            // Use PBES1 encryption scheme to decrypt key
            scheme = new PBES1EncryptionScheme(PBES1Algorithm.fromOid(alg.getObjectId().getId()),
                    PBEParameter.decode((DERSequence) alg.getParameters()));
        }
        return scheme.decrypt(password, ki.getEncryptedData());
    } catch (Exception e) {
        throw new CryptException("Failed decrypting PKCS#8 private key", e);
    }
}

From source file:org.cryptacular.asn.PKCS8PrivateKeyDecoder.java

License:Open Source License

@Override
protected byte[] decryptKey(final byte[] encrypted, final char[] password) {
    final EncryptionScheme scheme;
    final EncryptedPrivateKeyInfo ki = EncryptedPrivateKeyInfo.getInstance(tryConvertPem(encrypted));
    final AlgorithmIdentifier alg = ki.getEncryptionAlgorithm();
    if (PKCSObjectIdentifiers.id_PBES2.equals(alg.getAlgorithm())) {
        scheme = new PBES2EncryptionScheme(PBES2Parameters.getInstance(alg.getParameters()), password);
    } else {// w  ww  .j av  a 2  s.  c o  m
        scheme = new PBES1EncryptionScheme(PBES1Algorithm.fromOid(alg.getAlgorithm().getId()),
                PBEParameter.getInstance(alg.getParameters()), password);
    }
    return scheme.decrypt(ki.getEncryptedData());
}

From source file:org.xwiki.crypto.password.internal.pbe.AbstractBcPBES2Cipher.java

License:Open Source License

@Override
public AlgorithmIdentifier getPBEParameters() throws IOException {
    KeyDerivationFunc kdfParams;/*from   w  ww.  j a  v  a 2s . co m*/

    if (getKeyDerivationFunction() instanceof AbstractBcKDF) {
        kdfParams = ((AbstractBcKDF) getKeyDerivationFunction()).getKeyDerivationFunction();
    } else {
        kdfParams = KeyDerivationFunc.getInstance(getKeyDerivationFunction().getEncoded());
    }

    EncryptionScheme scheme = getScheme(getParameters());

    return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBES2, new PBES2Parameters(kdfParams, scheme));
}

From source file:org.xwiki.crypto.password.internal.pbe.factory.AbstractBcPBES2CipherFactory.java

License:Open Source License

@Override
public PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, ASN1Encodable parameters) {
    AlgorithmIdentifier alg = AlgorithmIdentifier.getInstance(parameters);

    if (!alg.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBES2)) {
        throw new IllegalArgumentException(
                "Illegal algorithm identifier for PBES2: " + alg.getAlgorithm().getId());
    }//from  ww w  .jav a  2  s .  co m

    PBES2Parameters params = PBES2Parameters.getInstance(alg.getParameters());
    return getInstance(forEncryption, password, params.getKeyDerivationFunc(), params.getEncryptionScheme());
}

From source file:org.xwiki.crypto.password.internal.pbe.factory.BcPBES2CipherFactory.java

License:Open Source License

private AlgorithmIdentifier getPBES2AlgorithmIdentifier(ASN1Encodable parameters) {
    AlgorithmIdentifier alg = AlgorithmIdentifier.getInstance(parameters);

    if (!alg.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBES2)) {
        throw new IllegalArgumentException(
                "Illegal algorithm identifier for PBES2: " + alg.getAlgorithm().getId());
    }//from w  ww.j  av a  2 s . c  o m
    return alg;
}