Java 使用 AES 的字符串加密:仅接受某些密钥
Java String Encryption with AES: Accepts only certain keys
这个很奇怪:
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=="));
}
}
我 运行 这个文件的密钥 'TheBestSecretKey' 一切都很好。
我 运行 这个文件有密钥 'TheBestSecretKez' 并且它坏了!
在后一种情况下,它给了我一个
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
解密方法内部。
我不明白。为什么它在一种情况下有效而在另一种情况下无效?
谢谢,
伯恩哈德
它不会解决你的问题,但你永远不应该在没有包含模式和填充的情况下指定密码算法。原因是默认情况下它的安全性较低,并且规范中不能保证用于加密的转换 (algorithm/mode/padding) 默认情况下与用于解密的转换相同。对于安全性,明确总是更好。所以这个:
Cipher c = Cipher.getInstance(AES);
应该变成这样:
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
随处可见。
正如 Artjom B. 指出的那样,问题是您的 decrypt 硬编码了 main 方法中的密文,这将是具有不同密钥的不同值。
这个很奇怪:
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=="));
}
}
我 运行 这个文件的密钥 'TheBestSecretKey' 一切都很好。
我 运行 这个文件有密钥 'TheBestSecretKez' 并且它坏了!
在后一种情况下,它给了我一个
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
解密方法内部。
我不明白。为什么它在一种情况下有效而在另一种情况下无效?
谢谢, 伯恩哈德
它不会解决你的问题,但你永远不应该在没有包含模式和填充的情况下指定密码算法。原因是默认情况下它的安全性较低,并且规范中不能保证用于加密的转换 (algorithm/mode/padding) 默认情况下与用于解密的转换相同。对于安全性,明确总是更好。所以这个:
Cipher c = Cipher.getInstance(AES);
应该变成这样:
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
随处可见。
正如 Artjom B. 指出的那样,问题是您的 decrypt 硬编码了 main 方法中的密文,这将是具有不同密钥的不同值。