Last active
April 7, 2024 20:42
-
-
Save immclarenbaby/2d30fe3f194a12e4bb59178132e792f7 to your computer and use it in GitHub Desktop.
feat: completly changed it because it didnt work
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 generateECDHKeyPair() { | |
| const ecdh = crypto.createECDH("prime256v1"); | |
| const publicKey = ecdh.generateKeys(); | |
| return { ecdh, publicKey }; | |
| } | |
| function encryptWithAES(key, iv, plaintext) { | |
| const cipher = crypto.createCipheriv("aes-256-cbc", key, iv); | |
| let encrypted = cipher.update(plaintext, "utf8", "base64"); | |
| encrypted += cipher.final("base64"); | |
| return encrypted; | |
| } | |
| function decryptWithAES(key, iv, ciphertext) { | |
| const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv); | |
| let decrypted = decipher.update(ciphertext, "base64", "utf8"); | |
| decrypted += decipher.final("utf8"); | |
| return decrypted; | |
| } | |
| const alice = generateECDHKeyPair(); | |
| const bob = generateECDHKeyPair(); | |
| const aliceSharedSecret = alice.ecdh.computeSecret(bob.publicKey); | |
| const bobSharedSecret = bob.ecdh.computeSecret(alice.publicKey); | |
| const aesKey = aliceSharedSecret.slice(0, 32); | |
| const iv = crypto.randomBytes(16); | |
| const message = "Hello, World!"; | |
| const encryptedMessage = encryptWithAES(aesKey, iv, message); | |
| console.log("Encrypted message:", encryptedMessage); | |
| const decryptedMessage = decryptWithAES(aesKey, iv, encryptedMessage); | |
| console.log("Decrypted message:", decryptedMessage); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment