我正在尝试用 Rust 实现解密服务。 rust-crypto crate 无法在我的机器(Apple M1 Max)上运行,因为符号
_rust_crypto_util_fixed_time_eq_asm
未定义。
切换到 aes-gcm crate (v0.10.3) 后,程序现在不会立即失败,而是解密在内部失败并仅返回
aead::Error
。
密钥、iv(随机数)和解密数据正确(用Java程序测试)。没有标签。
我错过了什么吗?除了缺少的问号之外,我刚刚使用了 AES-GCM 箱文档中的示例代码。
代码:
use aes_gcm::{aead::{Aead, AeadCore, KeyInit}, Nonce, Key, Aes128Gcm};
fn main() {
let key = hex::decode("some_hex_string").expect("Decoding failed");
let key = Key::<Aes128Gcm>::from_slice(key.as_ref());
let cipher = Aes128Gcm::new(&key);
let iv: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
let nonce = Nonce::from_slice(&iv);
let plaintext = cipher.decrypt(nonce, decryped_byte_array.as_ref());
}
我发现,如果我不能使用标签,我就不能使用
cipher.decrypt
函数,因为它要求我发送 payload (包含 AEAD/标签)或加密消息+标签的串联。 (标签不能为空)
按照此 StackOverflow 帖子 上接受的答案,我现在使用 底层函数。 (仍然没有弄清楚为什么“加密”适用于“解密,但没关系......?)
cipher.encrypt_in_place_detached(&iv.into(), &[], &mut some_byte_array)
// Manipulate the now decrypted some_byte_array further