forked from nodejsera/30daysofnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
30 days of node - Day 10 : Symmetric encryption and decryption using …
…crypto module of node.js
- Loading branch information
Showing
7 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
Example of Asymmetric encryption | ||
Encrypting using private key and decrypting using public key | ||
File Name : enc-priv-dec-pub.js | ||
Author : @nodejsera | ||
**/ | ||
//Including the required modules | ||
var crypto = require('crypto'); | ||
var fs = require('fs'); | ||
|
||
//Reading the Private Key | ||
privK = { | ||
key: fs.readFileSync('priv.key').toString(), | ||
passphrase: 'nodejsera' | ||
} | ||
//Passing the text to be encrypted using private key | ||
var buf = Buffer.from('rishabh', 'utf8'); | ||
|
||
//Encrypting the text | ||
secretData = crypto.privateEncrypt(privK, buf); | ||
//printing the encrypted text | ||
console.log(secretData.toString('utf8')); | ||
//reading the Public key | ||
pubK = fs.readFileSync('pub.key').toString(); | ||
//decrypting the text using public key | ||
origData = crypto.publicDecrypt(pubK, secretData) | ||
//Printing the original content | ||
console.log(origData.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
Example of Asymmetric encryption | ||
Encrypting using public key and decrypting using private key | ||
File Name : enc-pub-dec-priv.js | ||
Author : @nodejsera | ||
**/ | ||
//Including the required modules | ||
var crypto = require('crypto'); | ||
var fs = require('fs'); | ||
|
||
//Reading the Public Key | ||
pubK = privK = fs.readFileSync('pub.key').toString(); | ||
|
||
//Passing the text to be encrypted using private key | ||
var buf = Buffer.from('This is secret code', 'utf8'); | ||
|
||
//Encrypting the text | ||
secretData = crypto.publicEncrypt(pubK, buf); | ||
//printing the encrypted text | ||
console.log(secretData.toString('utf8')); | ||
//reading the Private key | ||
privK = { | ||
key: fs.readFileSync('priv.key').toString(), | ||
passphrase: 'nodejsera' | ||
} | ||
//decrypting the text using public key | ||
origData = crypto.privateDecrypt(privK, secretData) | ||
//Printing the original content | ||
console.log(origData.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
Proc-Type: 4,ENCRYPTED | ||
DEK-Info: DES-EDE3-CBC,D881D8AD164DDF78 | ||
|
||
vVACPx+pbY1YEiDJj81czSGV4I0v0e0aXyMQvWJO536Yjr2JyhAt7+5o1YImoT9C | ||
tf6q1vNo56XgzwYfVoxnH+BIjDZuRq9M8E4ebdg0ptXDvkkffQv7EebduGWVL0JW | ||
f/GEG9DQw2VprKT84LUTIQB0o6HvnV5j8tmbQfOulwcwUFlBufa2YAH7pS+iZjfN | ||
nrLCuCXEi5tpR+Qtptv5BIz0W36OzrjfvKQQiTKP7+HxGrC+B+6QSNNSr5kMGlH6 | ||
asIa7YQt6PnDNyySMbKwedwb5uxOZ+fAoGEHgXl/G2AS45nI++a4+k80k8l3njQH | ||
pwn+P+1VBCUHo79uVULYJqOY0Kjkpkrm0JrZTYPcL6SYAAa0sQ4on90tRKLjR04y | ||
CeZTWC0un2jRr225plIc6L9hbCA9eaIZM+J0LuoWNfk8yLQ1wQFlVNeH2qhlTDlM | ||
54K2K3lFZHa1iG0KjvztVZjvf7pbv+bizKe9Cgqv6MvC5u8qesHr/OAZqc3HHZ8K | ||
Zf5TKBejpD8XgMuYnabOiglOQ/R96WGzfMUwwGF8mvvOuB1Fz21pzxIefrVnLm0T | ||
TlL93uiH3oKNF9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sqsPEMlsKb | ||
-----END RSA PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo9sqaQbz3ddaxd8uQG3K | ||
GIr98k/NS6xUxssTadR4uOgiJtcm9uknBgAAa2+RuSup60uCGW0KFiPHYfpDYzr8 | ||
V8ZnErGkl2VUEGEiduNcwvaqOcGYWu4FoOAVn6tuf62l8qChn+S5CHV2Kx2UszWS | ||
00Gcryl/Dm/cFKZ0bmWfa1EYtMcpbEZJBM79RQKB1WL17fYs5EXkFmf5eVQ2C4O7 | ||
VtxeyrgW4ner7KpqytYc/z/XD6FZ+kEpMQcle9CvIGFi78yb8YoW+eVxgyZfloQv | ||
X1aaRnmZMu6mLNekv1ZZSCRWLjzO5x4ODDNRqjvMpqmUTJkK4j2qrad41FiRoXDl | ||
uQIDAQAB | ||
-----END PUBLIC KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var crypto = require('crypto'),algorithm = 'aes-256-ctr',password = 'RJ23edrf'; | ||
//Here "aes-256-cbc" is the advance encryption standard we are using for encryption. | ||
//Text is the Confidential data which we need to encrypt using 'password'(Key). | ||
|
||
function encrypt(text){ | ||
var cipher = crypto.createCipher(algorithm,password) | ||
var crypted = cipher.update(text,'utf8','hex') | ||
crypted += cipher.final('hex'); | ||
return crypted; | ||
} | ||
|
||
//Here "aes-256-cbc" is the advance encyption standard we used for encrytion. | ||
//Text is the Cipher which we need to decrypt using 'password'(Key). | ||
function decrypt(text){ | ||
var decipher = crypto.createDecipher(algorithm,password) | ||
var dec = decipher.update(text,'hex','utf8') | ||
dec += decipher.final('utf8'); | ||
return dec; | ||
} | ||
|
||
//Actual content | ||
var text = "Nodejsera for all web development languages"; | ||
//Calling the encrypt function and printing the encrypted content | ||
var e = encrypt(text); | ||
console.log(e); | ||
//calling the decrypt function and printing the decrypted content | ||
var d = decrypt(e); | ||
console.log(d); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
Proc-Type: 4,ENCRYPTED | ||
DEK-Info: DES-EDE3-CBC,05391439A8237D95 | ||
|
||
w492ZsqxSXeM4XnVK4TMW1DpXLf3+yJqhXdjXfPRGnVelIcPkb/zrrUtYqFKqHH+ | ||
2UHIWW5yWEJ7UxkHBd436QkePt88fFxHX8X8HTUvHtVQ/3y3TMghFSqyG/An2SDQ | ||
AXEoYioxU/cXixIzsB00Qe9jbmsmrfId7xfPoPeSl8GBOt4N8gEfMROoGVaxyN9n | ||
EN0u3LZRs+u2OBPgYPbMwAD8NdGPRFFL21wpAfG9z6dCC0wrTK16ecjH2+f53zeD | ||
rRx534TO167Fy79jS1YOfsn+Aeyi1FMpinojPDQSaQ+aNGEmoY6P1hd+Xn85lz5Q | ||
VuKkH7LDcsNfulsUTzGiL3rM9+YSgUGL1EjPmfi7eRJpWJ3jdfOUwFV96Z2baKhg | ||
NgAfMqJhq/Gdpphdfg9KCyM6SLDXJaeaBqOM0spPvhlBAq9J7KcKsPJMAOaAttsM | ||
qqzwzq4orknRX71HEs+qxJqd7DtgRt4khlFZkunkFpOAlzgOXVkfzKFOP3DhPkrI | ||
Ft84DAU5MN8MIf/ZYX4VNqmM5ytnC5tf8AHSFEKdRVbSkmr/ZWN1Exfegmlgp3TR | ||
J++UpAIwEhdI1kgCP4awvAJBcHNvtqt0fOHvuI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contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzIUe9DwLYTHe15MKVLdw | ||
c1Qz0UanMc1DqsXz9/vX2edjbLWaz5KSLbTU/m+iZKBqNc7AtvTuC3CSH9V64U3G | ||
kE5qClnVptHMA7c1UzDHwA2ZldOEIGT1xhf+/xj6ts+XmXATQJooMcYAeQGOqdsr | ||
kDWi3XUk1kunMFOzzDXGb0UXn/BNknEm/RB5niBbuP0J1mYiH9qCssR0a0YJct5p | ||
pVq/49yN7E62JPKV/43vBOX4nxej2NzQ9GWtb8x080EA8aZ377yuSO4d+Oa+A1pq | ||
Kr8S8FA/wthCrcyzDpBwu8oovjbRmvGZVITnuPJDesqCpUWFPrBYbMLarRHl57aM | ||
JwIDAQAB | ||
-----END PUBLIC KEY----- |