Last active
March 24, 2023 07:34
-
-
Save korrio/8d36d2eb9c87cc09467d5677018d3046 to your computer and use it in GitHub Desktop.
GPG Encryption
This file contains 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 openpgp = require('openpgp'); | |
// Generate a new key pair (public key and private key) | |
const { privateKeyArmored, publicKeyArmored } = await openpgp.generateKey({ | |
type: 'rsa', | |
userIDs: [{ name: 'John Doe', email: 'johndoe@example.com' }], | |
passphrase: 'mySecretPassphrase', | |
}); | |
// Alice encrypts a file for Bob using his public key | |
const fileContents = fs.readFileSync('/path/to/myfile.csv'); | |
const encryptedFile = await openpgp.encrypt({ | |
message: openpgp.message.fromBinary(fileContents), | |
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys, | |
}); | |
// Save the encrypted file to disk | |
fs.writeFileSync('/path/to/myfile.gpg', encryptedFile.data); | |
// Bob decrypts the encrypted file using his private key | |
const encryptedFileContents = fs.readFileSync('/path/to/myfile.gpg'); | |
const decryptedFile = await openpgp.decrypt({ | |
message: await openpgp.message.readArmored(encryptedFileContents), | |
privateKeys: (await openpgp.key.readArmored(privateKeyArmored)).keys, | |
passphrase: 'mySecretPassphrase', | |
}); | |
// Save the decrypted file to disk | |
fs.writeFileSync('/path/to/decrypted_file.csv', decryptedFile.data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment