Skip to content

Instantly share code, notes, and snippets.

@CHAOWEICHIU
Last active February 28, 2019 01:12
Show Gist options
  • Save CHAOWEICHIU/5265c286602dfada9a0f0e49842c0fa3 to your computer and use it in GitHub Desktop.
Save CHAOWEICHIU/5265c286602dfada9a0f0e49842c0fa3 to your computer and use it in GitHub Desktop.
encrypt and decrypt message with public key and private key
/*
Requirement
--------------
node: v8.10.0
npm: v5.6.0
--------------
npm installation
-----------------------
npm install eth-crypto
-----------------------
*/
const EthCrypto = require('eth-crypto')
const keyPrivate = '0x61ce8b95ca5fd6f55cd97ac60817777bdf64f1670e903758ce53efc32c3dffeb'
const keyPublic = 'fff49b58b83104ff16875452852466a46c7169ba4e368d11830c9170624e0a9509080a05a38c18841718ea4fc13483ac467d3e2d728d41ff16b73b9c943734f8'
const encryptStringWithPublicKey = async ({ message, publicKey }) => {
const dataObject = await EthCrypto.encryptWithPublicKey(publicKey, message)
const compressedString = await EthCrypto.cipher.stringify(dataObject)
return compressedString
}
const decryptStringWithPrivateKey = async ({ encryptedString, privateKey }) => {
const decompressedString = await EthCrypto.cipher.parse(encryptedString)
const originalMessage = await EthCrypto.decryptWithPrivateKey(privateKey, decompressedString)
return originalMessage
}
const startHacking = async () => {
const msg = 'DEXON\'s latency is 1s'
const encryptedString = await encryptStringWithPublicKey({
publicKey: keyPublic,
message: msg,
})
// encryptedString: 2c628541e2c6a7f71584f1bfd55e10a90277c30bdd83f21d3a99696e1f499ca6683da0d901e95d49e6649fed89ce7026c67b7eb9d8ed0aed63be73bbed01b5dfa9170fbde22c053cc23029b924448babed092e06576f2c2dc70ad72a114a2f11b834847aa5721472decdf5a8410ea1bb55
const decryptedString = await decryptStringWithPrivateKey({
privateKey: keyPrivate,
encryptedString: encryptedString,
})
if (decryptedString === msg) {
console.log('Really?')
}
}
startHacking()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment