Skip to content

Commit 41a3736

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 41a3736

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

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

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

1919
import org.bouncycastle.crypto.BufferedBlockCipher;
2020
import org.bouncycastle.crypto.InvalidCipherTextException;
21+
import org.bouncycastle.crypto.engines.AESEngine;
2122
import org.bouncycastle.crypto.modes.CBCBlockCipher;
2223
import org.bouncycastle.crypto.paddings.PKCS7Padding;
2324
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
@@ -45,23 +46,21 @@ public BouncyCastleAesCbcBytesEncryptor(String password, CharSequence salt, Byte
4546
}
4647

4748
@Override
48-
@SuppressWarnings("deprecation")
4949
public byte[] encrypt(byte[] bytes) {
5050
byte[] iv = this.ivGenerator.generateKey();
5151
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(
52-
new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
52+
CBCBlockCipher.newInstance(AESEngine.newInstance()), new PKCS7Padding());
5353
blockCipher.init(true, new ParametersWithIV(this.secretKey, iv));
5454
byte[] encrypted = process(blockCipher, bytes);
5555
return (iv != null) ? EncodingUtils.concatenate(iv, encrypted) : encrypted;
5656
}
5757

5858
@Override
59-
@SuppressWarnings("deprecation")
6059
public byte[] decrypt(byte[] encryptedBytes) {
6160
byte[] iv = EncodingUtils.subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength());
6261
encryptedBytes = EncodingUtils.subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length);
6362
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(
64-
new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
63+
CBCBlockCipher.newInstance(AESEngine.newInstance()), new PKCS7Padding());
6564
blockCipher.init(false, new ParametersWithIV(this.secretKey, iv));
6665
return process(blockCipher, encryptedBytes);
6766
}

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

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

1919
import org.bouncycastle.crypto.InvalidCipherTextException;
20+
import org.bouncycastle.crypto.engines.AESEngine;
2021
import org.bouncycastle.crypto.modes.AEADBlockCipher;
2122
import org.bouncycastle.crypto.modes.GCMBlockCipher;
2223
import org.bouncycastle.crypto.params.AEADParameters;
@@ -44,21 +45,19 @@ public BouncyCastleAesGcmBytesEncryptor(String password, CharSequence salt, Byte
4445
}
4546

4647
@Override
47-
@SuppressWarnings("deprecation")
4848
public byte[] encrypt(byte[] bytes) {
4949
byte[] iv = this.ivGenerator.generateKey();
50-
GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine());
50+
GCMBlockCipher blockCipher = (GCMBlockCipher) GCMBlockCipher.newInstance(AESEngine.newInstance());
5151
blockCipher.init(true, new AEADParameters(this.secretKey, 128, iv, null));
5252
byte[] encrypted = process(blockCipher, bytes);
5353
return (iv != null) ? EncodingUtils.concatenate(iv, encrypted) : encrypted;
5454
}
5555

5656
@Override
57-
@SuppressWarnings("deprecation")
5857
public byte[] decrypt(byte[] encryptedBytes) {
5958
byte[] iv = EncodingUtils.subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength());
6059
encryptedBytes = EncodingUtils.subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length);
61-
GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine());
60+
GCMBlockCipher blockCipher = (GCMBlockCipher) GCMBlockCipher.newInstance(AESEngine.newInstance());
6261
blockCipher.init(false, new AEADParameters(this.secretKey, 128, iv, null));
6362
return process(blockCipher, encryptedBytes);
6463
}

0 commit comments

Comments
 (0)