Reputation: 1609
I am trying to encrypt a message using PGP but I am getting an error that states:
ReferenceError: TextDecoder is not defined
at Object.decodeUTF8 (node_modules/openpgp/src/util.js:225:21)
at Do.read (node_modules/openpgp/src/packet/userid.js:78:25)
at callback (node_modules/openpgp/src/packet/packetlist.js:82:28)
at Xs (node_modules/openpgp/src/packet/packet.js:282:13)
at node_modules/openpgp/src/packet/packetlist.js:96:11
This is how my code looks like:
const plainData = random.uuid()
const publicKeyArmored = fs.readFileSync('/path/any.asc').toString();
const publicKey = await openpgp.readKey( { armoredKey: publicKeyArmored } )
console.log(publicKey)
When I print the publicKeyArmored
, all is OK. As soon as it reaches the console.log
shown before, the error appears
Upvotes: 1
Views: 1117
Reputation: 1609
This is how I ended up making this work, I hope it helps someone:
import * as openpgp from 'openpgp'
const plainText = 'Any text you wanna encrypt'
const textEncoding = await import('text-encoding-utf-8')
global.TextEncoder = textEncoding.TextEncoder
global.TextDecoder = textEncoding.TextDecoder
const publicKeyArmored = fs.readFileSync('/path/any.asc').toString();
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored })
const encrypted = await openpgp.encrypt({
message: openpgp.Message.fromText(plainText),
publicKeys: [publicKey]
})
console.log(encrypted)
Upvotes: 2