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) { 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
/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.).