0
我使用PBEWithMD5AndDES算法来加密/解密java中的一个字符串。如何在JavaScript中实现PBEWithMD5AndDES算法?
public static String encrypt(String originalPassword) throws Exception {
String methodName = "encrypt -->";
_logger.debug(methodName + Constants.CALLED);
String encryptedString = null;
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
encryptedString = base64Encode(pbeCipher.doFinal(originalPassword.getBytes("UTF-8")));
_logger.debug(methodName + "encrypted string " + encryptedString);
} catch (Exception e) {
_logger.error(methodName + "Encryption failed due to: " + e.getMessage());
throw new Exception("Failed to Encrypt String");
}
_logger.debug(methodName + Constants.END);
return encryptedString;
}
public static String decrypt(String encryptedPassword) throws Exception {
String methodName = "decrypt -->";
_logger.debug(methodName + Constants.CALLED);
String decryptedString = null;
try {
_logger.debug(methodName + " string to decrypt " + encryptedPassword);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
decryptedString = new String(pbeCipher.doFinal(base64Decode(encryptedPassword)), "UTF-8");
} catch (Exception e) {
_logger.error(methodName + "Decryption failed due to: " + e.getMessage());
throw new Exception("Failed to Decrypt String");
}
_logger.debug(methodName + Constants.END);
return decryptedString;
}
我已经为此算法定义了我自己的salt。但我想从JavaScript加密一些字符串。
我们可以在JavaScript中实现相同的算法(PBEWithMD5AndDES)吗?
非常感谢Mantu。 JS中的PBEWithMD5AndDES不可能实现加密吗? –
这里是工作小提琴的网址http://fiddle.jshell.net/artjomb/Lpbo7yrb/ –
我真的很感激你的回复,但我想知道,我们可以用PBEWithMD5AndDES做同样的事情吗?如果没有,那么我会在前端添加CryptoJS代码,并且相应地更改我的Java代码。 –