小编Fen*_*aji的帖子

AES GCM 在 nodejs 中加密并在浏览器中解密?

我正在尝试在 nodejs 中加密一段字符串,并且需要在前端 javascript 中对其进行解密。在 nodejs 中,我使用了加密库,而在前端使用了网络加密。在前端解密时遇到一些错误。

节点

const crypto = require('crypto');
const iv = crypto.randomBytes(12);
const algorithm = 'aes-256-gcm';
let password = 'passwordpasswordpasswordpassword';
let text = 'Hello World!';
let cipher = crypto.createCipheriv(algorithm, password, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
var tag = cipher.getAuthTag();
let cipherObj = {
    content: encrypted,
    tag: tag,
    iv: iv
}
Run Code Online (Sandbox Code Playgroud)

前端

let cipherObj;  //GET FROM BE
let aesKey = await crypto.subtle.importKey(
  "raw",
  Buffer.from('passwordpasswordpasswordpassword'), //password
  "AES-GCM",
  true,
  ["decrypt"]
);

let decrypted = …
Run Code Online (Sandbox Code Playgroud)

javascript encryption cryptography aes-gcm webcrypto-api

5
推荐指数
1
解决办法
1288
查看次数