Search in sources :

Example 1 with DecryptException

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;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeystoreException(com.adaptris.security.exc.KeystoreException) CertException(com.adaptris.security.exc.CertException) VerifyException(com.adaptris.security.exc.VerifyException) EncryptException(com.adaptris.security.exc.EncryptException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DecryptException(com.adaptris.security.exc.DecryptException) NoSuchProviderException(java.security.NoSuchProviderException) DecryptException(com.adaptris.security.exc.DecryptException)

Aggregations

AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)1 CertException (com.adaptris.security.exc.CertException)1 DecryptException (com.adaptris.security.exc.DecryptException)1 EncryptException (com.adaptris.security.exc.EncryptException)1 KeystoreException (com.adaptris.security.exc.KeystoreException)1 VerifyException (com.adaptris.security.exc.VerifyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 Cipher (javax.crypto.Cipher)1 SecretKey (javax.crypto.SecretKey)1 SecretKeyFactory (javax.crypto.SecretKeyFactory)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1