pu369com

rust 大神crypto2的例子AES加解密

我稍微改了一下,代码:

extern crate crypto2;
use crypto2::blockcipher::Aes128;
fn main() {
    let key = [
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    ];   
    let plaintext_str =String::from("abcdef0123456789");   
    let mut plaintext=[0;16]; 
    for (i,el) in plaintext_str.as_bytes().iter().enumerate(){
        plaintext[i]=*el;
    }       
    let cipher = Aes128::new(&key);
    let mut ciphertext = plaintext;
    cipher.encrypt(&mut ciphertext);    
    let mut cleartext = ciphertext.clone();
    cipher.decrypt(&mut cleartext);    
    println!("plaintext : {:?}", &plaintext[..]);
    println!("ciphertext: {:?}", &ciphertext[..]);
    println!("cleartext : {:?}", &cleartext[..]);
}

 

[dependencies]
crypto2 = "0.1.2"

 

参考:https://crates.io/crates/crypto2

posted on 2021-08-22 09:09  pu369com  阅读(835)  评论(0编辑  收藏  举报

导航