2727import java .security .InvalidKeyException ;
2828import java .security .MessageDigest ;
2929import java .security .NoSuchAlgorithmException ;
30+ import java .security .NoSuchProviderException ;
3031import java .security .SecureRandom ;
3132import javax .crypto .BadPaddingException ;
3233import javax .crypto .Cipher ;
@@ -52,6 +53,16 @@ public class CTRCipherGenerator {
5253
5354 static int sha256BufferLen = 65536 ;
5455
56+ private static String AesCipherProvider = "" ;
57+
58+ public static String getAesCipherProvider () {
59+ return AesCipherProvider ;
60+ }
61+
62+ public static void setAesCipherProvider (String aesCipherProvider ) {
63+ AesCipherProvider = aesCipherProvider ;
64+ }
65+
5566 public SecureRandom getSecureRandom () {
5667 return secureRandom ;
5768 }
@@ -87,6 +98,7 @@ public byte[] getCryptoKeyBytes() {
8798 public byte [] getRandomCryptoKeyBytes () {
8899 return getRandomBytes (CRYPTO_KEY_BYTES_LEN );
89100 }
101+
90102 public byte [] getRandomBytes (int randomBytesLen ) {
91103 byte [] randomBytes ;
92104 randomBytes = new byte [randomBytesLen ];
@@ -130,23 +142,22 @@ public CTRCipherGenerator(
130142 public CipherInputStream getAES256DecryptedStream (
131143 InputStream ciphertextInput , byte [] object_CryptoIvBytes , byte [] object_CryptoKeyBytes )
132144 throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException ,
133- InvalidKeyException {
145+ InvalidKeyException , NoSuchProviderException {
134146 SecretKeySpec keySpec = new SecretKeySpec (object_CryptoKeyBytes , "AES" );
135147 IvParameterSpec ivSpec = new IvParameterSpec (object_CryptoIvBytes );
136- Cipher cipher = Cipher . getInstance ( AES_ALGORITHM );
148+ Cipher cipher = getAesCipher ( );
137149 cipher .init (Cipher .DECRYPT_MODE , keySpec , ivSpec );
138150 return new CipherInputStream (ciphertextInput , cipher );
139151 }
140152
141153 public CipherInputStream getAES256EncryptedStream (
142154 InputStream plaintextInput , byte [] object_CryptoIvBytes , byte [] object_CryptoKeyBytes )
143155 throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException ,
144- InvalidKeyException {
156+ InvalidKeyException , NoSuchProviderException {
145157 SecretKeySpec keySpec = new SecretKeySpec (object_CryptoKeyBytes , "AES" );
146158 IvParameterSpec ivSpec = new IvParameterSpec (object_CryptoIvBytes );
147- Cipher cipher = Cipher . getInstance ( AES_ALGORITHM );
159+ Cipher cipher = getAesCipher ( );
148160 cipher .init (Cipher .ENCRYPT_MODE , keySpec , ivSpec );
149-
150161 return new CipherInputStream (plaintextInput , cipher );
151162 }
152163
@@ -161,10 +172,10 @@ public static byte[] getBytesFromBase64(String cryptoInfo) throws UnsupportedEnc
161172 public static byte [] getAESEncryptedBytes (
162173 byte [] plainText , int plainTextOffset , int plainTextLength , byte [] aesKeyBytes , byte [] aesIvBytes )
163174 throws IllegalBlockSizeException , BadPaddingException , InvalidAlgorithmParameterException ,
164- InvalidKeyException , NoSuchPaddingException , NoSuchAlgorithmException {
175+ InvalidKeyException , NoSuchPaddingException , NoSuchAlgorithmException , NoSuchProviderException {
165176 SecretKeySpec keySpec = new SecretKeySpec (aesKeyBytes , "AES" );
166177 IvParameterSpec ivSpec = new IvParameterSpec (aesIvBytes );
167- Cipher cipher = Cipher . getInstance ( AES_ALGORITHM );
178+ Cipher cipher = getAesCipher ( );
168179 cipher .init (Cipher .ENCRYPT_MODE , keySpec , ivSpec );
169180 return cipher .doFinal (plainText , plainTextOffset , plainTextLength );
170181 }
@@ -225,6 +236,13 @@ public static byte[] getFileSha256Bytes(String filePath) throws IOException, NoS
225236 return fileSha256Digest .digest ();
226237 }
227238
239+ public static Cipher getAesCipher ()
240+ throws NoSuchPaddingException , NoSuchAlgorithmException , NoSuchProviderException {
241+ return AesCipherProvider .equals ("" )
242+ ? Cipher .getInstance (AES_ALGORITHM )
243+ : Cipher .getInstance (AES_ALGORITHM , AesCipherProvider );
244+ }
245+
228246 public SHA256Info computeSHA256HashAES (
229247 InputStream plainTextStream ,
230248 byte [] object_CryptoIvBytes ,
@@ -235,7 +253,7 @@ public SHA256Info computeSHA256HashAES(
235253 try {
236254 SecretKeySpec keySpec = new SecretKeySpec (object_CryptoKeyBytes , "AES" );
237255 IvParameterSpec ivSpec = new IvParameterSpec (object_CryptoIvBytes );
238- Cipher cipher = Cipher . getInstance ( AES_ALGORITHM );
256+ Cipher cipher = getAesCipher ( );
239257 cipher .init (Cipher .ENCRYPT_MODE , keySpec , ivSpec );
240258 bis = new BufferedInputStream (plainTextStream );
241259 MessageDigest plainTextSha256 = MessageDigest .getInstance ("SHA-256" );
0 commit comments