使用Node.js的加密庫,我加密的PDF像這樣:解密了內容加密的PDF後,解密的結果是空
//encrypting the pdf
fs.readFile('./full.pdf', function (err,data) {
if (err) {
return console.log(err);
}
var cipher = crypto.createCipher('aes-256-cbc', encPassword);
var text = data;
var crypted = cipher.update(text,'utf8','hex');
crypted += cipher.final('hex');
console.log(crypted);
});
然後我去crypted
並運行它通過:
var decipher = crypto.createDecipher('aes-256-cbc', encPassword);
var dec = decipher.update(args[0],'hex','utf8');
dec += decipher.final('utf8');
fs.writeFileSync('./output.pdf', dec);
./output.pdf現在是一個空白頁面,它應該是充滿原始內容。
注
當我使用完全相同的代碼(不含fs.writeFileSync('./output.pdf', dec);
)來加密和解密一個簡單的字符串進行加密和解密完美。
在'decipher.update'調用之前''args [0]'中有什麼? – vbo
由於加密和解密是2個獨立的腳本。爲了解密,我把第一個('$ crypted')的輸出作爲解密腳本的命令行參數使用 – bzupnick
如果你試圖在一個腳本中加入編碼和解碼步驟,並通過js變量傳遞它,該怎麼辦? – vbo