Deno crypto cannot decrypt encrypted files by node:crypto

admin

I encrypt a file in nodejs like below, /path/to/node-proejct/crypto.js const Crypto = require(“node:crypto”); function encodeText(text) { return new TextEncoder().encode(text); } function encrypt(buffer, passphrase) { const key = encodeText(passphrase); const iv = Crypto.randomBytes(16); const cipher = Crypto.createCipheriv(“aes-128-gcm”, key, iv); const result = Buffer.concat([iv, cipher.update(buffer), cipher.final()]); console.log(“Encrypted File Length: “, result.length); return result; } function decrypt(buffer, passphrase)…

imageI encrypt a file in nodejs like below,

/path/to/node-proejct/crypto.js

const Crypto = require(“node:crypto”); function encodeText(text) { return new TextEncoder().encode(text); } function encrypt(buffer, passphrase) { const key = encodeText(passphrase); const iv = Crypto.randomBytes(16); const cipher = Crypto.createCipheriv(“aes-128-gcm”, key, iv); const result = Buffer.concat([iv, cipher.update(buffer), cipher.final()]); console.log(“Encrypted File Length: “, result.length); return result; } function decrypt(buffer, passphrase) { const key = encodeText(passphrase); const iv = buffer.slice(0, 16); const decipher = Crypto.createDecipheriv(“aes-128-gcm”, key, iv); return Buffer.concat([decipher.update(buffer.slice(16))]); } module.exports = { encrypt, decrypt, };

/path/to/node-proejct/crypto.test.js

const { encrypt, decrypt } = require(“./crypto”); const fs = require(“node:fs”); const PASSWORD = “some-passwrod”.padEnd(16, “0”); const ORIGINAL_FILE_PATH = “/some/path/to/test.png”; const ENCRYPTED_FILE_PATH = “/some/path/to/test.node.enc.png”; const DECRYPTED_FILE_PATH = “/some/path/to/test.node.dec.png”; test(“Encrypt and decrypt file”, function() { const original = fs.readFileSync(ORIGINAL_FILE_PATH); const encrypted = encrypt(original, PASSWORD); fs.writeFileSync(ENCRYPTED_FILE_PATH, encrypted); const decrypted = decrypt(encrypted, PASSWORD); fs.writeFileSync(DECRYPTED_FILE_PATH, decrypted); expect(decrypted).toEqual(original); // PASS });

And, I’ll decrypt encrypted file in Deno.

/path/to/deno-project/crypto.ts

import { crypto } from “$std/crypto/mod.ts”; // https://deno.land/

[[email protected]]/crypto/mod.ts function encodeText(text: string): Uint8Array { return new TextEncoder().encode(text); } export async function encrypt(buffer: Uint8Array, passphrase: string): Promise { const key = encodeText(passphrase); const iv = crypto.getRandomValues(new Uint8Array(16)); const cryptoKey = await crypto.subtle.importKey(“raw”, key, { name: “AES-GCM”, length: 128 }, true, [“encrypt”]); const encrypted = await crypto.subtle.encrypt({ name: “AES-GCM”, length: 128, iv }, cryptoKey, buffer); return new Uint8Array([…iv, …new Uint8Array(encrypted)]); } export async function decrypt(buffer: Uint8Array, passphrase: string): Promise { const key = encodeText(passphrase); const iv = buffer.slice(0.16); const cryptoKey = await crypto.subtle.importKey(“raw”, key, { name: “AES-GCM”, length: 128 }, true, [“decrypt”]); const decrypted = await crypto.subtle.decrypt({ name: “AES-GCM”, length: 128, iv }, cryptoKey, buffer.slice(16)); return new Uint8Array(decrypted); }

/path/to/deno-project/crypto.test.ts

import * as Assertions from “$std/assert/mod.ts”; // https://deno.land/

[[email protected]]/assert/mod.ts import { encrypt, decrypt } from “./crypto.ts”; const PASSWORD = “some-passwrod”.padEnd(16, “0”); const ORIGINAL_FILE_PATH = “/some/path/to/test.png”; const ENCRYPTED_FILE_PATH = “/some/path/to/test.deno.enc.png”; const DECRYPTED_FILE_PATH = “/some/path/to/test.deno.dec.png”; const NODE_ENCRYPTED_FILE_PATH = “/some/path/to/test.node.enc.png”; // encrypted in nodejs Deno.test(“Deno Encryption Test”, async function() { const original = await Deno.readFile(ORIIGNAL_FILE_PATH); const encrypted = await encrypt(original, PASSWORD); await Deno.writeFile(ENCRYPTED_FILE, encrypted); const decrypted = await decrypt(encrypted, PASSWORD); await Deno.writeFile(DECRYPTED_FILE, decrypted); Assertions.assertEquals(original, decrypted); // PASS }); Deno.test(“Node 2 Deno Encryption Test”, async function() { const original = await Deno.readFile(ORIGINAL_FILE_PATH); const encrypted = await Deno.readFile(NODE_ENCRYPTED_FILE_PATH); const decrypted = await decrypt(encrypted, PASSWORD); // OperationError: Decryption failed Assertions.assertEquals(original, decrypted); });

It seems not to be same encryption process(AES-GCM 128bits…) Let me know what I did wrong…

I tried compare thoes file’s size (they are same.)

And making iv same as key, then compare encrypted bytes in node and deno.they are not same.(In same key and iv, the result of

cipher.update and the result of

crypto.subtle.encrypt are different.).

Leave a Reply

Next Post

Discover the Best Cryptocurrencies for Online Gaming in 2023

We’ve seen the traditional options like poker and blackjack, but crash gambling has become a top pick as well at a number of crypto casinos on a [list for November 2023](https://www.techopedia.com/cryptocurrency/best-crash-gambling-sites).But even as consumers enjoy all of these games, some cryptos have proven to be their go-tos to play them.1.Bitcoin To no one’s surprise, Bitcoin…
Discover the Best Cryptocurrencies for Online Gaming in 2023

Subscribe US Now