Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This use case does not work #29

Closed
shijimasoft opened this issue Sep 30, 2023 · 7 comments
Closed

This use case does not work #29

shijimasoft opened this issue Sep 30, 2023 · 7 comments

Comments

@shijimasoft
Copy link

I open this issue because I ran into a somewhat strange situation: I am rewriting a python code in rust using libaes for AES decryption, below is the code I use:

    let data: [u8; 16] = hex!("9f2707bc98bb5781d4e7b461bffe6270");
    let key: [u8; 16] = hex!("64c5fd55dd3ad988325baaec5243db98");
    let iv: [u8; 16] = hex!("0004008c001658000000000000000000");
    
    let aes: Cipher = Cipher::new_128(&key);
    let dec: Vec<u8> = aes.cbc_decrypt(&iv, &data);
    
    println!("{:?}", dec); // This will print an empty vec

Using pycrypto instead works correctly, returning the expected result: 59f96218d8eccab277ed477a33dcb7f3 (expected byte array translated into an hex string)

Maybe I'm doing something wrong or just forgetting some padding things or the data format is incorrect (literal hex! returns a decimal format byte array i.e: [159, 39, 7, 188, 152, 187, 87, 129, 212, 231, 180, 97, 191, 254, 98, 112])

@shijimasoft
Copy link
Author

I tried the reverse (encryption) process and the data I got are slightly different from what I expected in fact the encrypted data are: 9f2707bc98bb5781d4e7b461bffe6270b419f11edb920f94f6a1b6ec8e469926 (32 bytes) and not the expected 9f2707bc98bb5781d4e7b461bffe6270 (16 bytes).

@keepsimple1
Copy link
Owner

This is because padding (PKCS7) is always included, hence for a 16-byte input, its padding will be another 16-byte padding, leading to total 32 bytes.

pycrypto is different in a way that padding is handled separately and require the user to add padding when needed.

@shijimasoft
Copy link
Author

shijimasoft commented Sep 30, 2023

Is it possible to implement a flag for manual padding management? (For encryption as well as decryption)

@keepsimple1
Copy link
Owner

Is it possible to implement a flag for manual padding management? (For encryption as well as decryption)

Yes it's possible. I've opened a PR #30 to do that. Would you have time to try out the PR's branch and see it works for you?

@shijimasoft
Copy link
Author

I just checked the PR, everything seems to be working correctly (I also tried other use cases and they are all correct).
Thank you for being so quick to help me!

@keepsimple1
Copy link
Owner

Thanks for verifying! I've merged the PR. Will post a new release soon.

@shijimasoft
Copy link
Author

Okk! Thank you for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants