请问rust中如何实现AES128 ECB模式的加解密?How to implement AES128 ECB mode encryption and decryption in Rust?

I want to decrypt a very long piece of information, and it seems that decrypting this piece of information requires AES128's ECB mode, as found online.
I gave up after trying many Rust libraries.
Please help me.

我想要将一段很长的信息解密,从网上查到解密这段信息似乎需要AES128的ECB模式。
在尝试很多Rust库后我放弃了。
求助大神们帮帮我。
(纯小白一位)

This is my code,but it's worry:

use aes::cipher::{generic_array::GenericArray,BlockDecrypt,KeyInit};
use aes::Aes128;
fn main() {
    let key = GenericArray::from([104_u8, 122, 72, 82, 65, 109, 115, 111, 53, 107, 73, 110, 98, 97, 120, 87]);
    let mut block = GenericArray::from([
        72_u8, 170, 177, 143, 13, 142, 159, 112, 49, 105, 33, 219, 5, 185, 115, 121, 157, 114, 107,
        204, 135, 78, 237, 195, 33, 227, 228, 153, 124, 191, 25, 141, 89, 68, 86, 190, 131, 103,
        49, 69, 245, 21, 198, 44, 154, 235, 236, 218, 67, 1, 170, 42, 6, 1, 16, 179, 245, 176, 73,
        218, 67, 182, 181, 164, 216, 0, 233, 151, 84, 156, 222, 238, 36, 68, 57, 54, 76, 116, 21,
        100, 87, 193, 167, 151, 70, 216, 4, 151, 204, 214, 244, 152, 193, 241, 30, 9, 147, 192,
        141, 65, 54, 76, 178, 100, 251, 97, 214, 22, 151, 190, 26, 88, 112, 97, 192, 162, 194, 144,
        60, 107, 59, 224, 161, 203, 152, 179, 41, 122,
    ]);

    // Initialize cipher
    let cipher = Aes128::new(&key);

    // Decrypt it
    cipher.decrypt_block(&mut block);
}

Hi!

I don't know Chinese, so I had to check a machine translation of your question - I've copied it below in case it was wrong.

Machine translation I want to decrypt a long piece of information. I found from the Internet that decrypting this piece of information seems to require the AES128 ECB mode. I gave up after trying many Rust libraries. Please help me, gods. (A pure novice)

Without knowing the encryption key, it's effectively impossible to decrypt AES128. If you do know the key, you can probably write a little code gluing your application to the aes crate or roll your own implementation of the decryption algorithm.

1 Like

Thanks for your help!
Actually I know the key.

I have referred to the official documentation and written a piece of code like this:

use aes::cipher::{generic_array::GenericArray,BlockDecrypt,KeyInit};
use aes::Aes128;
fn main() {
    let key = GenericArray::from([104, 122, 72, 82, 65, 109, 115, 111, 53, 107, 73, 110, 98, 97, 120, 87]);
    let mut block = GenericArray::from([
        72_u8, 170, 177, 143, 13, 142, 159, 112, 49, 105, 33, 219, 5, 185, 115, 121, 157, 114, 107,
        204, 135, 78, 237, 195, 33, 227, 228, 153, 124, 191, 25, 141, 89, 68, 86, 190, 131, 103,
        49, 69, 245, 21, 198, 44, 154, 235, 236, 218, 67, 1, 170, 42, 6, 1, 16, 179, 245, 176, 73,
        218, 67, 182, 181, 164, 216, 0, 233, 151, 84, 156, 222, 238, 36, 68, 57, 54, 76, 116, 21,
        100, 87, 193, 167, 151, 70, 216, 4, 151, 204, 214, 244, 152, 193, 241, 30, 9, 147, 192,
        141, 65, 54, 76, 178, 100, 251, 97, 214, 22, 151, 190, 26, 88, 112, 97, 192, 162, 194, 144,
        60, 107, 59, 224, 161, 203, 152, 179, 41, 122,
    ]);

    // Initialize cipher
    let cipher = Aes128::new(&key);

    // Decrypt it
    cipher.decrypt_block(&mut block);
}


