Created
May 28, 2021 17:20
-
-
Save EarlGeorge/0893b3d4066d69d5911475b1922d5b1e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const crypto = require('crypto'); | |
function decryptWithPrivateKey(privateKey, encryptedMessage) { | |
return crypto.privateDecrypt(privateKey, encryptedMessage); | |
} | |
function decryptWithPublicKey(publicKey, encryptedMessage) { | |
return crypto.publicDecrypt(publicKey, encryptedMessage); | |
} | |
module.exports.decryptWithPrivateKey = decryptWithPrivateKey; | |
module.exports.decryptWithPublicKey = decryptWithPublicKey; |
This file contains hidden or 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
const crypto = require('crypto'); | |
function encryptWithPublicKey(publicKey, message) { | |
const bufferMessage = Buffer.from(message, 'utf8'); | |
return crypto.publicEncrypt(publicKey, bufferMessage); | |
} | |
function encryptWithPrivateKey(privateKey, message) { | |
const bufferMessage = Buffer.from(message, 'utf8'); | |
return crypto.privateEncrypt(privateKey, bufferMessage); | |
} | |
module.exports.encryptWithPublicKey = encryptWithPublicKey; | |
module.exports.encryptWithPrivateKey = encryptWithPrivateKey; |
This file contains hidden or 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
const crypto = require('crypto'); | |
const fs = require('fs'); | |
function genKeyPair() { | |
// Generates an object where the keys are stored in properties `privateKey` and `publicKey` | |
const keyPair = crypto.generateKeyPairSync('rsa', { | |
modulusLength: 4096, // bits - standard for RSA keys | |
publicKeyEncoding: { | |
type: 'pkcs1', // "Public Key Cryptography Standards 1" | |
format: 'pem' // Most common formatting choice | |
}, | |
privateKeyEncoding: { | |
type: 'pkcs1', // "Public Key Cryptography Standards 1" | |
format: 'pem' // Most common formatting choice | |
} | |
}); | |
// Create the public key file | |
fs.writeFileSync(__dirname + '/id_rsa_pub.pem', keyPair.publicKey); | |
// Create the private key file | |
fs.writeFileSync(__dirname + '/id_rsa_priv.pem', keyPair.privateKey); | |
} | |
// Generates the keypair | |
genKeyPair(); |
This file contains hidden or 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
-----BEGIN RSA PRIVATE KEY----- | |
MIIJKwIBAAKCAgEA2I24W1DFE0I2O8tcUfOHq44TJRgwcRzHcDFO20Y+OkQQxKpi | |
UHVSnBepCmCMrf8f/Qq+OQl1+6z3UsJUxjX6Yr/BUoPs5kOVhSvBJI3r3V6hX9GR | |
AYyUmAGCvpYs6ViyrtpUrIHVA1f+TLZmfxjtns4TwoWOQd06U+mA3mR2OoOhCQfg | |
u2tsch1M6IUFmEFUz1aXudwyfclaRUm40eSSB0JGE64sQHCKjZScF/+yUJvamc0+ | |
cCeCyR4aBbRcLNEsNS+1wx+F7IXWeJnK98cnK4TOD/tKQEaPP5WNmCo8GC5KwSKr | |
l/lhlPg2/mOayZQCAnAMqUtCMCp/YHC5xj6LfnQWLx5SuA8bTDR91biLkI2rcU6Z | |
wQ9WJevVIjrkzD8mRA5RKju0iiUmnAtD6ppB/qksIM27Tne9U8lEL7vQeAuC8RPq | |
FD2FdYry1m+NlFp7yPj7KCNW+w1Nz5CV+BO8ibfl21J/O06PhNiID5gIy7nBYe/g | |
+Lg4XmdQfsf4RwItbl+R5xZsUyKbrELtgghRE7xZq8/dU0x8QePBZ4zB6uM40omu | |
H5I8FZVorwdZ/KTJ1rh9UopipOGRBnilFSgt0oL1N5KHLs4DMht33NTW0KUFFA/X | |
T7Ef5uDXx5fmRjRt2VRxF1bnEw/iCC1gSaCc6IMaDITe+lbqPv6mpOJ5BOsCAwEA | |
AQKCAgEA00ozvqUoJIpN8DmTckJy9WhCNcdMPJEf5s+1aS04OhF/auPz2R0f5T0Q | |
qhdaK8rpozOANpaMj2tMRWtm255/FyqR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e1gdc/kf6BgzAiSHBi5OzDlil3j9kY44JB | |
0VoyH3K2c9P8+SKItkXqWqNT3QMNja+qb5sgT0KwcsuSw37/3UO0bb5mzf+j7mlL | |
J/6xnTXPbVkTMbYqG3vkbAAVj8GVn3ebPAGuyko7kZj7PgIWTDXkFdI7kZwvDglN | |
Yr8OrYa5RkqK8LQRIkO+VLB99vOy0Fbp3zXH7Fv1ZIGrNlRSZf5dAh4R1g5UkPJl | |
tsBPy+Tp4xoq35ECEvytqdXso0w8SjVyJg+Ge++YfdcF008saWuuv5OOynSw85k= | |
-----END RSA PRIVATE KEY----- |
This file contains hidden or 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
-----BEGIN RSA PUBLIC KEY----- | |
MIICCgKCAgEA2I24W1DFE0I2O8tcUfOHq44TJRgwcRzHcDFO20Y+OkQQxKpiUHVS | |
nBepCmCMrf8f/Qq+OQl1+6z3UsJUxjX6Yr/BUoPs5kOVhSvBJI3r3V6hX9GRAYyU | |
mAGCvpYs6ViyrtpUrIHVA1f+TLZmfxjtns4TwoWOQd06U+mA3mR2OoOhCQfgu2ts | |
ch1M6IUFmEFUz1aXudwyfclaRUm40eSSB0JGE64sQHCKjZScF/+yUJvamc0+cCeC | |
yR4aBbRcLNEsNS+1wx+F7IXWeJnK98cnK4TOD/tKQEaPP5WNmCo8GC5KwSKrl/lh | |
lPg2/mOayZQCAnAMqUtCMCp/YHC5xj6LfnQWLx5SuA8bTDR91biLkI2rcU6ZwQ9W | |
JevVIjrkzD8mRA5RKju0iiUmnAtD6ppB/qksIM27Tne9U8lEL7vQeAuC8RPqFD2F | |
dYry1m+NlFp7yPj7KCNW+w1Nz5CV+BO8ibfl21J/O06PhNiID5gIy7nBYe/g+Lg4 | |
XmdQfsf4RwItbl+R5xZsUyKbrELtgghRE7xZq8/dU0x8QePBZ4zB6uM40omuH5I8 | |
FZVorwdZ/KTJ1rh9UopipOGRBnilFSgt0oL1N5KHLs4DMht33NTW0KUFFA/XT7Ef | |
5uDXx5fmRjRt2VRxF1bnEw/iCC1gSaCc6IMaDITe+lbqPv6mpOJ5BOsCAwEAAQ== | |
-----END RSA PUBLIC KEY----- |
This file contains hidden or 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
const fs = require('fs'); | |
const encrypt = require('./encrypt'); | |
const decrypt = require('./decrypt'); | |
const publicKey = fs.readFileSync(__dirname + '/id_rsa_pub.pem', 'utf8'); | |
// Stores a Buffer object | |
const encryptedMessage = encrypt.encryptWithPublicKey(publicKey, 'Super secret message'); | |
// If you try and "crack the code", you will just get gibberish | |
console.log(encryptedMessage.toString()); | |
const privateKey = fs.readFileSync(__dirname + '/id_rsa_priv.pem', 'utf8'); | |
const decryptedMessage = decrypt.decryptWithPrivateKey(privateKey, encryptedMessage); | |
// Convert the Buffer to a string and print the message! | |
console.log(decryptedMessage.toString()); |
This file contains hidden or 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
const crypto = require('crypto') | |
const hash = crypto.createHash('sha256') | |
const fs = require('fs') | |
const encrypt = require('./encrypt') | |
const decrypt = require('./decrypt') | |
const data = { | |
firstName: 'George', | |
lastName: 'Davituri', | |
location: 'Tbilisi.Georgia', | |
socialSecurityNumber: 'Are you kidding? Never put sensitive information in digitally signed message \ | |
since type of cryptography does not hide the data' | |
} | |
const dataString = JSON.stringify(data) | |
// Sets the value on the hash object! | |
hash.update(dataString) | |
// Hashed data in Hexidecimal format. | |
const hashedData = hash.digest('hex') | |
const senderPrivateKey = fs.readFileSync(__dirname + '/id_rsa_priv.pem', 'utf-8') | |
const signedMessage = encrypt.encryptWithPrivateKey(senderPrivateKey, hashedData) | |
const packageOfDataToSend = { | |
algorithm: 'sha256', | |
originalData: data, | |
signedAndEncryptedData: signedMessage | |
} | |
module.exports.packageOfDataToSend = packageOfDataToSend |
This file contains hidden or 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
const crypto = require('crypto') | |
const fs = require('fs') | |
const decrypt = require('./decrypt') | |
const receivedData = require('./signMessage').packageOfDataToSend | |
const hash = crypto.createHash(receivedData.algorithm) | |
const publicKey = fs.readFileSync(__dirname + '/id_rsa_pub.pem', 'utf-8') | |
// ReceivedData: Hash value | |
const decryptedMessage = decrypt.decryptWithPublicKey(publicKey, receivedData.signedAndEncryptedData) | |
const decryptedMessageHex = decryptedMessage.toString() | |
const hashOfOriginal = hash.update(JSON.stringify(receivedData.originalData)) | |
const hashOfOriginalHex = hash.digest('hex') | |
if (hashOfOriginalHex === decryptedMessageHex) { | |
console.log('Success! The data has not been tampered with and the sender is valid') | |
} else { | |
console.log('Uh oh.. Someone is trying to manipulate the data or someone else is sending this!') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment