解密加密字符串时会出现此随机错误。如果我们第二次尝试解密,它就会起作用
String salt = "This is my test";
byte[] saltBytes = salt.getBytes("UTF-8");
byte[] ivBytes = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07,
0x72, 0x6F, 0x5A, (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72,
0x6F, 0x5A };
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec spec = new PBEKeySpec(encryptionKey.toCharArray(), saltBytes,
iterationCount, keySize);
SecretKey secretKey = factory.generateSecret(spec);
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
encryptionCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(ivBytes));
decryptionCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
decryptionCipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));
byte[] encryptedTextBytes=encryptionCipher.doFinal(stringToEncrypt.getBytes("UTF- 8"));
encrypted = new Base64().encodeAsString(encryptedTextBytes);
byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);
decryptedTextBytes = decryptionCipher.doFinal(encryptedTextBytes);
return new String(decryptedTextBytes);
问题是随机发生的。