Skip to content

Commit 28ec657

Browse files
Simplifying JceKeyCipher methods
1 parent 78e956b commit 28ec657

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/main/java/com/amazonaws/encryptionsdk/internal/JceKeyCipher.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import javax.crypto.Cipher;
2323
import javax.crypto.SecretKey;
24+
import javax.crypto.spec.SecretKeySpec;
2425
import java.nio.charset.Charset;
2526
import java.nio.charset.StandardCharsets;
2627
import java.security.GeneralSecurityException;
@@ -80,7 +81,7 @@ abstract Cipher buildUnwrappingCipher(Key key, byte[] extraInfo, int offset,
8081
* during encryption and decryption to provide additional authenticated data (AAD).
8182
* @return The encrypted data key.
8283
*/
83-
public EncryptedDataKey encryptKey(final Key key, final String keyName,
84+
public EncryptedDataKey encryptKey(final SecretKey key, final String keyName,
8485
final Map<String, String> encryptionContext) {
8586

8687
final byte[] keyBytes = key.getEncoded();
@@ -112,7 +113,7 @@ public EncryptedDataKey encryptKey(final Key key, final String keyName,
112113
* @return The decrypted key.
113114
* @throws GeneralSecurityException If a problem occurred decrypting the key.
114115
*/
115-
public KeyBlob decryptKey(final CryptoAlgorithm algorithm, final EncryptedDataKey edk, final String keyName,
116+
public SecretKey decryptKey(final CryptoAlgorithm algorithm, final EncryptedDataKey edk, final String keyName,
116117
final Map<String, String> encryptionContext) throws GeneralSecurityException {
117118
final byte[] keyNameBytes = keyName.getBytes(KEY_NAME_ENCODING);
118119

@@ -124,7 +125,7 @@ public KeyBlob decryptKey(final CryptoAlgorithm algorithm, final EncryptedDataKe
124125
return null;
125126
}
126127

127-
return new KeyBlob(edk.getProviderId(), edk.getProviderInformation(), rawKey);
128+
return new SecretKeySpec(rawKey, algorithm.getDataKeyAlgo());
128129
}
129130

130131
static class WrappingData {

src/main/java/com/amazonaws/encryptionsdk/jce/JceMasterKey.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
2121
import com.amazonaws.encryptionsdk.exception.UnsupportedProviderException;
2222
import com.amazonaws.encryptionsdk.internal.JceKeyCipher;
23-
import com.amazonaws.encryptionsdk.model.KeyBlob;
2423

2524
import javax.crypto.SecretKey;
2625
import javax.crypto.spec.SecretKeySpec;
@@ -145,12 +144,10 @@ public DataKey<JceMasterKey> decryptDataKey(final CryptoAlgorithm algorithm,
145144
try {
146145
if (edk.getProviderId().equals(getProviderId())
147146
&& arrayPrefixEquals(edk.getProviderInformation(), keyIdBytes_, keyIdBytes_.length)) {
148-
final KeyBlob decryptedKey = jceKeyCipher_.decryptKey(algorithm, edk, keyId_, encryptionContext);
147+
final SecretKey decryptedKey = jceKeyCipher_.decryptKey(algorithm, edk, keyId_, encryptionContext);
149148

150149
if(decryptedKey != null) {
151-
return new DataKey<>(
152-
new SecretKeySpec(decryptedKey.getEncryptedDataKey(), algorithm.getDataKeyAlgo()),
153-
edk.getEncryptedDataKey(), edk.getProviderInformation(), this);
150+
return new DataKey<>(decryptedKey, edk.getEncryptedDataKey(), edk.getProviderInformation(), this);
154151
}
155152
}
156153
} catch (final Exception ex) {

0 commit comments

Comments
 (0)