这个很奇怪:
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class CryptoUtils {
private static final String AES = "AES";
// private static byte[] keyValue = new byte[] // OK
// { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
private static byte[] keyValue = new byte[] // FAILS !!! WTF!
{ 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'z' };
public static String encrypt(String Data) throws Exception {
Key key = new SecretKeySpec(keyValue, AES);
Cipher c = Cipher.getInstance(AES);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
return new BASE64Encoder().encode(encVal);
}
public static String decrypt(String encryptedData) throws Exception {
Key key = new SecretKeySpec(keyValue, AES);
Cipher c = Cipher.getInstance(AES);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
return new String(decValue);
}
public static void main(String[] args) throws Exception {
System.out.println(CryptoUtils.encrypt("<PASSWORD>"));
System.out.println(CryptoUtils.decrypt("Z4i3ywGXil2QCfM6R8S5qw=="));
}
}
我使用 key “TheBestSecretKey”运行此文件,一切正常。
我用 key “TheBestSecretKez”运行这个文件,但它坏了!
在后一种情况下,它给了我一个
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
在解密方法中。
我不明白。为什么它在一种情况下有效而在另一种情况下无效?
谢谢, 伯恩哈德
最佳答案
它不会解决你的问题,但你永远不应该在没有包含模式和填充的情况下指定密码算法。原因是默认情况下它的安全性较低,并且在规范中不能保证用于加密的转换(算法/模式/填充)默认情况下与用于解密的转换相同。对于安全性,明确总是更好。所以这个:
Cipher c = Cipher.getInstance(AES);
应该变成这样:
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
任何你看到它的地方。
正如 Artjom B. 所指出的,您的 decrypt 硬编码了 main 方法中的密文,这将是具有不同 key 的不同值。
关于使用 AES : Accepts only certain keys 的 Java 字符串加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32120547/