Dan*_*ial 9 cryptography node.js node-crypto
我使用Node.js的Crypto库进行加密/解密,如下所示:
encrypt = function(text, passPhrase){
var cipher = crypto.createCipher('AES-128-CBC-HMAC-SHA1', passPhrase);
var crypted = cipher.update(text,'utf8','hex');
crypted += cipher.final('hex');
return crypted;
} ,
decrypt = function(text, passPhrase){
var decipher = crypto.createDecipher('AES-128-CBC-HMAC-SHA1', passPhrase)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
Run Code Online (Sandbox Code Playgroud)
加密部分没有问题.如果我发送正确的passPhrase进行解密,也没有问题.我的问题是,如果我发送'错误'的passPhrase进行解密,代码中断并抛出错误:
TypeError: Bad input string
at Decipher.Cipher.update (crypto.js:279:27)
at module.exports.decrypt (/xxxx/yyyyy/jjj/ssss/encryptionService.js:19:28)
at Object.module.exports.passwordDecryptor (/xxxx/yyyyy/jjj/ssss/encryptionService.js:59:56)
at Object.<anonymous> (/xxxx/yyyyy/jjj/ssss/test.js:32:33)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Run Code Online (Sandbox Code Playgroud)
我不希望它发生.我想要例如decrypt函数返回'Passpharse是错误的'句子.根据文档输入链接描述,这里 createDecipher 函数不接受回调函数.
Dan*_*ial 13
我用try和catch解决了这个问题.(回调函数不起作用.)
decrypt = function(text, passPhrase){
var decipher = crypto.createDecipher('AES-128-CBC-HMAC-SHA1', passPhrase);
try {
var dec = decipher.update(text,'hex','utf8');
dec += decipher.final('utf8');
return dec;
} catch (ex) {
console.log('failed');
return;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4436 次 |
| 最近记录: |