use of com.adaptris.security.exc.DecryptException in project interlok by adaptris.
the class StdSecurityService method decrypt.
private StdOutput decrypt(byte[] payload, PrivateKey pk) throws AdaptrisSecurityException {
StdOutput target = new StdOutput(Output.PLAIN);
target.split(payload);
try {
if (target.getSessionKey() != null) {
String cipherName = getCipherName(alg.getAlgorithm());
Cipher keyCipher = Cipher.getInstance(pk.getAlgorithm());
/*,
Constants.SECURITY_PROVIDER);*/
keyCipher.init(Cipher.DECRYPT_MODE, pk);
byte[] sessionKeyBytes = keyCipher.doFinal(target.getSessionKey());
SecretKeyFactory skf = SecretKeyFactory.getInstance(cipherName);
/*,
Constants.SECURITY_PROVIDER);*/
SecretKeySpec key = new SecretKeySpec(sessionKeyBytes, cipherName);
SecretKey sessionKey = skf.generateSecret(key);
Cipher payloadCipher = Cipher.getInstance(alg.getAlgorithm());
/*,
Constants.SECURITY_PROVIDER);*/
if (target.getSessionVector() != null) {
IvParameterSpec spec = new IvParameterSpec(target.getSessionVector());
payloadCipher.init(Cipher.DECRYPT_MODE, sessionKey, spec);
} else {
payloadCipher.init(Cipher.DECRYPT_MODE, sessionKey);
}
target.setDecryptedData(payloadCipher.doFinal(target.getEncryptedData(true)));
} else {
target.setDecryptedData(target.getEncryptedData(true));
}
} catch (Exception e) {
throw new DecryptException("Payload could not be decrypted", e);
}
return target;
}
Aggregations