use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.
the class BcPasswordRecipient method extractSecretKey.
protected KeyParameter extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException {
Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
keyEncryptionCipher.init(false, new ParametersWithIV(new KeyParameter(derivedKey), 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);
}
}
use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.
the class BcPasswordRecipient method calculateDerivedKey.
public byte[] calculateDerivedKey(int schemeID, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException {
PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
byte[] encodedPassword = (schemeID == PasswordRecipient.PKCS5_SCHEME2) ? PBEParametersGenerator.PKCS5PasswordToBytes(password) : PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
try {
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(EnvelopedDataHelper.getPRF(params.getPrf()));
gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue());
return ((KeyParameter) gen.generateDerivedParameters(keySize)).getKey();
} catch (Exception e) {
throw new CMSException("exception creating derived key: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.
the class BcPasswordRecipientInfoGenerator method calculateDerivedKey.
protected byte[] calculateDerivedKey(int schemeID, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException {
PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
byte[] encodedPassword = (schemeID == PasswordRecipient.PKCS5_SCHEME2) ? PBEParametersGenerator.PKCS5PasswordToBytes(password) : PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
try {
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(EnvelopedDataHelper.getPRF(params.getPrf()));
gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue());
return ((KeyParameter) gen.generateDerivedParameters(keySize)).getKey();
} catch (Exception e) {
throw new CMSException("exception creating derived key: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.
the class EnvelopedDataHelper method calculateDerivedKey.
byte[] calculateDerivedKey(int schemeID, char[] password, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException {
PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
try {
SecretKeyFactory keyFact;
if (schemeID == PasswordRecipient.PKCS5_SCHEME2) {
keyFact = helper.createSecretKeyFactory("PBKDF2with8BIT");
} else {
keyFact = helper.createSecretKeyFactory((String) PBKDF2_ALG_NAMES.get(params.getPrf()));
}
SecretKey key = keyFact.generateSecret(new PBEKeySpec(password, params.getSalt(), params.getIterationCount().intValue(), keySize));
return key.getEncoded();
} catch (GeneralSecurityException e) {
throw new CMSException("Unable to calculate derived key from password: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.
the class EnvelopedDataHelper method generateParameters.
AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, SecretKey encKey, SecureRandom rand) throws CMSException {
try {
AlgorithmParameterGenerator pGen = createAlgorithmParameterGenerator(encryptionOID);
if (encryptionOID.equals(CMSAlgorithm.RC2_CBC)) {
byte[] iv = new byte[8];
rand.nextBytes(iv);
try {
pGen.init(new RC2ParameterSpec(encKey.getEncoded().length * 8, iv), rand);
} catch (InvalidAlgorithmParameterException e) {
throw new CMSException("parameters generation error: " + e, e);
}
}
return pGen.generateParameters();
} catch (NoSuchAlgorithmException e) {
return null;
} catch (GeneralSecurityException e) {
throw new CMSException("exception creating algorithm parameter generator: " + e, e);
}
}
Aggregations