D
Deno

help

`RangeError: Offset is outside the bounds of the DataView` when trying to encrypt with AES-CRT

Sszkiddaj1/3/2024
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) };
}
}
DAHDeno AI Helper1/3/2024
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.
Sszkiddaj1/3/2024
unrelated, the encryption is still errors out
DAHDeno AI Helper1/3/2024
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.
Ppyrote1/4/2024
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.
Sszkiddaj1/5/2024
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

Looking for more? Join the community!

Recommended Posts
Any way for VS Code to auto-complete library subdirectory in import?Is there any way for VS Code to suggest subdirectory in a package (especially for npm package) when get hours & minutes in a specific timezoneMaybe I search the wrong way, but all I'd like to do is to use a specific timezone in my project runConnection refused when creating a local API serverI'm following [Simple API server | Deno Docs](https://docs.deno.com/deploy/tutorials/simple-api "Simdeno deploy queuesis there a special flag or setting required for queues in deno deploy , i ceated a very simple exampWhat is the difference between importing "preact" via esm.sh or npm:...?What is the difference between importing "preact" via esm.sh or npm:...? "preact": "https://esm.sh/pDeployctl, error: Uncaught (in promise) NotFound: Failed to spawn 'start': program not found```powershell > deployctl logs a i Provisioning a new access token... i Authorization URL: https://dis there a way to make the call stack in deno larger?I already think this is a no from what I have gathered but I thought Id askwhere would i go to report a minor bug in module search on deno.land ?hi, i noticed a minor bug in module search on deno.land. it's easily replicable. where would i rep@headlessui/react + FreshI'm trying to use preact/compat to leverage @headlessui/react, but running into In the `"imports"`Is there any good tools to covert npm source code to deno source package?I want to covert some old npm source package to deno source codeEnvironment Variable always returns undefinedI'm just trying to import the values from dotenv to the code, but it always returns undefined. denoUpdated examples for "Roll your own JavaScript runtime" blog posts?Hi all. I'd like to embed Deno in a project and I discovered the excellent series of "Roll your own