szkiddaj
szkiddaj7mo ago

`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) };
}
}
5 Replies
Deno AI Helper
Deno AI Helper7mo ago
I wasn't able to find the answer to your RangeError: Offset is outside the bounds of the DataView issue in the official Deno documentation. However, looking at your code snippet, there is an obvious issue that I can point out. You are importing crypto from node:crypto, which is a Node.js module, and this is not compatible with Deno. Deno has its own standard library and does not support Node.js modules out of the box. Additionally, you are using fs.readFileSync which is also a Node.js API. In Deno, you would typically use Deno.readTextFileSync or Deno.readFile for reading files. Here's a corrected version of the file reading part of your code snippet that is more aligned with Deno's APIs:
// ...
const FileContent = Deno.readTextFileSync(FileSource);
// ...
const FileContent = Deno.readTextFileSync(FileSource);
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. If you continue to experience issues, I recommend checking the Deno documentation for the correct usage of the crypto APIs and file system operations, or seeking help from the community.
szkiddaj
szkiddaj7mo ago
unrelated, the encryption is still errors out
Deno AI Helper
Deno AI Helper7mo ago
I apologize for the confusion earlier. Since the issue is with the encryption process and not with the file reading, let's focus on the encryption part of your code. The error message you're encountering, RangeError: Offset is outside the bounds of the DataView, suggests that there is an issue with how the DataView is being accessed within the Ctr class from the crypto module you're using. This could be due to an incorrect buffer size, an incorrect offset, or an issue within the library itself. Given that the error is occurring within the Ctr.encrypt method, it's possible that the issue is not with your code but with the library or the way the library is being used. Since I don't have access to the official Deno documentation beyond my knowledge cutoff date, I can't provide a verified solution. However, I would recommend the following steps to troubleshoot the issue: 1. Ensure that the Key and IVKey are of the correct length expected by the AES-128-CTR algorithm, which is 16 bytes for both. 2. Verify that the Ctr class is being used correctly according to the documentation or examples provided by the crypto module you're using. 3. Check if there are any open issues or updates in the crypto module repository that might address this problem. 4. Consider creating a minimal reproducible example and reaching out to the maintainers of the crypto module for assistance. Remember to always refer to the official documentation or resources provided by the library for the most accurate and up-to-date information.
pyrote
pyrote7mo ago
I'm not sure if that modules CTR implementation is supposed to be very low level or isn't finished yet. Its expecting the input data to be in blocks of 16 bytes. If you data isn't a multiple of 16 bytes you get that error. Which shouldn't be a requirement for CTR. You may want to look at the standard JavaScript crypto.subtle API. Its built into Deno. https://deno.land/api@v1.39.1?s=SubtleCrypto The Mozilla docs provide some good examples of how to use it. https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt#aes-ctr
Deno
SubtleCrypto | Runtime APIs | Deno
This Web Crypto API interface provides a number of low-level cryptographic functions. It is accessed via the Crypto.subtle properties available in a window context (via Window.crypto).
MDN Web Docs
SubtleCrypto: encrypt() method - Web APIs | MDN
The encrypt() method of the SubtleCrypto interface encrypts data.
szkiddaj
szkiddaj7mo ago
I've tried crypto.subtle, but I couldn't get it work with cryptopp for some reason. (I wanted to encrypt with Deno, decrypt with cryptopp. Now I just spawnSync a process to encrypt files with cryptopp cli itself.) But thanks:D
More Posts