簡體   English   中英

我如何在 npm 的病房后解密

[英]How do i decrypt after wards in npm

我有來自 nodejs.org 的文件加密示例

const fs = require('fs');

const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
// Use the async `crypto.scrypt()` instead.
const key = crypto.scryptSync(password, 'salt', 24);
// Use `crypto.randomBytes()` to generate a random iv instead of the static iv
// shown here.
const iv = Buffer.alloc(16, 0); // Initialization vector.

const cipher = crypto.createCipheriv(algorithm, key, iv);

const input = fs.createReadStream('test.js');
const output = fs.createWriteStream('test.enc');

input.pipe(cipher).pipe(output);
}

我如何做相反的事情並使用相同的密鑰讓 test.enc 到 test.js?

提前致謝

您可以使用類似的過程來解密使用上述代碼加密的文件。

我們將解密后的文件寫入“test-decoded.js”,這樣我們就不會覆蓋我們的原始文件。

我還要指出,理想情況下,您應該使用隨機值填充 IV 並將其與加密數據一起存儲,但如果您只是測試它並沒有太大關系!

const fs = require('fs');
const crypto = require("crypto");

const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
const key = crypto.scryptSync(password, 'salt', 24);
const iv = Buffer.alloc(16, 0); // Initialization vector.

const decryptInput = fs.createReadStream('test.enc');
const decryptOutput = fs.createWriteStream('test-decoded.js');
const decipher = crypto.createDecipheriv(algorithm, key, iv);
decryptInput.pipe(decipher).pipe(decryptOutput);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM