gpt4 book ai didi

javascript - 跨平台 AES 256 GCM Javascript 和 Elixir

转载 作者:行者123 更新时间:2023-11-29 20:39:48 25 4
gpt4 key购买 nike

我一直在尝试使用带有 GCM 的 AES 256 在 Javascript 中加密并在 Elixir 中解密。我从各处借用了一些示例并得出了以下结论。

Javascript 中的加密

const _crypto = require('crypto');

function encrypt(message, secret) {
// random initialization vector
const iv = _crypto.randomBytes(16);

// extract the auth tag
const cipher = _crypto.createCipheriv('aes-256-gcm', secret, iv);

// encrypt the given text
const encrypted = Buffer.concat([cipher.update(message, 'utf8'), cipher.final()]);

// extract the auth tag
const tag = cipher.getAuthTag();

const encrypted_message = Buffer.concat([iv, tag, encrypted]).toString('base64');
return encrypted_message;
}

const secret = _crypto.randomBytes(32);
encrypt("secret message", secret);

Elixir 中的解密

def decrypt(encrypted_message, secret) do
secret_key = :base64.decode(secret)
ciphertext = :base64.decode(encrypted_message)

<<iv::binary-16, tag::binary-16, ciphertext::binary>> = ciphertext
:crypto.block_decrypt(:aes_gcm, secret_key, iv, {"AES256GCM", ciphertext, tag})
end

# secret would be the secret from javascript encoded in base64
decrypt(encrypted_message, secret)

而我在 Elixir 方面的结果一直是 :error我的感觉是它与编码和解码有关,但我似乎无法找出哪里出了问题。

如果有人能指出我正确的方向,将不胜感激。

谢谢!

更新的工作版本

对于那些打算使用相同语言的人:

Javascript 加密

const _crypto = require('crypto');

function encrypt(message, secret) {
// random initialization vector
const iv = _crypto.randomBytes(16);

// extract the auth tag
const cipher = _crypto.createCipheriv('aes-256-gcm', secret, iv);

// add the following line if you want to include "AES256GCM" on the elixir side
// cipher.setAAD(Buffer.from("AES256GCM", 'utf8'));

// encrypt the given text
const encrypted = Buffer.concat([cipher.update(message, 'utf8'), cipher.final()]);

// extract the auth tag
const tag = cipher.getAuthTag();

const encrypted_message = Buffer.concat([iv, tag, encrypted]).toString('base64');
return encrypted_message;
}

const secret = _crypto.randomBytes(32);
encrypt("secret message", secret);

Elixir 解密

def decrypt(encrypted_message, secret) do
secret_key = :base64.decode(secret)
ciphertext = :base64.decode(encrypted_message)

<<iv::binary-16, tag::binary-16, ciphertext::binary>> = ciphertext

// make sure _AAD is an empty string "" if you didn't set it during encryption
:crypto.block_decrypt(:aes_gcm, secret_key, iv, {_AAD, ciphertext, tag})

// otherwise, you would need to set _AAD to whatever you set during encryption, using "AES256GCM" as example
// Note: AAD (Associated Authenticated Data) can be whatever string you want to my knowledge, just to make sure you have the same in both encryption and decryption process
// :crypto.block_decrypt(:aes_gcm, secret_key, iv, {"AES256GCM", ciphertext, tag})
end

# secret would be the secret from javascript encoded in base64
decrypt(encrypted_message, secret)

最佳答案

这很简单:您的 "AES256GCM" 不应该出现(或者为空,我对 Erlang 不是很熟悉)。它代表额外的认证数据,包含在认证标签的计算中,明显不同于加密代码生成的认证标签。

:aes_gcm 已经指定了模式, key 大小当然是由secret_key 的大小决定的,所以这个字符串无论如何都是不必要的。

关于javascript - 跨平台 AES 256 GCM Javascript 和 Elixir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55834903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com