【问题标题】:Openpgp Encrypt with PGPOpenpgp 使用 PGP 加密
【发布时间】:2022-04-20 22:48:39
【问题描述】:

我正在尝试使用 PGP 加密消息,但收到一条错误消息:

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

这就是我的代码的样子:

    const plainData = random.uuid()
    const publicKeyArmored = fs.readFileSync('/path/any.asc').toString();
    const publicKey = await openpgp.readKey( { armoredKey: publicKeyArmored } )
    console.log(publicKey)

当我打印publicKeyArmored 时,一切正常。一旦到达前面显示的console.log,就会出现错误

【问题讨论】:

    标签: node.js openpgp.js


    【解决方案1】:

    这就是我最终完成这项工作的方式,我希望它可以帮助某人:

    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)
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 2013-09-22
      • 2022-06-24
      • 2020-01-04
      • 1970-01-01
      相关资源
      最近更新 更多