【问题标题】:How to Implement PBEWithMD5AndDES algorithm in JavaScript? [duplicate]如何在 JavaScript 中实现 PBEWithMD5AndDES 算法? [复制]
【发布时间】:2017-06-01 11:12:09
【问题描述】:

我正在使用 PBEWithMD5AndDES 算法加密/解密 java 中的字符串。

 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;
    }

我已经为这个算法定义了自己的盐。但我想从 JavaScript 加密几个字符串。

我们可以在 JavaScript 中实现相同的算法(PBEWithMD5AndDES)吗?

【问题讨论】:

    标签: javascript java angularjs algorithm encryption


    【解决方案1】:

    您可以为此使用 CryptoJS 这是网址 https://github.com/sytelus/CryptoJS

    【讨论】:

    • 非常感谢曼图。 JS中不能用PBEWithMD5AndDES实现加密吗?
    • 这里是工作小提琴的网址fiddle.jshell.net/artjomb/Lpbo7yrb
    • 非常感谢您的回复,但我想知道,我们可以用 PBEWithMD5AndDES 做同样的事情吗?如果没有,那么我将在前端添加 CryptoJS 代码,并相应地更改我的 Java 代码。
    • 您可以通过REST调用使用JS调用Java API。
    • 下一次,只需将问题标记为重复,而不是复制内容。
    猜你喜欢
    • 2020-11-01
    • 2014-08-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多