-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.js
39 lines (29 loc) · 1.14 KB
/
rsa.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import crypto from 'crypto';
import { stdin, stdout } from "process";
import readline from 'readline/promises';
import { TextEncoder } from "util";
async function main() {
//Generate pair of keys
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
});
console.log('public', publicKey.export({
type: "pkcs1",
format: "pem",
}));
console.log('private', privateKey.export({
type: "pkcs1",
format: "pem",
}));
const user = readline.createInterface({ input: stdin, output: stdout });
const text = await user.question("Enter the text to encrypt: ")
const textBuffer = new TextEncoder().encode(text);
const encryptedBuffer = crypto.publicEncrypt(publicKey, textBuffer);
const encryptedMessage = encryptedBuffer.toString('base64');
console.log('encrypted message', encryptedMessage);
const decryptedBuffer = crypto.privateDecrypt(privateKey, Buffer.from(encryptedMessage, 'base64'));
console.log('The decripted message is', decryptedBuffer.toString());
}
await main();
process.exit(0);