remove unused code

This commit is contained in:
Sam Hadow 2024-04-28 18:33:50 +02:00
parent afeb5dd280
commit 6b1a7eed5e
4 changed files with 51 additions and 65 deletions

View File

@ -279,19 +279,6 @@ impl Aes {
result result
} }
pub fn encrypt_string(&self, text: &String) -> [u8; 16] {
let mut block = [0u8; 16];
let bytes = text.as_bytes();
if bytes.len() > 16 {
println!("string too long, will be cut.")
}
let len = bytes.len().min(16);
block[..len].copy_from_slice(&bytes[..len]);
self.encrypt_block(&block)
}
pub fn decrypt_block(&self, block: &[u8; 16]) -> [u8; 16] { pub fn decrypt_block(&self, block: &[u8; 16]) -> [u8; 16] {
let mut result = [0u8; 16]; let mut result = [0u8; 16];
@ -328,19 +315,6 @@ impl Aes {
result result
} }
pub fn decrypt_string(&self, text: &String) -> [u8; 16] {
let mut block = [0u8; 16];
let bytes = text.as_bytes();
if bytes.len() > 16 {
println!("string too long, will be cut.")
}
let len = bytes.len().min(16);
block[..len].copy_from_slice(&bytes[..len]);
self.decrypt_block(&block)
}
pub fn key_schedule(key_bytes: &[u8; 16]) -> [[u8; 4]; 44] { pub fn key_schedule(key_bytes: &[u8; 16]) -> [[u8; 4]; 44] {
let mut original_key = [[0u8; 4]; 4]; let mut original_key = [[0u8; 4]; 4];
let mut expanded_key = [[0u8; 4]; 44]; let mut expanded_key = [[0u8; 4]; 44];

View File

@ -62,26 +62,23 @@ fn main() -> std::io::Result<()> {
decrypt, decrypt,
} => { } => {
println!("original text: {}", &text); println!("original text: {}", &text);
let aeskey: [u8; 16];
let result: [u8; 16]; let result: [u8; 16];
let aestext: [u8; 16];
let mut aesnturn: usize = 10; let mut aesnturn: usize = 10;
match hexkey { let aeskey: [u8; 16] = match hexkey {
false => aeskey = utils::parse_hex_string(&key), false => utils::parse_hex_string(key),
true => aeskey = utils::string_to_u8_16(&key), true => utils::string_to_u8_16(key),
} };
match nturn { if let Some(number) = nturn {
Some(number) => aesnturn = number.parse::<usize>().unwrap(), aesnturn = number.parse::<usize>().unwrap()
_ => {}
} }
let aescipher = Aes::new(&aeskey, &aesnturn); let aescipher = Aes::new(&aeskey, &aesnturn);
match hextext { let aestext: [u8; 16] = match hextext {
true => aestext = utils::parse_hex_string(&text), true => utils::parse_hex_string(text),
false => aestext = utils::string_to_u8_16(&text), false => utils::string_to_u8_16(text),
} };
match decrypt { match decrypt {
true => { true => {
@ -97,11 +94,10 @@ fn main() -> std::io::Result<()> {
println!(); println!();
} }
Commands::Findkey { key, hexkey } => { Commands::Findkey { key, hexkey } => {
let aeskey: [u8; 16]; let aeskey: [u8; 16] = match hexkey {
match hexkey { false => utils::parse_hex_string(key),
false => aeskey = utils::parse_hex_string(&key), true => utils::string_to_u8_16(key),
true => aeskey = utils::string_to_u8_16(&key), };
}
let keyfound: [u8; 16] = Aes::findkey(&aeskey); let keyfound: [u8; 16] = Aes::findkey(&aeskey);
if keyfound == aeskey { if keyfound == aeskey {
println!("key found."); println!("key found.");

View File

@ -47,25 +47,27 @@ impl Aes {
} }
ciphertexts ciphertexts
} }
pub fn decrypt_block_reduced_1_step(&self, block: &[u8; 16]) -> [u8; 16] {
let mut result = [0u8; 16];
let mut state = [[0u8; 4]; 4]; // pub fn decrypt_block_reduced_1_step(&self, block: &[u8; 16]) -> [u8; 16] {
for i in 0..16 { // let mut result = [0u8; 16];
state[i % 4][i / 4] = block[i]; //
} // let mut state = [[0u8; 4]; 4];
add_round_key(&mut state, &clone_into_array(&self.expanded_key[16..20])); // for i in 0..16 {
inverse_shift_rows(&mut state); // state[i % 4][i / 4] = block[i];
inverse_substitute_state(&mut state); // }
// add_round_key(&mut state, &clone_into_array(&self.expanded_key[16..20]));
// inverse_shift_rows(&mut state);
// inverse_substitute_state(&mut state);
//
// for i in 0..4 {
// for j in 0..4 {
// result[4 * j + i] = state[i][j]
// }
// }
//
// result
// }
for i in 0..4 {
for j in 0..4 {
result[4 * j + i] = state[i][j]
}
}
result
}
pub fn guessroundkey(&texts: &[[u8; 16]; 256]) -> [Vec<u8>; 16] { pub fn guessroundkey(&texts: &[[u8; 16]; 256]) -> [Vec<u8>; 16] {
let mut key: [Vec<u8>; 16] = Default::default(); let mut key: [Vec<u8>; 16] = Default::default();
let mut s: u8; let mut s: u8;

View File

@ -25,8 +25,22 @@ pub fn string_to_u8_16(input: &String) -> [u8; 16] {
result result
} }
pub fn u8_16_to_string(input: &[u8; 16]) -> String { // pub fn u8_16_to_string(input: &[u8; 16]) -> String {
let slice = &input[..]; // let slice = &input[..];
let string = str::from_utf8(slice).expect("Invalid UTF-8"); // let string = str::from_utf8(slice).expect("Invalid UTF-8");
string.to_string() // string.to_string()
// }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_hex_string_test() {
let result: [u8; 16] = parse_hex_string(&String::from("2b7e151628aed2a6abf7158809cf4f3c"));
let expected: [u8; 16] = [
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf,
0x4f, 0x3c,
];
assert_eq!(result, expected);
}
} }