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
}
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] {
let mut result = [0u8; 16];
@ -328,19 +315,6 @@ impl Aes {
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] {
let mut original_key = [[0u8; 4]; 4];
let mut expanded_key = [[0u8; 4]; 44];

View File

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

View File

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

View File

@ -25,8 +25,22 @@ pub fn string_to_u8_16(input: &String) -> [u8; 16] {
result
}
pub fn u8_16_to_string(input: &[u8; 16]) -> String {
let slice = &input[..];
let string = str::from_utf8(slice).expect("Invalid UTF-8");
string.to_string()
// pub fn u8_16_to_string(input: &[u8; 16]) -> String {
// let slice = &input[..];
// let string = str::from_utf8(slice).expect("Invalid UTF-8");
// 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);
}
}