【问题标题】:AEAD AES-256-GCM in Node.jsNode.js 中的 AEAD AES-256-GCM
【发布时间】:2021-04-12 20:24:17
【问题描述】:

如何用 Node.js 解密其他语言加密的数据? 为了描述这个问题,我们用 Python 写了一些代码:

plain_text = 'some data'
nonce = 'some nonce string'
associated_data = 'some associated data'
key = '--- 32 bytes secret key here ---'

在 python 中加密

from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import base64

aesgcm = AESGCM(key.encode())
encrypted = aesgcm.encrypt(nonce.encode(), plain_text.encode(), associated_data.encode())
print(aesgcm.decrypt(nonce.encode(), encrypted, associated_data.encode()))

ciphertext = base64.b64encode(encrypted)

【问题讨论】:

    标签: node.js aes-gcm


    【解决方案1】:

    在 Node.js 中解密,我们不需要额外的依赖。

    crypto模块实现了GCM算法,但概念不同。

        const crypto = require('crypto')
    
        encrypted = Buffer.from(ciphertext, 'base64')
    
        let decipher = crypto.createDecipheriv('AES-256-GCM', key, nonce)
    
        decipher.setAuthTag(encrypted.slice(-16))
        decipher.setAAD(Buffer.from(associated_data))
    
        let output = Buffer.concat([
            decipher.update(encrypted.slice(0, -16)),
            decipher.final()
        ])
    
        console.log(output.toString())
    

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 2021-05-12
      • 2021-08-10
      • 2021-09-23
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      相关资源
      最近更新 更多