From 79219fdb38262b66ca5a110a78de68a0cc611f83 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 31 May 2024 18:01:16 +0200 Subject: [PATCH] add lab5 --- l5/.gitignore | 3 ++ l5/librc4/Cargo.lock | 75 +++++++++++++++++++++++++++++ l5/librc4/Cargo.toml | 7 +++ l5/librc4/src/lib.rs | 38 +++++++++++++++ l5/zad1/Cargo.lock | 90 +++++++++++++++++++++++++++++++++++ l5/zad1/Cargo.toml | 9 ++++ l5/zad1/src/main.rs | 43 +++++++++++++++++ l5/zad2/Cargo.lock | 82 ++++++++++++++++++++++++++++++++ l5/zad2/Cargo.toml | 7 +++ l5/zad2/src/main.rs | 40 ++++++++++++++++ l5/zad3/Cargo.lock | 109 +++++++++++++++++++++++++++++++++++++++++++ l5/zad3/Cargo.toml | 10 ++++ l5/zad3/links | 1 + l5/zad3/src/main.rs | 66 ++++++++++++++++++++++++++ 14 files changed, 580 insertions(+) create mode 100644 l5/.gitignore create mode 100644 l5/librc4/Cargo.lock create mode 100644 l5/librc4/Cargo.toml create mode 100644 l5/librc4/src/lib.rs create mode 100644 l5/zad1/Cargo.lock create mode 100644 l5/zad1/Cargo.toml create mode 100644 l5/zad1/src/main.rs create mode 100644 l5/zad2/Cargo.lock create mode 100644 l5/zad2/Cargo.toml create mode 100644 l5/zad2/src/main.rs create mode 100644 l5/zad3/Cargo.lock create mode 100644 l5/zad3/Cargo.toml create mode 100644 l5/zad3/links create mode 100644 l5/zad3/src/main.rs diff --git a/l5/.gitignore b/l5/.gitignore new file mode 100644 index 0000000..f4f22b2 --- /dev/null +++ b/l5/.gitignore @@ -0,0 +1,3 @@ +*/target +*.zip +*.jpg diff --git a/l5/librc4/Cargo.lock b/l5/librc4/Cargo.lock new file mode 100644 index 0000000..354c633 --- /dev/null +++ b/l5/librc4/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "librc4" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/l5/librc4/Cargo.toml b/l5/librc4/Cargo.toml new file mode 100644 index 0000000..65cf27f --- /dev/null +++ b/l5/librc4/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "librc4" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/l5/librc4/src/lib.rs b/l5/librc4/src/lib.rs new file mode 100644 index 0000000..280b185 --- /dev/null +++ b/l5/librc4/src/lib.rs @@ -0,0 +1,38 @@ +fn ksa(key: &Vec) -> Vec { + let n = key.len(); + + let t = (0..=255) + .map(|i| key[i % n]) + .collect::>(); + + let mut s = (0..=255).collect::>(); + + let mut j = 0; + for i in 0..256 { + j = (j + s[i] as usize + t[i] as usize) % 256; + s.swap(i, j); + } + + s +} + +fn prga(s: &mut Vec, data: &Vec) -> Vec { + let mut i = 0; + let mut j = 0; + let mut ciphertext = Vec::new(); + for byte in data { + i = (i + 1) % 256; + j = (j + s[i] as usize) % 256; + s.swap(i, j); + let k = s[((s[i] as usize + s[j] as usize) % 256) as usize]; + ciphertext.push(byte ^ k); + } + ciphertext +} + +pub fn rc4(key: &Vec, data: &Vec) -> Vec { + let mut s = ksa(key); + + prga(&mut s, data) +} + diff --git a/l5/zad1/Cargo.lock b/l5/zad1/Cargo.lock new file mode 100644 index 0000000..aa5cb7a --- /dev/null +++ b/l5/zad1/Cargo.lock @@ -0,0 +1,90 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "librc4" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zad1" +version = "0.1.0" +dependencies = [ + "hex", + "librc4", + "rand", +] diff --git a/l5/zad1/Cargo.toml b/l5/zad1/Cargo.toml new file mode 100644 index 0000000..b33d718 --- /dev/null +++ b/l5/zad1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "zad1" +version = "0.1.0" +edition = "2021" + +[dependencies] +librc4 = { path = "../librc4" } +rand = "0.8.5" +hex = "0.4.3" diff --git a/l5/zad1/src/main.rs b/l5/zad1/src/main.rs new file mode 100644 index 0000000..f5d6e9c --- /dev/null +++ b/l5/zad1/src/main.rs @@ -0,0 +1,43 @@ +#![feature(ascii_char)] + +use librc4::rc4; +use rand::{thread_rng, Rng}; + +fn test() { + let key = b"BigBrother".to_vec(); + let cipher = hex::decode("62AEC4043041DB").unwrap(); + + let binding = rc4(&key, &cipher); + let data = binding.as_ascii().unwrap(); + + println!("data: {:?}", data); +} + +fn main() { + test(); + let k0 = b"".to_vec(); + let k1 = b"".to_vec(); + for i in 0..8192 { + print!("{i}: "); + let data = (0..1024) + .map(|_| thread_rng().gen()) + .collect::>(); + let c0 = rc4(&k0, &data); + let d0 = rc4(&k0, &c0); + + let c1 = rc4(&k1, &data); + let d1 = rc4(&k1, &c1); + + fn result(cond: bool) -> char { + match cond { + true => '👍', + false => '👎', + } + } + + print!("k0: {}; ", result(data == d0)); + print!("k1: {};", result(data == d1)); + + println!(); + } +} diff --git a/l5/zad2/Cargo.lock b/l5/zad2/Cargo.lock new file mode 100644 index 0000000..2495c4b --- /dev/null +++ b/l5/zad2/Cargo.lock @@ -0,0 +1,82 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "librc4" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zad2" +version = "0.1.0" +dependencies = [ + "librc4", +] diff --git a/l5/zad2/Cargo.toml b/l5/zad2/Cargo.toml new file mode 100644 index 0000000..ccf0596 --- /dev/null +++ b/l5/zad2/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "zad2" +version = "0.1.0" +edition = "2021" + +[dependencies] +librc4 = { path = "../librc4" } diff --git a/l5/zad2/src/main.rs b/l5/zad2/src/main.rs new file mode 100644 index 0000000..acea665 --- /dev/null +++ b/l5/zad2/src/main.rs @@ -0,0 +1,40 @@ +use librc4::rc4; + +fn check_same_key(c0: &Vec, c1: &Vec) -> bool { + for i in 0..c0.len().min(c1.len()) { + if c0[i] ^ c1[i] >= 0x80 { + return false; + } + } + + true +} + +fn main() { + let k0 = b"some random key".to_vec(); + let k1 = b"a different key".to_vec(); + + let d0 = b"02831923821370983210".to_vec(); + let d1 = b"9483012137839109312903".to_vec(); + + let c0 = rc4(&k0, &d0); + let c1 = rc4(&k1, &d1); + + fn result(cond: bool) -> &'static str { + match cond { + true => "the keys were the same!", + false => "the keys were different!", + } + } + + println!("keys should be different"); + println!("result: {}", result(check_same_key(&c0, &c1))); + + let key = b"these keys are identical".to_vec(); + + let c0 = rc4(&key, &d0); + let c1 = rc4(&key, &d1); + + println!("keys should be the same"); + println!("result: {}", result(check_same_key(&c0, &c1))); +} diff --git a/l5/zad3/Cargo.lock b/l5/zad3/Cargo.lock new file mode 100644 index 0000000..8753f8c --- /dev/null +++ b/l5/zad3/Cargo.lock @@ -0,0 +1,109 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "either" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "librc4" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_pcg" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zad3" +version = "0.1.0" +dependencies = [ + "itertools", + "librc4", + "rand", + "rand_pcg", +] diff --git a/l5/zad3/Cargo.toml b/l5/zad3/Cargo.toml new file mode 100644 index 0000000..5ea455e --- /dev/null +++ b/l5/zad3/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "zad3" +version = "0.1.0" +edition = "2021" + +[dependencies] +librc4 = { path = "../librc4" } +rand = "0.8.5" +rand_pcg = "0.3.1" +itertools = "0.13.0" diff --git a/l5/zad3/links b/l5/zad3/links new file mode 100644 index 0000000..c8b381d --- /dev/null +++ b/l5/zad3/links @@ -0,0 +1 @@ +https://bankoweabc.pl/2021/05/05/cyfry-w-numerze-rachunku-bankowego/ diff --git a/l5/zad3/src/main.rs b/l5/zad3/src/main.rs new file mode 100644 index 0000000..a2adc45 --- /dev/null +++ b/l5/zad3/src/main.rs @@ -0,0 +1,66 @@ +use itertools::Itertools; +use librc4::rc4; +use rand::{Rng, SeedableRng}; +use rand_pcg::Pcg64; + +fn main() { + let bank_numbers = gen_bank_numbers(10); + let key = b"amazingkeysolong".to_vec(); + let cryptograms = &bank_numbers.iter() + .map(|bank_number| rc4(&key, &bank_number.as_bytes().to_vec())) + .collect::>(); + + for (c0, c1) in cryptograms.iter().tuple_combinations() { + let xored: Vec = c0.iter() + .zip(c1.iter()) + .map(|(i0, i1)| i0 ^ i1) + .collect(); + + println!(); + println!("c0: {:?}", c0); + println!("c1: {:?}", c1); + println!("xored: {:?}", xored); + println!(); + } +} + +fn gen_bank_numbers(q: usize) -> Vec { + let mut bank_numbers: Vec = Vec::new(); + let routing_numbers: [[u8; 8]; 5] = [ + [1, 0, 1, 0, 0, 0, 0, 0], // NBP + [1, 0, 2, 0, 1, 0, 9, 7], // PKO BP, oddział 8 w WWA + [1, 0, 5, 0, 1, 9, 2, 4], // ING, oddział pruszków ul Ołówkowa 1d + [1, 2, 4, 0, 5, 4, 8, 4], // Pekao, oddział w Gdańsku ul Franciszka Rakoczego 17 + [1, 7, 5, 0, 1, 2, 8, 1], // BNP Paribas, oddział w Poznaniu al. Solidarności 36 + ]; + let mut rng = Pcg64::seed_from_u64(2137); + for nr in routing_numbers { + for _ in 0..q { + let mut bank_number = String::new(); + let mut client_number = [0u8; 16]; + for i in 0..16 { + client_number[i] = rng.gen_range(0..10); + } + let mut tmp: u128 = 252100; + for i in 0..8 { + tmp += nr[i] as u128 * 10u128.pow((7 - i + 21) as u32); + } + for i in 0..16 { + tmp += client_number[i] as u128 * 10u128.pow((15 - i + 5) as u32); + } + + tmp = tmp % 97; + tmp = 98 - tmp; + + bank_number.push_str(format!("{:02}", tmp).as_str()); + for i in 0..8 { + bank_number.push_str(nr[i].to_string().as_str()); + } + for i in 0..16 { + bank_number.push_str(client_number[i].to_string().as_str()); + } + bank_numbers.push(bank_number); + } + } + bank_numbers +}