com.fitbur.bouncycastle.cms.bc.BcPasswordRecipient Maven / Gradle / Ivy
package com.fitbur.bouncycastle.cms.bc;
import com.fitbur.bouncycastle.asn1.ASN1OctetString;
import com.fitbur.bouncycastle.asn1.pkcs.PBKDF2Params;
import com.fitbur.bouncycastle.asn1.x509.AlgorithmIdentifier;
import com.fitbur.bouncycastle.cms.CMSException;
import com.fitbur.bouncycastle.cms.PasswordRecipient;
import com.fitbur.bouncycastle.crypto.InvalidCipherTextException;
import com.fitbur.bouncycastle.crypto.Wrapper;
import com.fitbur.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
import com.fitbur.bouncycastle.crypto.params.KeyParameter;
import com.fitbur.bouncycastle.crypto.params.ParametersWithIV;
/**
* the RecipientInfo class for a recipient who has been sent a message
* encrypted using a password.
*/
public abstract class BcPasswordRecipient
implements PasswordRecipient
{
private int schemeID = PasswordRecipient.PKCS5_SCHEME2_UTF8;
private char[] password;
BcPasswordRecipient(
char[] password)
{
this.password = password;
}
public BcPasswordRecipient setPasswordConversionScheme(int schemeID)
{
this.schemeID = schemeID;
return this;
}
protected KeyParameter extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier contentEncryptionAlgorithm, byte[] com.fitburrivedKey, byte[] encryptedContentEncryptionKey)
throws CMSException
{
Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
keyEncryptionCipher.init(false, new ParametersWithIV(new KeyParameter(com.fitburrivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));
try
{
return new KeyParameter(keyEncryptionCipher.unwrap(encryptedContentEncryptionKey, 0, encryptedContentEncryptionKey.length));
}
catch (InvalidCipherTextException e)
{
throw new CMSException("unable to unwrap key: " + e.getMessage(), e);
}
}
public byte[] calculateDerivedKey(byte[] encodedPassword, AlgorithmIdentifier com.fitburrivationAlgorithm, int keySize)
throws CMSException
{
PBKDF2Params params = PBKDF2Params.getInstance(com.fitburrivationAlgorithm.getParameters());
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator();
gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue());
return ((KeyParameter)gen.generateDerivedParameters(keySize)).getKey();
}
public int getPasswordConversionScheme()
{
return schemeID;
}
public char[] getPassword()
{
return password;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy