Skip to content

Commit c1d94bf

Browse files
committed
Replace BouncyCastle's deprecated AESFastEngine with the default AESEngine
- Update AESEngine to use the default AES engine, following BouncyCastle's recommendations (see release-1-56 of changelog: https://www.bouncycastle.org/download/bouncy-castle-java/?filter=java%3Drelease-1-56). - Migrate to the latest API 'newInstance()' method to allow removal of @SuppressWarnings("deprecation") - Remove @SuppressWarnings("deprecation")
1 parent 409d552 commit c1d94bf

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.bouncycastle.crypto.BufferedBlockCipher;
2020
import org.bouncycastle.crypto.InvalidCipherTextException;
21+
22+
import org.bouncycastle.crypto.engines.AESEngine;
2123
import org.bouncycastle.crypto.modes.CBCBlockCipher;
2224
import org.bouncycastle.crypto.paddings.PKCS7Padding;
2325
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
@@ -45,23 +47,21 @@ public BouncyCastleAesCbcBytesEncryptor(String password, CharSequence salt, Byte
4547
}
4648

4749
@Override
48-
@SuppressWarnings("deprecation")
4950
public byte[] encrypt(byte[] bytes) {
5051
byte[] iv = this.ivGenerator.generateKey();
5152
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(
52-
new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
53+
CBCBlockCipher.newInstance(AESEngine.newInstance()), new PKCS7Padding());
5354
blockCipher.init(true, new ParametersWithIV(this.secretKey, iv));
5455
byte[] encrypted = process(blockCipher, bytes);
5556
return (iv != null) ? EncodingUtils.concatenate(iv, encrypted) : encrypted;
5657
}
5758

5859
@Override
59-
@SuppressWarnings("deprecation")
6060
public byte[] decrypt(byte[] encryptedBytes) {
6161
byte[] iv = EncodingUtils.subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength());
6262
encryptedBytes = EncodingUtils.subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length);
6363
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(
64-
new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
64+
CBCBlockCipher.newInstance(AESEngine.newInstance()), new PKCS7Padding());
6565
blockCipher.init(false, new ParametersWithIV(this.secretKey, iv));
6666
return process(blockCipher, encryptedBytes);
6767
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.security.crypto.encrypt;
1818

1919
import org.bouncycastle.crypto.InvalidCipherTextException;
20+
21+
import org.bouncycastle.crypto.engines.AESEngine;
2022
import org.bouncycastle.crypto.modes.AEADBlockCipher;
2123
import org.bouncycastle.crypto.modes.GCMBlockCipher;
2224
import org.bouncycastle.crypto.params.AEADParameters;
@@ -44,21 +46,19 @@ public BouncyCastleAesGcmBytesEncryptor(String password, CharSequence salt, Byte
4446
}
4547

4648
@Override
47-
@SuppressWarnings("deprecation")
4849
public byte[] encrypt(byte[] bytes) {
4950
byte[] iv = this.ivGenerator.generateKey();
50-
GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine());
51+
GCMBlockCipher blockCipher = (GCMBlockCipher) GCMBlockCipher.newInstance(AESEngine.newInstance());
5152
blockCipher.init(true, new AEADParameters(this.secretKey, 128, iv, null));
5253
byte[] encrypted = process(blockCipher, bytes);
5354
return (iv != null) ? EncodingUtils.concatenate(iv, encrypted) : encrypted;
5455
}
5556

5657
@Override
57-
@SuppressWarnings("deprecation")
5858
public byte[] decrypt(byte[] encryptedBytes) {
5959
byte[] iv = EncodingUtils.subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength());
6060
encryptedBytes = EncodingUtils.subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length);
61-
GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine());
61+
GCMBlockCipher blockCipher = (GCMBlockCipher) GCMBlockCipher.newInstance(AESEngine.newInstance());
6262
blockCipher.init(false, new AEADParameters(this.secretKey, 128, iv, null));
6363
return process(blockCipher, encryptedBytes);
6464
}

0 commit comments

Comments
 (0)