Skip to content

Commit

Permalink
[feat] changes index of split on odd length, odd round messages
Browse files Browse the repository at this point in the history
haha!!!!! my theory was right !!
  • Loading branch information
o-klp committed Jun 12, 2015
1 parent e1f8d2f commit 791914c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/cipher.rs
Expand Up @@ -68,16 +68,23 @@ fn round_fn(right: Vec<u32>, subkey: u32) -> Vec<u32> {

pub fn feistel_decrypt(ciphertext: Vec<u32>, key: u32, rounds: u8) -> String {
let mut right: Vec<u32> = ciphertext.clone();
let mut left: Vec<u32> = right.split_off(ciphertext.len() / 2);
let split_index;
if (ciphertext.len() % 2 == 1) && (rounds % 2 == 1) {
// cipher text is odd, change index of split
split_index = (ciphertext.len() / 2) + 1;
} else {
split_index = ciphertext.len() / 2;
}
let mut left: Vec<u32> = right.split_off(split_index);
let mut subkey: u32;
let mut salty: u32;
let mut updated_left: Vec<u32>;
let mut updated_right: Vec<u32>;
// because encryption went from [0, rounds) decryption should
// generate subkeys for (rounds, 0]
for x in 1..rounds + 1 {
for x in 0..rounds {
// only difference in encryption & decryption is order of subkeys
salty = key.count_ones() + (rounds - x) as u32;
salty = key.count_ones() + (rounds - 1 - x) as u32;
subkey = key.wrapping_mul(salty);
// l[i] = R[i - 1]
updated_left = right.clone();
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -43,9 +43,9 @@ fn max_key_rounds() {
"The quick blue fox", 4294967295, 255);
let even_plaintext = feistel_decrypt(
even_ciphertext, 4294967295, 255);
assert_eq!("The quick brown fox", even_plaintext);
assert_eq!("The quick blue fox", even_plaintext);

let odd_ciphertext = feistel_encrypt("The quick blue fox", 4294967295, 255);
let odd_ciphertext = feistel_encrypt("The quick brown fox", 4294967295, 255);
let odd_plaintext = feistel_decrypt(odd_ciphertext, 4294967295, 255);
assert_eq!("The quick brown fox", odd_plaintext);
}
Expand Down

0 comments on commit 791914c

Please sign in to comment.