gpt4 book ai didi

javascript - 获取加密的缓冲区数据以用作客户端解密的 ArrayBuffer

转载 作者:行者123 更新时间:2023-12-05 04:40:28 26 4
gpt4 key购买 nike

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

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

/**********************
** 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.

最佳答案

您遗漏了很多有助于社区了解您如何加密图像、如何检索图像以及如何解密图像的细节。下面是获取图像、加密、解密并在浏览器中显示图像的完整示例。这在 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)
})()

关于javascript - 获取加密的缓冲区数据以用作客户端解密的 ArrayBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70263740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com