javax.
Crypto. Cipher is instantiated every time, and a large number of instantiations lead to failure of cipher instantiation.
Solution: put the instantiated cipher object in the HashMap, get it from the map each time, and instantiate it when it doesn’t exist. The problem is solved
// If the key is less than 16 bits, then it will be filled. The content of this if is important
int base = 16;
if (keyBytes.length % base != 0) {
int groups = keyBytes.length/base + (keyBytes.length % base != 0 ?1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
keyBytes = temp;
}
// Initialize
Security.addProvider(new BouncyCastleProvider());
// convert to JAVA key format
key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
try {
// initialize the cipher to avoid the Cipher not initialized exception when instantiating a lot
if(cipherMap.containsKey("cipher")) {
cipher = cipherMap.get("cipher");
}else {
cipher = Cipher.getInstance(algorithmStr);
//Cipher Initialize
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv.getBytes("utf-8")));
cipherMap.put("cipher", cipher);
}
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(),e);
} catch (NoSuchPaddingException e) {
log.error(e.getMessage(),e);
} catch (InvalidKeyException e) {
log.error(e.getMessage(),e);
} catch (InvalidAlgorithmParameterException e) {
log.error(e.getMessage(),e);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(),e);
}
Similar Posts:
- [Solved] exception is java.lang.NoClassDefFoundError: com.sun.crypto.provider.SunJCE
- AES secret key length error: java.security.InvalidKeyException: Illegal key size or default parameters [Solved]
- Exception: java.security.InvalidKeyException: Illegal key size [How to Solve]
- [Solved] Changing the selected file Error when uploading excel file. Net:: err_UPLOAD_FILE_CHANGED
- [Solved] Typeerror: incorrect padding occurred in python3 Base64 decoding
- Sun.misc.base64decoder import exception and handling ideas
- Error: failed building wheel for pycrypto (pycrypto installation in Python 3.6 environment under win)
- IDEA compile error: sun.misc.Base64decoder upgrade processing
- IDEA sun.misc.BASE64Encoder
- [Solved] spark Connect hive Error: javax.jdo.JDODataStoreException: Required table missing : “`DBS`” in Catalog “” Schema “”