Node.js/crypto:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第123行: 第123行:


const salt = 'lanqian';
const salt = 'lanqian';
const str = 'kangkk';
const pubkey = 'kangkk';
const key = crypto.scryptSync(str, salt, 24);
const key = crypto.scryptSync(pubkey, salt, 24);
const cipher = crypto.createCipheriv('aes192', key, Buffer.alloc(16, 0));
const cipher = crypto.createCipheriv('aes192', key, Buffer.alloc(16, 0));
console.log(cipher);</syntaxhighlight>|{{consoleout|<pre>
console.log(cipher);</syntaxhighlight>|{{consoleout|<pre>
第134行: 第134行:
   [Symbol(kHandle)]: {}
   [Symbol(kHandle)]: {}
}
}
</pre>}}}}
输入加密内容。
{{leftrightflex|<syntaxhighlight lang="javascript">
const crypto = require('crypto');
const salt = 'lanqian';
const pubkey = 'kangkk';
const key = crypto.scryptSync(pubkey, salt, 24);
const cipher = crypto.createCipheriv('aes192', key, Buffer.alloc(16, 0));
const data = 'i l y';
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted);</syntaxhighlight>|{{consoleout|<pre>
c5543cd5004e5491ed7cfbeaa3a3d3e2
</pre>}}}}
解密。
{{leftrightflex|<syntaxhighlight lang="javascript">
const crypto = require('crypto');
const salt = 'lanqian';
const pubkey = 'kangkk';
const key = crypto.scryptSync(pubkey, salt, 24);
const data = 'c5543cd5004e5491ed7cfbeaa3a3d3e2';
const decipher = crypto.createDecipheriv('aes192', key, Buffer.alloc(16, 0));
let decrypted = decipher.update(data, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(decrypted);</syntaxhighlight>|{{consoleout|<pre>
i l y
</pre>}}}}
</pre>}}}}