From abda5da496477142672674891e8a0bea3a856362 Mon Sep 17 00:00:00 2001 From: Thomas Lindner Date: Wed, 27 Dec 2023 20:23:38 +0100 Subject: [PATCH] solve Challenge 7 of Set 1 --- Cargo.lock | 88 +++++++++++++++++++++++++ Cargo.toml | 1 + set1/challenge7_aes_ecb/7.txt | 64 ++++++++++++++++++ set1/challenge7_aes_ecb/7_plaintext.txt | 79 ++++++++++++++++++++++ set1/challenge7_aes_ecb/Cargo.toml | 12 ++++ set1/challenge7_aes_ecb/src/main.rs | 49 ++++++++++++++ 6 files changed, 293 insertions(+) create mode 100644 set1/challenge7_aes_ecb/7.txt create mode 100644 set1/challenge7_aes_ecb/7_plaintext.txt create mode 100644 set1/challenge7_aes_ecb/Cargo.toml create mode 100644 set1/challenge7_aes_ecb/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index a5320d3..faf5e48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,28 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aes" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac1f845298e95f983ff1944b728ae08b8cebab80d684f0a832ed0fc74dfa27e2" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes_ecb" +version = "0.1.0" +dependencies = [ + "aes", + "common", + "env_logger", + "log", + "rustc-serialize", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -27,6 +49,22 @@ dependencies = [ "rustc-serialize", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "common" version = "0.1.0" @@ -34,6 +72,25 @@ dependencies = [ "log", ] +[[package]] +name = "cpufeatures" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "detect_xor" version = "0.1.0" @@ -77,6 +134,16 @@ dependencies = [ "rustc-serialize", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "hermit-abi" version = "0.3.3" @@ -98,6 +165,15 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + [[package]] name = "is-terminal" version = "0.4.9" @@ -200,6 +276,18 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 800e077..3dd6c00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ members = [ "set1/challenge4_detect_xor", "set1/challenge5_repeating_xor", "set1/challenge6_break_repeating_xor", + "set1/challenge7_aes_ecb", ] resolver = "2" diff --git a/set1/challenge7_aes_ecb/7.txt b/set1/challenge7_aes_ecb/7.txt new file mode 100644 index 0000000..c02ff8a --- /dev/null +++ b/set1/challenge7_aes_ecb/7.txt @@ -0,0 +1,64 @@ +CRIwqt4+szDbqkNY+I0qbDe3LQz0wiw0SuxBQtAM5TDdMbjCMD/venUDW9BL +PEXODbk6a48oMbAY6DDZsuLbc0uR9cp9hQ0QQGATyyCESq2NSsvhx5zKlLtz +dsnfK5ED5srKjK7Fz4Q38/ttd+stL/9WnDzlJvAo7WBsjI5YJc2gmAYayNfm +CW2lhZE/ZLG0CBD2aPw0W417QYb4cAIOW92jYRiJ4PTsBBHDe8o4JwqaUac6 +rqdi833kbyAOV/Y2RMbN0oDb9Rq8uRHvbrqQJaJieaswEtMkgUt3P5Ttgeh7 +J+hE6TR0uHot8WzHyAKNbUWHoi/5zcRCUipvVOYLoBZXlNu4qnwoCZRSBgvC +wTdz3Cbsp/P2wXB8tiz6l9rL2bLhBt13Qxyhhu0H0+JKj6soSeX5ZD1Rpilp +9ncR1tHW8+uurQKyXN4xKeGjaKLOejr2xDIw+aWF7GszU4qJhXBnXTIUUNUf +RlwEpS6FZcsMzemQF30ezSJHfpW7DVHzwiLyeiTJRKoVUwo43PXupnJXDmUy +sCa2nQz/iEwyor6kPekLv1csm1Pa2LZmbA9Ujzz8zb/gFXtQqBAN4zA8/wt0 +VfoOsEZwcsaLOWUPtF/Ry3VhlKwXE7gGH/bbShAIKQqMqqUkEucZ3HPHAVp7 +ZCn3Ox6+c5QJ3Uv8V7L7SprofPFN6F+kfDM4zAc59do5twgDoClCbxxG0L19 +TBGHiYP3CygeY1HLMrX6KqypJfFJW5O9wNIF0qfOC2lWFgwayOwq41xdFSCW +0/EBSc7cJw3N06WThrW5LimAOt5L9c7Ik4YIxu0K9JZwAxfcU4ShYu6euYmW +LP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UW3u6xnhH3mYVRk88EMtpEpKrSIWfXphgDUPZ0f4agRzehkn9vtzCm +NjFnQb0/shnqTh4Mo/8oommbsBTUKPYS7/1oQCi12QABjJDt+LyUan+4iwvC +i0k0IUIHvk21381vC0ixYDZxzY64+xx/RNID+iplgzq9PDZgjc8L7jMg+2+m +rxPS56e71m5E2zufZ4d+nFjIg+dHD/ShNPzVpXizRVUERztLuak8Asah3/yv +wOrH1mKEMMGC1/6qfvZUgFLJH5V0Ep0n2K/Fbs0VljENIN8cjkCKdG8aBnef +EhITdV7CVjXcivQ6efkbOQCfkfcwWpaBFC8tD/zebXFE+JshW16D4EWXMnSm +/9HcGwHvtlAj04rwrZ5tRvAgf1IR83kqqiTvqfENcj7ddCFwtNZrQK7EJhgB +5Tr1tBFcb9InPRtS3KYteYHl3HWR9t8E2YGE8IGrS1sQibxaK/C0kKbqIrKp +npwtoOLsZPNbPw6K2jpko9NeZAx7PYFmamR4D50KtzgELQcaEsi5aCztMg7f +p1mK6ijyMKIRKwNKIYHagRRVLNgQLg/WTKzGVbWwq6kQaQyArwQCUXo4uRty +zGMaKbTG4dns1OFB1g7NCiPb6s1lv0/lHFAF6HwoYV/FPSL/pirxyDSBb/FR +RA3PIfmvGfMUGFVWlyS7+O73l5oIJHxuaJrR4EenzAu4Avpa5d+VuiYbM10a +LaVegVPvFn4pCP4U/Nbbw4OTCFX2HKmWEiVBB0O3J9xwXWpxN1Vr5CDi75Fq +NhxYCjgSJzWOUD34Y1dAfcj57VINmQVEWyc8Tch8vg9MnHGCOfOjRqp0VGyA +S15AVD2QS1V6fhRimJSVyT6QuGb8tKRsl2N+a2Xze36vgMhw7XK7zh//jC2H diff --git a/set1/challenge7_aes_ecb/7_plaintext.txt b/set1/challenge7_aes_ecb/7_plaintext.txt new file mode 100644 index 0000000..14283df --- /dev/null +++ b/set1/challenge7_aes_ecb/7_plaintext.txt @@ -0,0 +1,79 @@ +I'm back and I'm ringin' the bell +A rockin' on the mike while the fly girls yell +In ecstasy in the back of me +Well that's my DJ Deshay cuttin' all them Z's +Hittin' hard and the girlies goin' crazy +Vanilla's on the mike, man I'm not lazy. + +I'm lettin' my drug kick in +It controls my mouth and I begin +To just let it flow, let my concepts go +My posse's to the side yellin', Go Vanilla Go! + +Smooth 'cause that's the way I will be +And if you don't give a damn, then +Why you starin' at me +So get off 'cause I control the stage +There's no dissin' allowed +I'm in my own phase +The girlies sa y they love me and that is ok +And I can dance better than any kid n' play + +Stage 2 -- Yea the one ya' wanna listen to +It's off my head so let the beat play through +So I can funk it up and make it sound good +1-2-3 Yo -- Knock on some wood +For good luck, I like my rhymes atrocious +Supercalafragilisticexpialidocious +I'm an effect and that you can bet +I can take a fly girl and make her wet. + +I'm like Samson -- Samson to Delilah +There's no denyin', You can try to hang +But you'll keep tryin' to get my style +Over and over, practice makes perfect +But not if you're a loafer. + +You'll get nowhere, no place, no time, no girls +Soon -- Oh my God, homebody, you probably eat +Spaghetti with a spoon! Come on and say it! + +VIP. Vanilla Ice yep, yep, I'm comin' hard like a rhino +Intoxicating so you stagger like a wino +So punks stop trying and girl stop cryin' +Vanilla Ice is sellin' and you people are buyin' +'Cause why the freaks are jockin' like Crazy Glue +Movin' and groovin' trying to sing along +All through the ghetto groovin' this here song +Now you're amazed by the VIP posse. + +Steppin' so hard like a German Nazi +Startled by the bases hittin' ground +There's no trippin' on mine, I'm just gettin' down +Sparkamatic, I'm hangin' tight like a fanatic +You trapped me once and I thought that +You might have it +So step down and lend me your ear +'89 in my time! You, '90 is my year. + +You're weakenin' fast, YO! and I can tell it +Your body's gettin' hot, so, so I can smell it +So don't be mad and don't be sad +'Cause the lyrics belong to ICE, You can call me Dad +You're pitchin' a fit, so step back and endure +Let the witch doctor, Ice, do the dance to cure +So come up close and don't be square +You wanna battle me -- Anytime, anywhere + +You thought that I was weak, Boy, you're dead wrong +So come on, everybody and sing this song + +Say -- Play that funky music Say, go white boy, go white boy go +play that funky music Go white boy, go white boy, go +Lay down and boogie and play that funky music till you die. + +Play that funky music Come on, Come on, let me hear +Play that funky music white boy you say it, say it +Play that funky music A little louder now +Play that funky music, white boy Come on, Come on, Come on +Play that funky music diff --git a/set1/challenge7_aes_ecb/Cargo.toml b/set1/challenge7_aes_ecb/Cargo.toml new file mode 100644 index 0000000..5ff5361 --- /dev/null +++ b/set1/challenge7_aes_ecb/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "aes_ecb" +version = "0.1.0" +edition = "2021" +authors = ["Thomas Lindner "] + +[dependencies] +aes = "0.8.3" +common = { path = "../../common" } +log = "0.4" +env_logger = "0.10" +rustc-serialize = "0.3" diff --git a/set1/challenge7_aes_ecb/src/main.rs b/set1/challenge7_aes_ecb/src/main.rs new file mode 100644 index 0000000..548439c --- /dev/null +++ b/set1/challenge7_aes_ecb/src/main.rs @@ -0,0 +1,49 @@ +use aes::cipher::generic_array::GenericArray; +use aes::cipher::{BlockDecrypt, KeyInit}; +use aes::Aes128; +use log::error; +use rustc_serialize::base64::FromBase64; +use std::error::Error; +use std::io::{read_to_string, stdin, Read}; + +fn decrypt_aes_ecb(input: &mut dyn Read) -> Result> { + let key = GenericArray::from_slice("YELLOW SUBMARINE".as_bytes()); + let mut text = read_to_string(input)?.from_base64()?; + let len = text.len(); + let cipher = Aes128::new(&key); + for block_nr in 0..len / 16 { + let block = GenericArray::from_mut_slice(&mut text[block_nr * 16..(block_nr + 1) * 16]); + cipher.decrypt_block(block); + } + Ok(String::from_utf8( + text[..len - text[len - 1] as usize].to_vec(), + )?) +} + +fn main() { + env_logger::init(); + + match decrypt_aes_ecb(&mut stdin().lock()) { + Ok(s) => println!("{}", s), + Err(e) => error!("{}", e), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + use std::io::BufReader; + use std::path::Path; + + #[test] + fn challenge7_decrypt_aes_ecb() { + let cargo_manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let testinput = File::open(cargo_manifest_dir.join("7.txt")).unwrap(); + let decrypted = decrypt_aes_ecb(&mut BufReader::new(testinput)).unwrap(); + let plaintext = + read_to_string(File::open(cargo_manifest_dir.join("7_plaintext.txt")).unwrap()) + .unwrap(); + assert_eq!(decrypted, plaintext); + } +}