[英]Not able to use the info via Terminal in the code sample
I am newbie with JS and trying to write a simple code to encrypt and decrypt the info via Javascript, the sample code is below.我是JS的新手,正在尝试编写一个简单的代码来通过Javascript加密和解密信息,示例代码如下。 It is working till the encryption, but I want to take the 'mystr' into the decryption section back, which is however not working.
它一直工作到加密,但我想将“mystr”带回解密部分,但这是行不通的。 Any clue would be helpful, thnaks!
任何线索都会有所帮助,thnaks!
var crypto = require('crypto');
var mykey = crypto.createCipher('aes-256-cbc', 'mypassword');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
readline.question(`The string to Encrypt?`, (secret) => {
var mystr = mykey.update(`${secret}`, 'utf8', 'hex')
mystr += mykey.final('hex');
console.log(`${mystr}`);
// readline.close()
//deencryption
var mykey2 = crypto.createDecipher('aes-256-cbc', 'mypassword');
var mystr2 = mykey2.update(mystr, 'hex', 'utf8');
mystr2 += mykey2.final('utf8');
console.log(`${decrypt}`);
readline.close()
})
You need to create a decipher:您需要创建一个解密器:
var crypto = require('crypto');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
readline.question(`The string to Encrypt?`, (secret) => {
//create cipher
var cipher = crypto.createCipher('aes-256-cbc', 'mypassword');
//encrypt string
var mystr = cipher.update(`${secret}`, 'utf8', 'hex')
mystr += cipher.final('hex');
console.log(`${mystr}`);
//create decipher
var decipher = crypto.createDecipher('aes-256-cbc', 'mypassword');
//decrypt string
var mystr2 = decipher.update(`${mystr}`, 'hex', 'utf8')
mystr2 += decipher.final('utf8');
console.log(`${mystr2}`);
readline.close()
})
For more infos look here: https://www.w3schools.com/nodejs/ref_crypto.asp有关更多信息,请查看此处: https://www.w3schools.com/nodejs/ref_crypto.asp
look like, it was some silly mistake after adding decipher, it is working now:) thanks all for your comments, it help me to fix it.看起来,添加解码器后出现了一些愚蠢的错误,现在可以使用了:)感谢大家的评论,它帮助我修复了它。
Here the working code:这里的工作代码:
var crypto = require('crypto');
var mykey = crypto.createCipher('aes-256-cbc', 'mypassword');
var mykey2 = crypto.createDecipher('aes-256-cbc', 'mypassword');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
var mystr = '';
var mystr2 = '';
readline.question(`The string to Encrypt?`, (secret) => {
mystr = mykey.update(`${secret}`, 'utf8', 'hex')
mystr += mykey.final('hex');
console.log(`${mystr}`);
mystr2 = mykey2.update(mystr, 'hex', 'utf8');
mystr2 += mykey2.final('utf8');
console.log(`${mystr2}`);
readline.close()
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.