English 中文(简体)
密码o. 无法加密的微妙,iv isn t ArrayBuffer, 加密数据未经界定
原标题:crypto.subtle unable to decrypt, iv isn t ArrayBuffer, encrypted data is undefined

I m 试图加密档案,将其上载到云层,并用Node.js诉21.6.2在当地加以加密。 不幸的是,我遇到麻烦,难以使加密发挥作用。 能够产生关键和加密的Im,但脱轨在据称是ArrayBuffer的四处失败,但应当如此。 是否有更好的办法确保四舍五入为缓冲,以便我能够适当加密档案? 我已注意到,加密数据物体没有定义,我不敢确定这一问题。 没有人对我做错做什么有什么想法? 我发现的错误如下:

node:internal/webidl:181
  const err = new TypeError(message);
              ^

TypeError: Failed to execute  decrypt  on  SubtleCrypto : 3rd argument is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.
    at codedTypeError (node:internal/webidl:181:15)
    at makeException (node:internal/webidl:190:10)
    at converters.BufferSource (node:internal/crypto/webidl:206:11)
    at SubtleCrypto.decrypt (node:internal/crypto/webcrypto:961:28)
    at decrypt (/Users/me/code/encryptiontest/aes/aes.js:31:43)
    at testEncryptionDecryption (/Users/me/code/encryptiontest/aes/aes.js:52:29) {
  code:  ERR_INVALID_ARG_TYPE 
}

我的守则是:

const { subtle } = globalThis.crypto;

async function generateAesKey(length = 256) {
    const key = await subtle.generateKey({
        name:  AES-CBC ,
        length,
    }, true, [ encrypt ,  decrypt ]);

    return key;
}

async function aesEncrypt(plaintext) {
    const ec = new TextEncoder();
    const key = await generateAesKey();
    const iv = crypto.getRandomValues(new Uint8Array(16));

    const ciphertext = await crypto.subtle.encrypt({
        name:  AES-CBC ,
        iv,
    }, key, ec.encode(plaintext));

    return {
        key,
        iv,
        ciphertext,
    };
}

async function decrypt(ciphertext, key, iv) {
    const dec = new TextDecoder();
    const plaintext = await crypto.subtle.decrypt({
        name:  AES-CBC ,
        iv,
    }, key, ciphertext);

    return dec.decode(plaintext);
}

// takes Uint8Array and returns ArrayBuffer
function typedArrayToBuffer(array) {
    return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset);
}


async function testEncryptionDecryption() {
    const data = "Hello, world!";
    console.log("ENCRYPTING DATA");
    const { key, iv, encrypted } = await aesEncrypt(data);
    console.log(iv);
    console.log("DECRYPTING DATA");
    console.log(typeof iv.buffer);
    const decrypted = await decrypt(encrypted, key, typedArrayToBuffer(iv));
    console.log("Original:", data);
    console.log("Decrypted:", decrypted);
}

testEncryptionDecryption();

我利用四、四、四、四、四、四、四、五。 加密中的斜体,但我对四分五裂的错误不是ArrayBuffer、Buffer、PedArray或数据观察。

问题回答

您的问题不是四(这是第3次对your<>>>>/em>的争论,而是——正如错误信息所言——第3次辩论to-SubtleCrypto.decode>>>>m>的争论,而应是un Defin

这是因为您的行文。

const { key, iv, encrypted } = await aesEncrypt(data);

<代码>aesEncode> 回归物体,其中包含key iv ciphertext,但并非en加密<> 代码>,从而将这一编号key iv<> >发送回标(正确)的成员,但并未将 加密<>> > > /code> 发送至任何内容,并抛弃了成员代码。 修改此法和以下法规,以提取和使用<条码>西部分<>条/代码>,并使用简便的<条码>。





相关问题
Extend Contacts application on Android to provide encryption

I want to encrypt individual contacts stored by the Contacts application on Android based on user s preference. So, I am thinking I ll have to hook/extend the Contacts application before the it stores ...

Make md5 strong

Im making a website that will intergrate with game that only support md5 hashing metod (atm). Which ofc is not especially safe anymore. But how could i make it stronger? Should I just generate long ...

How to Pack/Encrypt/Unpack/Decrypt a bunch of files in Java?

I m essentially trying to do the following on a Java/JSP-driven web site: User supplies a password Password is used to build a strongly-encrypted archive file (zip, or anything else) containing a ...

Thread & Queue vs Serial performance

I though it ll be interesting to look at threads and queues, so I ve written 2 scripts, one will break a file up and encrypt each chunk in a thread, the other will do it serially. I m still very new ...

Convert PHP encryption code to C#

I m trying to convert this piece of code from PHP to C#. It s part of a Captive Portal. Could somebody explain what it does? $hexchal = pack ("H32", $challenge); if ($uamsecret) { $newchal = ...

Encryption: how to have 1 iv despite multiple fields

I ve been stuck trying to arrive at a best solution for this for a while. I know that an initialization vector has to be unique for each item being encrypted. So if I m encrypting an address and I ...