BUT Rust says that:

error[E0308]: mismatched types
   --> src\main.rs:16:30
    |
16  |     let cipher = Aes128::new(&key);
    |                  ----------- ^^^^ expected `&GenericArray<u8, ...>`, found `&GenericArray<..., ...>`

How can I fix it?

On the first number of your array add the suffix _u8 to tell the compiler the type of integer you want. Like you did for the block.

[1_u8, 3, 6, 9, ...]

Thank you but it still not work......
This is the full error reporting:

error[E0308]: mismatched types
   --> src\main.rs:19:26
    |
19  |     cipher.decrypt_block(&mut block);
    |            ------------- ^^^^^^^^^^ expected `UTerm`, found `UInt<UInt<UInt<UTerm, B1>, B0>, B0>`
    |            |
    |            arguments to this method are incorrect
    |
    = note: expected mutable reference `&mut GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>>`
               found mutable reference `&mut GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>>`
note: method defined here
   --> C:\Users\Lkhsss\.cargo\registry\src\rsproxy.cn-0dccff568467c15b\cipher-0.4.4\src\block.rs:213:8
    |
213 |     fn decrypt_block(&self, block: &mut Block<Self>) {
    |        ^^^^^^^^^^^^^

Seems like your array is larger than expected.

But the data is just that long.

I see. Everything within the array must be a u8.

See the example from the documentation:

use aes::Aes128;
use aes::cipher::{
    BlockCipher, BlockEncrypt, BlockDecrypt, KeyInit,
    generic_array::GenericArray,
};

let key = GenericArray::from([0u8; 16]);
let mut block = GenericArray::from([42u8; 16]);

// Initialize cipher
let cipher = Aes128::new(&key);

let block_copy = block.clone();

// Encrypt block in-place
cipher.encrypt_block(&mut block);

// And decrypt it back
cipher.decrypt_block(&mut block);
assert_eq!(block, block_copy);

Emmm
But every data in my array is u8...........

It's a block cypher which encrypts/decrypts one block at a time. (Split your data up into multiple blocks.)

use aes::cipher::{generic_array::GenericArray,BlockDecrypt,KeyInit};
use aes::Aes128;
fn main() {
    let key = GenericArray::from([104, 122, 72, 82, 65, 109, 115, 111, 53, 107, 73, 110, 98, 97, 120, 87]);
    let mut blocks = [
        GenericArray::from([72_u8, 170, 177, 143,  13, 142, 159, 112,  49, 105,  33, 219,   5, 185, 115, 121, ]),
        GenericArray::from([157,   114, 107, 204, 135,  78, 237, 195,  33, 227, 228, 153, 124, 191,  25, 141, ]),
        GenericArray::from([ 89,    68,  86, 190, 131, 103,  49,  69, 245,  21, 198,  44, 154, 235, 236, 218, ]),
        GenericArray::from([ 67,     1, 170,  42,   6,   1,  16, 179, 245, 176, 73,  218,  67, 182, 181, 164, ]),
        GenericArray::from([216,     0, 233, 151,  84, 156, 222, 238,  36,  68,  57,  54,  76, 116,  21, 100, ]),
        GenericArray::from([ 87,   193, 167, 151,  70, 216,   4, 151, 204, 214, 244, 152, 193, 241,  30,   9, ]),
        GenericArray::from([147,   192, 141,  65,   54, 76, 178, 100, 251,  97, 214,  22, 151, 190,  26,  88, ]),
        GenericArray::from([112,    97, 192, 162, 194, 144,  60, 107,  59, 224, 161, 203, 152, 179,  41, 122, ]),
    ];

    // Initialize cipher
    let cipher = Aes128::new(&key);

    // Decrypt it
    cipher.decrypt_blocks(&mut blocks);
}
4 Likes

Wow! Thank you!
Let me try it!

It works!!!!!!
Thank you very much!