I'm having trouble decrypting a byte string using the openssl crate. As a heads up this is for the Cryptopals challenges, specifically set 2 problem 2. The text file has been encrypted using AES with CBC mode, but I guess a single block can be decrypted with ECB.
I've already tried decrypting the entire 10.txt
file with CBC mode, and I know that works. I've also used the following Python code to verify that ECB decryption of the sames bytes also works.
For Python 3:
from base64 import b64decode
from Crypto.Cipher import AES
def main():
key = b'YELLOW SUBMARINE'
with open("10.txt") as input_file:
data = b64decode(input_file.read())
data = data[0:AES.block_size]
print(data)
cipher = AES.new(key, AES.MODE_ECB)
x = cipher.decrypt(data)
print(x)
return
For Rust:
extern crate openssl;
use openssl::symm::decrypt;
use openssl::symm::{encrypt, Cipher};
static KEY: &'static [u8] = b"YELLOW SUBMARINE";
fn main() -> std::io::Result<()> {
// No idea why this OpenSSL call doesn't work.
let data = b"\x09\x12\x30\xaa\xde\x3e\xb3\x30\xdb\xaa\x43\x58\xf8\x8d\x2a\x6c";
let cipher = Cipher::aes_128_ecb();
let new_data = decrypt(cipher, KEY, None, data);
println!("error {:?}", new_data);
Ok(())
}
I expected to see this function decrypt the byte string in Rust the same way it has been done in python.
OpenSSL is telling you that your input is not correctly padded. Change the input to
let data = b"\x09\x12\x30\xaa\xde\x3e\xb3\x30\xdb\xaa\x43\x58\xf8\x8d\x2a\x6c\x60\xfa\x36\x70\x7e\x45\xf4\x99\xdb\xa0\xf2\x5b\x92\x23\x01\xa5";
and you’ll see that your code decrypts it correctly.
If you need to disable padding, you can do that by using a Crypter
object and calling .pad(false)
on it (example).