Search in sources :

Example 1 with PasswordRecipientInfo

use of com.github.zhenwei.pkix.util.asn1.cms.PasswordRecipientInfo in project LinLong-Java by zhenwei1108.

the class PasswordRecipientInfoGenerator method generate.

public RecipientInfo generate(GenericKey contentEncryptionKey) throws CMSException {
    // / TODO: set IV size properly!
    byte[] iv = new byte[blockSize];
    if (random == null) {
        random = new SecureRandom();
    }
    random.nextBytes(iv);
    if (salt == null) {
        salt = new byte[20];
        random.nextBytes(salt);
    }
    keyDerivationAlgorithm = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, iterationCount, prf.prfAlgID));
    byte[] derivedKey = calculateDerivedKey(schemeID, keyDerivationAlgorithm, keySize);
    AlgorithmIdentifier kekAlgorithmId = new AlgorithmIdentifier(kekAlgorithm, new DEROctetString(iv));
    byte[] encryptedKeyBytes = generateEncryptedBytes(kekAlgorithmId, derivedKey, contentEncryptionKey);
    ASN1OctetString encryptedKey = new DEROctetString(encryptedKeyBytes);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(kekAlgorithm);
    v.add(new DEROctetString(iv));
    AlgorithmIdentifier keyEncryptionAlgorithm = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_PWRI_KEK, new DERSequence(v));
    return new RecipientInfo(new PasswordRecipientInfo(keyDerivationAlgorithm, keyEncryptionAlgorithm, encryptedKey));
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DERSequence(com.github.zhenwei.core.asn1.DERSequence) PasswordRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.PasswordRecipientInfo) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) SecureRandom(java.security.SecureRandom) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) PasswordRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.PasswordRecipientInfo) RecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)1 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DERSequence (com.github.zhenwei.core.asn1.DERSequence)1 PBKDF2Params (com.github.zhenwei.core.asn1.pkcs.PBKDF2Params)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 PasswordRecipientInfo (com.github.zhenwei.pkix.util.asn1.cms.PasswordRecipientInfo)1 RecipientInfo (com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo)1 SecureRandom (java.security.SecureRandom)1