【问题标题】:Fetching Encrypted Buffer Data to use as ArrayBuffer for client-side decryption获取加密的缓冲区数据以用作 ArrayBuffer 以进行客户端解密
【发布时间】:2022-01-12 17:44:42
【问题描述】:

我正在尝试从 Arweave 获取加密的原始缓冲区数据 (AES-256),传递给解密函数并使用它来显示图像。我正在尝试在前端(在我的 React 应用程序中)获取和解密 ArrayBuffer。

首先,我在 NodeJS 中加密 Buffer 数据并存储文件。这是它的代码:

/**********************
 **  Runs in NodeJS  **
 **********************/

const encrypt = (dataBuffer, key) => {
    // Create an initialization vector
    const iv = crypto.randomBytes(IV_LENGTH);
    // Create cipherKey
    const cipherKey = Buffer.from(key);
    // Create cipher
    const cipher = crypto.createCipheriv(ALGORITHM, cipherKey, iv);

    const encryptedBuffer = Buffer.concat([
        cipher.update(dataBuffer),
        cipher.final(),
    ]);
    const authTag = cipher.getAuthTag();
    let bufferLength = Buffer.alloc(1);
    bufferLength.writeUInt8(iv.length, 0);

    return Buffer.concat([bufferLength, iv, authTag, encryptedBuffer]);
};

const encryptedData = encrypt(data, key)

fs.writeFile("encrypted_data.enc", encryptedData, (err) => {
    if(err){
        return console.log(err)
    }
});

接下来,我尝试在前端获取和解密。到目前为止,我从响应中返回了一个 ArrayBuffer。我尝试将此 ArrayBuffer 传递给解密函数。代码如下:

/***********************
 **  Runs in React  **
 ***********************/
 import crypto from "crypto"

const getData = async (key) => {
  const result = await (await fetch('https://arweave.net/u_RwmA8gP0DIEeTBo3pOQTJ20LH2UEtT6LWjpLidOx0/encrypted_data.enc')).arrayBuffer()
  const decryptedBuffer = decrypt(result, key)
  console.log(decryptedBuffer)
}

//  Here is the decrypt function I am passing the ArrayBuffer to:
export const decrypt = (dataBuffer, key) => {
    // Create cipherKey
    const cipherKey = Buffer.from(key);
    // Get iv and its size
    const ivSize = dataBuffer.readUInt8(0);
    const iv = dataBuffer.slice(1, ivSize + 1);
    // Get authTag - is default 16 bytes in AES-GCM
    const authTag = dataBuffer.slice(ivSize + 1, ivSize + 17);

    // Create decipher
    const decipher = crypto.createDecipheriv("aes-256-gcm", cipherKey, iv);
    decipher.setAuthTag(authTag);

    return Buffer.concat([
        decipher.update(dataBuffer.slice(ivSize + 17)),
        decipher.final(),
    ]);
};

当我将 ArrayBuffer 数据传递给解密函数时,我收到此错误:

Unhandled Rejection (TypeError): First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.

【问题讨论】:

  • 我建议您剪切并粘贴您遇到的任何错误的确切文本,而不是解释。
  • 您传递的东西的类型是什么,而不是 字符串、缓冲区、ArrayBuffer、数组或类似数组的对象
  • 有什么问题?你永远不会提供错误。我不确定为什么这个问题会引起投票。
  • @wahwahwah 错误在问题的第一个文字块中逐字引用。
  • 提供你所有的代码。不要遗漏事情。识别节点中运行的内容和浏览器中运行的内容。

标签: javascript encryption encryption-symmetric arraybuffer


【解决方案1】:

您忽略了许多有助于社区了解您如何加密图像、如何检索图像以及如何解密图像的细节。这是获取图像、对其进行加密、解密并在浏览器中显示的完整示例。这在 Chrome v96 和 Firefox v95 中运行。

(async () => {
  const encryptionAlgoName = 'AES-GCM'
  const encryptionAlgo = {
      name: encryptionAlgoName,
      iv: window.crypto.getRandomValues(new Uint8Array(12)) // 96-bit
  }
  
  // create a 256-bit AES encryption key
  const encryptionKey = await crypto.subtle.importKey(
    'raw',
    new Uint32Array([1,2,3,4,5,6,7,8]),
    { name: encryptionAlgoName },
    true,
    ["encrypt", "decrypt"],
  )

  // fetch a JPEG image
  const imgBufferOrig = await (await fetch('https://fetch-progress.anthum.com/images/sunrise-baseline.jpg')).arrayBuffer()

  // encrypt the image
  const imgBufferEncrypted = await crypto.subtle.encrypt(
    encryptionAlgo,
    encryptionKey,
    imgBufferOrig
  )

  // decrypt recently-encrypted image
  const imgBufferDecrypted = await crypto.subtle.decrypt(
    encryptionAlgo, 
    encryptionKey,
    imgBufferEncrypted
  )

  // display unencrypted image
  const img = document.createElement('img')
  img.style.maxWidth = '100%'
  img.src = URL.createObjectURL(
    new Blob([ imgBufferDecrypted ])
  )
  document.body.append(img)    
})()

【讨论】:

  • 感谢您的回复。我真的很感谢你的帮助。很抱歉我没有提供完整的细节,我试图不要让它过于冗长,所以我在谨慎方面犯了错误。我现在意识到这是寻求帮助的错误方式,因为在没有上下文的情况下很难帮助解决我的具体错误。我已经运行了您的代码,并且它完全按预期工作。我在原始问题中附加了一些代码,显示了我如何加密和存储图像中的缓冲区数据。我正在写入服务器的文件系统并在前端获取数据以进行解密。
猜你喜欢
  • 2020-03-23
  • 2018-01-13
  • 2015-04-30
  • 2012-07-15
  • 2022-11-10
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多