This commit is contained in:
jacekpoz 2024-05-31 18:01:16 +02:00
parent e96c391fa0
commit 79219fdb38
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
14 changed files with 580 additions and 0 deletions

3
l5/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*/target
*.zip
*.jpg

75
l5/librc4/Cargo.lock generated Normal file
View file

@ -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"

7
l5/librc4/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "librc4"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"

38
l5/librc4/src/lib.rs Normal file
View file

@ -0,0 +1,38 @@
fn ksa(key: &Vec<u8>) -> Vec<u8> {
let n = key.len();
let t = (0..=255)
.map(|i| key[i % n])
.collect::<Vec<_>>();
let mut s = (0..=255).collect::<Vec<_>>();
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<u8>, data: &Vec<u8>) -> Vec<u8> {
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<u8>, data: &Vec<u8>) -> Vec<u8> {
let mut s = ksa(key);
prga(&mut s, data)
}

90
l5/zad1/Cargo.lock generated Normal file
View file

@ -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",
]

9
l5/zad1/Cargo.toml Normal file
View file

@ -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"

43
l5/zad1/src/main.rs Normal file
View file

@ -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::<Vec<u8>>();
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!();
}
}

82
l5/zad2/Cargo.lock generated Normal file
View file

@ -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",
]

7
l5/zad2/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "zad2"
version = "0.1.0"
edition = "2021"
[dependencies]
librc4 = { path = "../librc4" }

40
l5/zad2/src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use librc4::rc4;
fn check_same_key(c0: &Vec<u8>, c1: &Vec<u8>) -> 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)));
}

109
l5/zad3/Cargo.lock generated Normal file
View file

@ -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",
]

10
l5/zad3/Cargo.toml Normal file
View file

@ -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"

1
l5/zad3/links Normal file
View file

@ -0,0 +1 @@
https://bankoweabc.pl/2021/05/05/cyfry-w-numerze-rachunku-bankowego/

66
l5/zad3/src/main.rs Normal file
View file

@ -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::<Vec<_>>();
for (c0, c1) in cryptograms.iter().tuple_combinations() {
let xored: Vec<u8> = 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<String> {
let mut bank_numbers: Vec<String> = 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
}