use of com.github.zhenwei.pkix.cert.crmf.CRMFException in project LinLong-Java by zhenwei1108.
the class CRMFHelper method createContentCipher.
Cipher createContentCipher(final Key sKey, final AlgorithmIdentifier encryptionAlgID) throws CRMFException {
return (Cipher) execute(new JCECallback() {
public Object doInJCE() throws CRMFException, InvalidAlgorithmParameterException, InvalidKeyException, InvalidParameterSpecException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException {
Cipher cipher = createCipher(encryptionAlgID.getAlgorithm());
ASN1Primitive sParams = (ASN1Primitive) encryptionAlgID.getParameters();
ASN1ObjectIdentifier encAlg = encryptionAlgID.getAlgorithm();
if (sParams != null && !(sParams instanceof ASN1Null)) {
try {
AlgorithmParameters params = createAlgorithmParameters(encryptionAlgID.getAlgorithm());
try {
AlgorithmParametersUtils.loadParameters(params, sParams);
} catch (IOException e) {
throw new CRMFException("error decoding algorithm parameters.", e);
}
cipher.init(Cipher.DECRYPT_MODE, sKey, params);
} catch (NoSuchAlgorithmException e) {
if (encAlg.equals(CMSAlgorithm.DES_EDE3_CBC) || encAlg.equals(CMSAlgorithm.IDEA_CBC) || encAlg.equals(CMSAlgorithm.AES128_CBC) || encAlg.equals(CMSAlgorithm.AES192_CBC) || encAlg.equals(CMSAlgorithm.AES256_CBC)) {
cipher.init(Cipher.DECRYPT_MODE, sKey, new IvParameterSpec(ASN1OctetString.getInstance(sParams).getOctets()));
} else {
throw e;
}
}
} else {
if (encAlg.equals(CMSAlgorithm.DES_EDE3_CBC) || encAlg.equals(CMSAlgorithm.IDEA_CBC) || encAlg.equals(CMSAlgorithm.CAST5_CBC)) {
cipher.init(Cipher.DECRYPT_MODE, sKey, new IvParameterSpec(new byte[8]));
} else {
cipher.init(Cipher.DECRYPT_MODE, sKey);
}
}
return cipher;
}
});
}
use of com.github.zhenwei.pkix.cert.crmf.CRMFException in project LinLong-Java by zhenwei1108.
the class CRMFHelper method toPublicKey.
PublicKey toPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) throws CRMFException {
try {
X509EncodedKeySpec xspec = new X509EncodedKeySpec(subjectPublicKeyInfo.getEncoded());
AlgorithmIdentifier keyAlg = subjectPublicKeyInfo.getAlgorithm();
return createKeyFactory(keyAlg.getAlgorithm()).generatePublic(xspec);
} catch (Exception e) {
throw new CRMFException("invalid key: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.pkix.cert.crmf.CRMFException in project LinLong-Java by zhenwei1108.
the class CRMFHelper method generateParameters.
AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, SecretKey encKey, SecureRandom rand) throws CRMFException {
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 CRMFException("parameters generation error: " + e, e);
}
}
return pGen.generateParameters();
} catch (NoSuchAlgorithmException e) {
return null;
} catch (GeneralSecurityException e) {
throw new CRMFException("exception creating algorithm parameter generator: " + e, e);
}
}
Aggregations