sz
sz
DDeno
Created by sz on 1/3/2024 in #help
`RangeError: Offset is outside the bounds of the DataView` when trying to encrypt with AES-CRT
Hi, I'm trying to encrypt some files with aes128-crt, but I always get the error below. (Even if I just pass some random short string converted into an uint8array instead of the file's content) stacktrace:
RangeError: Offset is outside the bounds of the DataView
at DataView.getUint32 (<anonymous>)
at Ctr.encrypt (https://deno.land/x/crypto@v0.10.1/src/block-modes/ctr.ts:36:25)
at Builder.EncodeFile (file:///C:/redacted/builders/Shared.ts:42:34)
RangeError: Offset is outside the bounds of the DataView
at DataView.getUint32 (<anonymous>)
at Ctr.encrypt (https://deno.land/x/crypto@v0.10.1/src/block-modes/ctr.ts:36:25)
at Builder.EncodeFile (file:///C:/redacted/builders/Shared.ts:42:34)
code snippet:
import crypto from "node:crypto";
import { Aes } from "https://deno.land/x/crypto/aes.ts";
import { Ctr } from "https://deno.land/x/crypto/block-modes.ts";

// ...
class Builder {
// ...

protected EncodeFile(
FileSource: string,
FileTarget: string,
UsedKey?: Uint8Array,
UsedIVKey?: Uint8Array,
): { Key: string; Iv: string } {
const Key = UsedKey ?? new Uint8Array(16),
IVKey = UsedIVKey ?? new Uint8Array(16);

const Encoder = new TextEncoder();
const FileContent = Encoder.encode(fs.readFileSync(FileSource, "utf-8"));

// Create an AES-128-CTR cipher
const Cipher = new Ctr(Aes, Key, IVKey);

// Encrypt the data
const EncryptedData = Cipher.encrypt(FileContent);

WriteFile(FileTarget, EncryptedData, "utf-8");
return { Key: this.UInt8ArrayToHex(Key), Iv: this.UInt8ArrayToHex(IVKey) };
}
}
import crypto from "node:crypto";
import { Aes } from "https://deno.land/x/crypto/aes.ts";
import { Ctr } from "https://deno.land/x/crypto/block-modes.ts";

// ...
class Builder {
// ...

protected EncodeFile(
FileSource: string,
FileTarget: string,
UsedKey?: Uint8Array,
UsedIVKey?: Uint8Array,
): { Key: string; Iv: string } {
const Key = UsedKey ?? new Uint8Array(16),
IVKey = UsedIVKey ?? new Uint8Array(16);

const Encoder = new TextEncoder();
const FileContent = Encoder.encode(fs.readFileSync(FileSource, "utf-8"));

// Create an AES-128-CTR cipher
const Cipher = new Ctr(Aes, Key, IVKey);

// Encrypt the data
const EncryptedData = Cipher.encrypt(FileContent);

WriteFile(FileTarget, EncryptedData, "utf-8");
return { Key: this.UInt8ArrayToHex(Key), Iv: this.UInt8ArrayToHex(IVKey) };
}
}
6 replies