Node.js 에서 암복호화 하기
AES 방식(Key)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var key = 'this is password key'; var input = 'This_is_Password!'; var cipher = crypto.createCipher('aes192', key); cipher.update(input, 'utf8', 'base64'); var cipheredOutput = cipher.final('base64'); var decipher = crypto.createDecipher('aes192', key); decipher.update(cipheredOutput, 'base64', 'utf8'); var decipheredOutput = decipher.final('utf8'); console.log('original string: ' + input); console.log('ciphered string: ' + cipheredOutput); console.log('deciphered string: ' + decipheredOutput); |
http://oraclejavastudy.tistory.com/entry/Nodejs-%EA%B0%95%EC%A2%8C-%EC%9E%90%EB%A3%8C-crypto-%EB%AA%A8%EB%93%88 RSA 방식(public.key, private.key)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
var crypto = require('crypto'); //openssl genrsa -out private.key 2048 //openssl rsa -in private.key -out public.key -pubout var PUBKEY = fs.readFileSync(__dirname+'/public.key'); var PRIVKEY = fs.readFileSync(__dirname+'/private.key'); // RSA PRIVATE ENCRYPT -> PUBLIC DECRYPT // myMSG = "This_is_Password!"; function privENC_pubDEC(originMSG){ encmsg = crypto.privateEncrypt(PRIVKEY, Buffer.from(originMSG, 'utf8') ).toString('base64'); msg = crypto.publicDecrypt(PUBKEY, Buffer.from(encmsg, 'base64')); console.log("Encrypted with private key : "+encmsg); console.log(msg.toString()); } function pubENC_privDEC(originMSG){ encmsg = crypto.publicEncrypt(PUBKEY, Buffer.from(originMSG, 'utf8') ).toString('base64'); msg = crypto.privateDecrypt(PRIVKEY, Buffer.from(encmsg, 'base64')); console.log("\nEncrypted with public key : "+encmsg); console.log(msg.toString()); } privENC_pubDEC(myMSG); pubENC_privDEC(myMSG); |
http://indienote.tistory.com/253
Posted in node.js
Leave a comment