我正在尝试加密和相应地解密一个字符串。
当我指定编码方案为 "utf-8" 时,结果如预期:
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;
}
//text = 'The big brown fox jumps over the lazy dog.'
输出: (utf-8编码)
但是当我尝试使用'base-64'时,它给出了意外的结果:
function encrypt(text) {
var cipher = crypto.createCipher(algorithm, password)
var crypted = cipher.update(text, 'base64', '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;
}
输出:(使用base-64编码)
我不明白为什么base-64编码方案不能正确处理空格和右侧的“.”格式。
如果有人知道,请帮助我更好地理解。
任何帮助都将不胜感激。