我正在尝试加密并相应地解密字符串

  我正在尝试加密并相应地解密字符串. 当我将编码方案指定为’utf-8’时,我得到了预期的结果: function encrypt(text) { var cipher = crypto.createCipher(algorithm, password) var crypted = cipher.update(text, '

  我正在尝试加密并相应地解密字符串.

  当我将编码方案指定为’utf-8’时,我得到了预期的结果:

  <pre>function encrypt(text) {

    var cipher = crypto.createCipher(algorithm, password)
    var crypted = cipher.update(text, 'utf8', 'hex')
    crypted += cipher.final('hex');
    return crypted;
}
function decrypt(text) {
    var decipher = crypto.createDecipher(algorithm, password)
    var dec = decipher.update(text, 'hex', 'utf8')
    dec += decipher.final('utf8');
    return dec;
}
xt = 'The big brown fox jumps over the lazy dog.'</pre>

  Output : (utf-8 encoding)

  但是当我试图用’base-64’做它时,它给了我意想不到的结果:

  <pre>function encrypt(text) {

var cipher = crypto.createCipher(algorithm, password)
var crypted = cipher.update(text, '[base64][1]', 'hex')
crypted += cipher.final('hex');
return crypted;

}
function decrypt(text) {

var decipher = crypto.createDecipher(algorithm, password)
var dec = decipher.update(text, 'hex', 'base64')
dec += decipher.final('base64');
return dec;
re>

  Output : (base-64 encoding)

  我无法理解为什么base-64编码方案不接受空格和’.’格式正确.

  如果有人知道这一点,请帮助我更好地理解这一点.

  任何帮助表示赞赏.

  如果我理解正确的话,你用两个相同的字符串调用加密方法:大棕狐跳过懒狗.事实是,cipher.update的签名如下所示:

  cipher.update(data [,input_encoding] [,output_encoding])

  因此在第二种加密方法中,您使用’base64’作为输入编码.并且您的字符串不是base64编码的. Base64不能有空格,句号等.

  您可能希望首先在base64中对其进行编码.你可以在这里看到答案js对字符串加密解密,看看如何做到这一点:

  How to do Base64 encoding in node.js?

  然后,您可以使用第二种加密方法.解密后js对字符串加密解密,您将再次收到base64编码的字符串,并且您必须对其进行解码.上面的问题还说明了如何从base64解码.

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1912
0 评论
398

发表评论

!