簡體   English   中英

無法通過代碼示例中的終端使用信息

[英]Not able to use the info via Terminal in the code sample

我是JS的新手,正在嘗試編寫一個簡單的代碼來通過Javascript加密和解密信息,示例代碼如下。 它一直工作到加密,但我想將“mystr”帶回解密部分,但這是行不通的。 任何線索都會有所幫助,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()                                                                            
})                                                                                              

您需要創建一個解密器:

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()
})

有關更多信息,請查看此處: https://www.w3schools.com/nodejs/ref_crypto.asp

看起來,添加解碼器后出現了一些愚蠢的錯誤,現在可以使用了:)感謝大家的評論,它幫助我修復了它。

這里的工作代碼:

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.

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