Skip to content

Instantly share code, notes, and snippets.

@saltukalakus
Created December 3, 2019 14:15
Show Gist options
  • Save saltukalakus/08efe814847f9a049ce4d07140817c78 to your computer and use it in GitHub Desktop.
Save saltukalakus/08efe814847f9a049ce4d07140817c78 to your computer and use it in GitHub Desktop.
Encrypt & Decrypt with existing keys
const crypto = require("crypto");
let publicKey = `-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAsoykgfqb0VqzTIDDShJ7
Pa7sg6hV4kmBBof7LLPyvmMdNB/2zNZ3mfEr0Dw+qNinmyVXQ5b/DjwbjHvYQpn9
foiFFDS6fP8ErGkICEN/68JvI/MSEJsbfDxKM/rQTwXVfD+0wxEIRblzW1LMYraO
NuCPWgg0UZRmis+8Qcr0vZdf61lyfyaTx0C0bwS/wsJO0yFrIWGMsatYZktbbpPG
IOQwqT2VIWfvcKjMcV8OXHNMoVjG6ni3kqXjSPYKaT1qWl4L73/dWKS0IlJJfdYw
0hpzDsmgwnsug9KBtfzbreNenVPbMfCUXone8VQZ/0+Eb8iO7JwaPuFld+0Czcsu
kG6pmBUwaPk9ISlRNWUkyhUP9q4rUmAzAZgcYPNVWQVZwG/u7X2Jbf7jdsYvvCX3
qaqhXyWXrioyrLbjYRhkBdy0b1cfYgIi1cBW2CM5oVbGin/2gX5N46y21VGdppFd
Pcumf38VipG5dkRNlPAMdW+EAtkkXyIHprItRgcmCYRvPwollOGD0pzAuvsCwCjv
5OiIrS4iavAqDtO0h/nZoyPReCOz0EkGJ3eQmbL/ZdVvzM0jPKbSOXUYcUSt5pcv
/afqkGp1Avlw350iQHbjvhtWLHmkrqI73w0mg+qCGwCOh3Yh3lZOfd0r7A3wNkr5
/1gOGFusCAyTMEVQc5PG7t0CAwEAAQ==
-----END PUBLIC KEY-----`;
let privateKey = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIJrTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIRxz6IS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uDxf4C2vjRak+5dibZu8gQ2Lw
uvrdNz/y9tFF/Wb92F2cAuVt8x4t/wQ56DqWQ0WIqcot
-----END ENCRYPTED PRIVATE KEY-----`;
const user = "STRING_TO_ENCRYPT";
const userTimeString = Buffer.from(user, "utf8");
const encryptedString = crypto.publicEncrypt(publicKey, userTimeString);
const decryptedString = crypto.privateDecrypt(
{
key: privateKey.toString(),
passphrase: "temp"
},
encryptedString
);
console.log(Buffer.from(decryptedString).toString("utf8"));
console.log(decryptedString.toString("utf8"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment