@@ -81,16 +81,15 @@ abstract Cipher buildUnwrappingCipher(Key key, byte[] extraInfo, int offset,
81
81
* during encryption and decryption to provide additional authenticated data (AAD).
82
82
* @return The encrypted data key.
83
83
*/
84
- public EncryptedDataKey encryptKey (final SecretKey key , final String keyName ,
84
+ public EncryptedDataKey encryptKey (final byte [] key , final String keyName ,
85
85
final Map <String , String > encryptionContext ) {
86
86
87
- final byte [] keyBytes = key .getEncoded ();
88
87
final byte [] keyNameBytes = keyName .getBytes (KEY_NAME_ENCODING );
89
88
90
89
try {
91
90
final JceKeyCipher .WrappingData wData = buildWrappingCipher (wrappingKey , encryptionContext );
92
91
final Cipher cipher = wData .cipher ;
93
- final byte [] encryptedKey = cipher .doFinal (keyBytes );
92
+ final byte [] encryptedKey = cipher .doFinal (key );
94
93
95
94
final byte [] provInfo = new byte [keyNameBytes .length + wData .extraInfo .length ];
96
95
System .arraycopy (keyNameBytes , 0 , provInfo , 0 , keyNameBytes .length );
@@ -105,27 +104,20 @@ public EncryptedDataKey encryptKey(final SecretKey key, final String keyName,
105
104
/**
106
105
* Decrypts the given encrypted data key.
107
106
*
108
- * @param algorithm The algorithm that encrypted the data key.
109
107
* @param edk The encrypted data key.
110
108
* @param keyName A UTF-8 encoded representing a name for the key.
111
109
* @param encryptionContext A key-value mapping of arbitrary, non-secret, UTF-8 encoded strings used
112
110
* during encryption and decryption to provide additional authenticated data (AAD).
113
111
* @return The decrypted key.
114
112
* @throws GeneralSecurityException If a problem occurred decrypting the key.
115
113
*/
116
- public SecretKey decryptKey (final CryptoAlgorithm algorithm , final EncryptedDataKey edk , final String keyName ,
114
+ public byte [] decryptKey (final EncryptedDataKey edk , final String keyName ,
117
115
final Map <String , String > encryptionContext ) throws GeneralSecurityException {
118
116
final byte [] keyNameBytes = keyName .getBytes (KEY_NAME_ENCODING );
119
117
120
118
final Cipher cipher = buildUnwrappingCipher (unwrappingKey , edk .getProviderInformation (),
121
119
keyNameBytes .length , encryptionContext );
122
- final byte [] rawKey = cipher .doFinal (edk .getEncryptedDataKey ());
123
- if (rawKey .length != algorithm .getDataKeyLength ()) {
124
- // Something's wrong here. Assume that the decryption is invalid.
125
- return null ;
126
- }
127
-
128
- return new SecretKeySpec (rawKey , algorithm .getDataKeyAlgo ());
120
+ return cipher .doFinal (edk .getEncryptedDataKey ());
129
121
}
130
122
131
123
static class WrappingData {
0 commit comments