java - CryptoJs 解密函数的加密函数
问题描述
我有下一个从正确答案复制而来的代码:
public static String decrypt(String cipherText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte[] cipherData = Base64.getDecoder().decode(cipherText);
byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16);
MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[][] keyAndIV = generateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);
SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");
IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
byte[] encrypted = Arrays.copyOfRange(cipherData, 16, cipherData.length);
Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");
aesCBC.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decryptedData = aesCBC.doFinal(encrypted);
String decryptedText = new String(decryptedData, StandardCharsets.UTF_8);
return decryptedText;
}
如何在 Java 中为此编写加密函数?我尝试过这样的事情,但它不起作用:
public static String encrypt(String plainText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[][] keyAndIV = generateKeyAndIV(32, 16, 1, getNextSalt(), secret.getBytes(StandardCharsets.UTF_8), md5);
SecretKeySpec skeySpec = new SecretKeySpec(keyAndIV[0], "AES");
IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
public static byte[] getNextSalt() {
byte[] salt = new byte[8];
RANDOM.nextBytes(salt);
return salt;
}
解决方案
encrypt
- 方法必须以 OpenSSL 格式返回数据,该格式由 Salted__ 的 ASCII 编码组成,后跟 8 字节随机生成的 salt 和实际密文,其中数据在连接后进行 Base64 编码。
但是请注意,用于 OpenSSL 格式的密钥派生函数是不安全的,并且不是标准,here。该方法的可能扩展encrypt
可能是:
public static String encrypt(String plainText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
byte[] salt = getNextSalt();
MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[][] keyAndIV = generateKeyAndIV(32, 16, 1, salt, secret.getBytes(StandardCharsets.UTF_8), md5);
SecretKeySpec skeySpec = new SecretKeySpec(keyAndIV[0], "AES");
IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
byte[] prefix = "Salted__".getBytes(StandardCharsets.US_ASCII);
byte[] prefixSaltEncrypted = new byte[prefix.length + salt.length + encrypted.length];
System.arraycopy(prefix, 0, prefixSaltEncrypted, 0, prefix.length);
System.arraycopy(salt, 0, prefixSaltEncrypted, prefix.length, salt.length);
System.arraycopy(encrypted, 0, prefixSaltEncrypted, prefix.length + salt.length, encrypted.length);
return Base64.getEncoder().encodeToString(prefixSaltEncrypted);
}
推荐阅读
- xamarin - 如何在滚动视图中延迟加载图像
- sql - LIKE 运算符、N 和 % SQL Server 不适用于 nvarchar 列
- c# - WPF,使用触发器设置背景颜色
- excel - 如何修复此代码以将值复制到列中?
- forms - FormType ChoiceType(选择)数据预设在视图 Symfony 3.4 中不起作用
- jquery - 如何从 jQuery 中的 Web 服务返回的 JSON 中提取值?
- arrays - 我想从 Plist 中读取具有属性(btnImage+btnTitle)的字典数组到 TableViewCell
- c# - 使用 XSL 文件在 C# 中进行 Schematron XML 验证
- ruby-on-rails - 未初始化的常量 ActionView::TestCase::Behavior 与 rspec
- ios - UNCalendarNotificationTrigger 不适用于完整日期组件