Skip to content

Commit 4d63e2f

Browse files
committed
AesBytesEncryptor constructor that uses secret key
Fixes: gh-8402
1 parent 8e8251a commit 4d63e2f

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

crypto/src/main/java/org/springframework/security/crypto/encrypt/AesBytesEncryptor.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.springframework.security.crypto.keygen.KeyGenerators;
3737

3838
/**
39-
* Encryptor that uses 256-bit AES encryption.
39+
* Encryptor that uses AES encryption.
4040
*
4141
* @author Keith Donald
4242
* @author Dave Syer
@@ -99,9 +99,19 @@ public AesBytesEncryptor(String password, CharSequence salt,
9999

100100
public AesBytesEncryptor(String password, CharSequence salt,
101101
BytesKeyGenerator ivGenerator, CipherAlgorithm alg) {
102-
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt),
103-
1024, 256);
104-
SecretKey secretKey = newSecretKey("PBKDF2WithHmacSHA1", keySpec);
102+
this(newSecretKey("PBKDF2WithHmacSHA1", new PBEKeySpec(password.toCharArray(), Hex.decode(salt),
103+
1024, 256)), ivGenerator, alg);
104+
}
105+
106+
/**
107+
* Constructs an encryptor that uses AES encryption.
108+
*
109+
* @param secretKey the secret (symmetric) key
110+
* @param ivGenerator the generator used to generate the initialization vector. If null,
111+
* then a default algorithm will be used based on the provided {@link CipherAlgorithm}
112+
* @param alg the {@link CipherAlgorithm} to be used
113+
*/
114+
public AesBytesEncryptor(SecretKey secretKey, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) {
105115
this.secretKey = new SecretKeySpec(secretKey.getEncoded(), "AES");
106116
this.alg = alg;
107117
this.encryptor = alg.createCipher();

crypto/src/test/java/org/springframework/security/crypto/encrypt/AesBytesEncryptorTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
import org.springframework.security.crypto.codec.Hex;
2323
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
2424

25+
import javax.crypto.SecretKey;
26+
import javax.crypto.spec.PBEKeySpec;
27+
2528
import static org.assertj.core.api.Assertions.assertThat;
2629
import static org.mockito.Mockito.mock;
2730
import static org.mockito.Mockito.when;
2831
import static org.springframework.security.crypto.encrypt.AesBytesEncryptor.CipherAlgorithm.GCM;
32+
import static org.springframework.security.crypto.encrypt.CipherUtils.newSecretKey;
33+
import static org.springframework.security.crypto.password.Pbkdf2PasswordEncoder.SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1;
2934

3035
/**
3136
* Tests for {@link AesBytesEncryptor}
@@ -69,6 +74,23 @@ public void roundtripWhenUsingDefaultCipherThenEncryptsAndDecrypts() {
6974
public void roundtripWhenUsingGcmThenEncryptsAndDecrypts() {
7075
CryptoAssumptions.assumeGCMJCE();
7176
AesBytesEncryptor encryptor = new AesBytesEncryptor(this.password, this.hexSalt, this.generator, GCM);
77+
78+
byte[] encryption = encryptor.encrypt(this.secret.getBytes());
79+
assertThat(new String(Hex.encode(encryption)))
80+
.isEqualTo("4b0febebd439db7ca77153cb254520c3e4d61ae38207b4e42b820d311dc3d4e0e2f37ed5ee");
81+
82+
byte[] decryption = encryptor.decrypt(encryption);
83+
assertThat(new String(decryption)).isEqualTo(this.secret);
84+
}
85+
86+
@Test
87+
public void roundtripWhenUsingSecretKeyThenEncryptsAndDecrypts() {
88+
CryptoAssumptions.assumeGCMJCE();
89+
PBEKeySpec keySpec = new PBEKeySpec(this.password.toCharArray(), Hex.decode(this.hexSalt),
90+
1024, 256);
91+
SecretKey secretKey = newSecretKey(PBKDF2WithHmacSHA1.name(), keySpec);
92+
AesBytesEncryptor encryptor = new AesBytesEncryptor(secretKey, this.generator, GCM);
93+
7294
byte[] encryption = encryptor.encrypt(this.secret.getBytes());
7395
assertThat(new String(Hex.encode(encryption)))
7496
.isEqualTo("4b0febebd439db7ca77153cb254520c3e4d61ae38207b4e42b820d311dc3d4e0e2f37ed5ee");

0 commit comments

Comments
 (0)