Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Encryption 即使数据正确,AES-GCM 256解密也会失败_Encryption_Rust_Cryptography_Aes_Aes Gcm - Fatal编程技术网

Encryption 即使数据正确,AES-GCM 256解密也会失败

Encryption 即使数据正确,AES-GCM 256解密也会失败,encryption,rust,cryptography,aes,aes-gcm,Encryption,Rust,Cryptography,Aes,Aes Gcm,我有一个给定的加密消息(已解密,它是“加密的秘密消息”),我正试图从AES-GCM 256加密消息中检索这个原始字符串。我使用aes gcm板条箱来执行以下操作: 使用aes_gcm::Aes256Gcm; 使用aead::{aead,NewAead,generic_数组::GenericArray}; fn main(){ 让key_bytes=hex::decode(“ce265dbc38bb25ef29fade77a4b88fe07b6063215f6526a4623cf810a3d611

我有一个给定的加密消息(已解密,它是“加密的秘密消息”),我正试图从AES-GCM 256加密消息中检索这个原始字符串。我使用aes gcm板条箱来执行以下操作:

使用aes_gcm::Aes256Gcm;
使用aead::{aead,NewAead,generic_数组::GenericArray};
fn main(){
让key_bytes=hex::decode(“ce265dbc38bb25ef29fade77a4b88fe07b6063215f6526a4623cf810a3d611c9”).unwrap();
让nonce_bytes=hex::decode(“CE77357FE7B2400408F44”).unwrap();
让密文字节=十六进制::解码(“fd77fae68fa27ea00afbe474f4fcd47248a19b3cbf2a6d7e”).unwrap();
让key=GenericArray::从_片克隆_(key_bytes.as_片());
让nonce=GenericArray::from_slice(nonce_bytes.as_slice());
让cipher=Aes256Gcm::新建(密钥);
让明文=cipher.decrypt(nonce,ciphertext_bytes.as_slice()).unwrap();//解密失败时死机
println!(“{:?}”,明文);
}
这是my Cargo.toml的
依赖项部分:

[dependencies]
aes-gcm = "0.5.0"
aead = "0.2.0"
hex = "0.4.2"
问题是,程序在第13行的
unwrap
调用时总是出现恐慌,即使加密的消息、密钥和nonce都很好:

线程'main'在错误值为Error'的'called Result::unwrap()'处惊慌失措,src\main.rs:13:21
它看起来像是加密消息无效、加密消息、密钥或nonce无效的错误,但它不是。我写了一个Python程序,它做了完全相同的事情,并且工作正常;输出确实是
加密的秘密消息

从加密密码导入AES
key=bytes.fromhex(“ce265dbc38bb25ef29fade77a4b88fe07b6063215f6526a4623cf810a3d611c9”)
nonce=bytes.fromhex(“CE77357FE7B2400408F44”)
cipher_text=bytes.fromhex(“fd77fae68fa27ea00afbe474f4fcd47248a19b3cbf2a6d7e”)
密码=AES.new(密钥,AES.MODE\u GCM,nonce=nonce)
明文=密码。解密(密码文本)
打印(纯文本解码(“utf-8”))
我希望能够用Rust而不是Python来解密这些加密消息。我根本不知道为什么会出错。我错过什么了吗?

我的:

默认实现采用后缀标记(ala AES-GCM、AES-GCM-SIV、ChaCha20Poly1305)。不使用后缀标记的Aead实现将需要覆盖该标记以正确组装密文消息

既然你说你有标签:

我也有这个加密邮件的相关标签

您可以遵循相同的模式:

use aead::{generic_array::GenericArray, Aead, NewAead};
use aes_gcm::Aes256Gcm;

fn main() {
    let key_hex = "ce265dbc38bb25ef29fade77a4b88fe07b6063215f6526a4623cf810a3d611c9";
    let nonce_hex = "ce77357fe7b2401400408f44";
    let ciphertext_hex = "fd77fae68fa27ea00afbe474f4fcd47248a19b3cbf2a6d7e";

    // Append the tag data to the encrypted data
    let tag_hex = "2ef2a9d27909df90bcb45606067148a6";
    let ciphertext_and_tag_hex = format!("{}{}", ciphertext_hex, tag_hex);
    let ciphertext_bytes = hex::decode(ciphertext_and_tag_hex).unwrap();

    let key_bytes = hex::decode(key_hex).unwrap();
    let nonce_bytes = hex::decode(nonce_hex).unwrap();

    let key = GenericArray::clone_from_slice(&key_bytes);
    let nonce = GenericArray::from_slice(&nonce_bytes);

    let cipher = Aes256Gcm::new(key);

    let plaintext = cipher.decrypt(nonce, &*ciphertext_bytes).unwrap();
    println!("{}", String::from_utf8_lossy(&plaintext));
}

您还可以研究该方法,该方法更为复杂,但允许单独指定标记。

如果使用Rust加密数据,它将包含大量额外字节(十六进制:
2EF2AA9D27909DF90BCB456067148A6
)。这看起来你实际上并没有在两边使用相同的密码。事实上,数据并不是用锈码加密的。这个程序只是解密来自其他地方已经加密的消息。这就是我的观点。如果使用Rust代码对其进行加密,则密码文本会有所不同,但会在开头匹配。这让我觉得你实际上并没有使用相同的密码。尤其是padding.GCM是一种经过身份验证的密码模式,因此它应该有一个MAC标记。我怀疑是缺少它导致了这个错误。解密方法似乎希望它连接到密文。您的密文是在其他系统上加密的。这意味着您在任何地方都不依赖任何系统默认值,因为其他系统上的默认值可能不同。将代码中的所有内容显式设置为与用于加密的设置相匹配,包括加密端的所有默认设置。