跳转到内容
彼岸论坛
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Rust] rust 写代码真的很复杂


小天管理

已推荐帖子

```rust
use aes_gcm::{aead::{Aead, AeadCore, KeyInit, OsRng}, Aes256Gcm, AesGcm};
use aes_gcm::aead::consts::U12;
use aes_gcm::aead::Nonce;
use aes_gcm::aes::Aes256;

// aes-gcm = "0.10.3"

#[test]
fn test_3_aes256_cbc() {
// 生成一个随机的 AES256-GCM 加密密钥 key 。
// The encryption key can be generated randomly:
let key = Aes256Gcm::generate_key(OsRng);

// 使用生成的密钥 key 创建 AES256-GCM 加密器 cipher ,然后生成一个 96 位长度的随机 nonce nonce 。
// 使用 cipher 对 "plaintext message" 进行加密得到 ciphertext ,然后再对 ciphertext 进行解密得到 plaintext 。
// 最后使用 assert_eq! 宏来验证解密后的 plaintext 是否与原始消息相同。
let cipher: AesGcm<Aes256, U12> = Aes256Gcm::new(&key);
// 生成随机 nonce ( 96 bits )
// let nonce = GenericArray::from_slice(&Aes256Gcm::generate_nonce(&mut OsRng));
// let nonce: &GenericArray<u8, <Aes256Gcm as Aead>::NonceSize> = GenericArray::from_slice(&Aes256Gcm::generate_nonce(&mut OsRng));
// let nonce: GenericArray<u8, <Aes256Gcm as Aead>::NonceSize> = ...;
let nonce: Nonce<Aes256Gcm> = Aes256Gcm::generate_nonce(&mut OsRng); // 96-bits; unique per message

// 将明文消息作为字节数组传递给 encrypt 函数。
let plaintext: &[u8] = b"plaintext message";
let ciphertext = cipher.encrypt(&nonce, plaintext).unwrap();
let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();

assert_eq!(&plaintext, b"plaintext message");
}


```

let plaintext: &[u8] = b"plaintext message";

这一行代码,要自己将手动推导的 &[u8;17] 固定长度数组,,修改为可变数组。。

以及一些其他的,,整个代码才可以运行
意见的链接
分享到其他网站

加入讨论

您现在可以发表并稍后注册. 如果您是会员,请现在登录来参与讨论.

游客
回复主题...

×   粘贴为富文本.   粘贴为纯文本来代替

  只允许使用75个表情符号.

×   您的链接已自动嵌入.   显示为链接来代替

×   您之前的内容已恢复.   清除编辑器

×   您无法直接粘贴图片.要从网址上传或插入图片.

  • 游客注册

    游客注册

  • 会员

  • 最新的状态更新

    没有最新的状态更新
  • 最近查看

    • 没有会员查看此页面.
×
×
  • 创建新的...