new Encrypt and decrypt String data with PGP keys example

This commit is contained in:
Viet Nguyen 2018-05-09 21:48:00 -04:00
parent 6efcce1069
commit 65be3ed77a

View File

@ -135,36 +135,49 @@ openpgp.decrypt(options).then(function(plaintext) {
#### Encrypt and decrypt *String* data with PGP keys
```js
var options, encrypted;
const openpgp = require('openpgp') // use as CommonJS, AMD, ES6 module or via window.openpgp
openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path
var pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
var privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----'; //encrypted private key
var passphrase = 'secret passphrase'; //what the privKey is encrypted with
// put keys in backtick (``) to avoid errors caused by spaces or tabs
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----` //encrypted private key
const passphrase = `yourPassphrase` //what the privKey is encrypted with
var privKeyObj = openpgp.key.readArmored(privkey).keys[0];
await privKeyObj.decrypt(passphrase);
const encryptDecryptFunction = async() => {
const privKeyObj = openpgp.key.readArmored(privkey).keys[0]
await privKeyObj.decrypt(passphrase)
const options = {
data: 'Hello, World!', // input as String (or Uint8Array)
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
privateKeys: [privKeyObj] // for signing (optional)
}
openpgp.encrypt(options).then(ciphertext => {
encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
return encrypted
})
.then(encrypted => {
const options = {
message: openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}
openpgp.decrypt(options).then(plaintext => {
console.log(plaintext.data)
return plaintext.data // 'Hello, World!'
})
})
}
options = {
data: 'Hello, World!', // input as String (or Uint8Array)
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
privateKeys: [privKeyObj] // for signing (optional)
};
openpgp.encrypt(options).then(function(ciphertext) {
encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
});
```
```js
options = {
message: openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
};
openpgp.decrypt(options).then(function(plaintext) {
return plaintext.data; // 'Hello, World!'
});
encryptDecryptFunction()
```
#### Encrypt with compression