`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:
code snippet:
5 Replies
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:
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.unrelated, the encryption is still errors out
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.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.
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