diff --git a/des/src/block/block64.rs b/des/src/block/block64.rs index 0bb784f..29edb7c 100644 --- a/des/src/block/block64.rs +++ b/des/src/block/block64.rs @@ -22,18 +22,18 @@ impl Block64 { Self(u64::from_be_bytes(bytes)) } - #[inline] - #[must_use] - pub const fn to_le_bytes(self) -> [u8; 8] { - self.0.to_le_bytes() - } - #[inline] #[must_use] pub const fn to_be_bytes(self) -> [u8; 8] { self.0.to_be_bytes() } + #[inline] + #[must_use] + pub const fn to_le_bytes(self) -> [u8; 8] { + self.0.to_le_bytes() + } + #[inline] #[must_use] pub fn split_lr(self) -> LR { @@ -41,6 +41,12 @@ impl Block64 { } } +impl From<[u8; 8]> for Block64 { + fn from(bytes: [u8; 8]) -> Self { + Self::from_be_bytes(bytes) + } +} + impl From for Block64 { fn from(value: u64) -> Self { Self::new(value) @@ -65,12 +71,12 @@ impl From<&Block64> for LR { impl From for Vec { fn from(value: Block64) -> Self { - value.0.to_le_bytes().to_vec() + value.0.to_be_bytes().to_vec() } } impl From<&Block64> for Vec { fn from(value: &Block64) -> Self { - value.0.to_le_bytes().to_vec() + value.0.to_be_bytes().to_vec() } } diff --git a/des/src/constants.rs b/des/src/constants.rs index ef9d17c..4a69bc3 100644 --- a/des/src/constants.rs +++ b/des/src/constants.rs @@ -95,7 +95,7 @@ pub const S_BOXES: [[[u8; 16]; 4]; 8] = [ ], // S8 [ - [13, 2, 8, 4, 6, 15, 11, 1, 19, 9, 3, 14, 5, 0, 12, 7], + [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7], [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2], [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8], [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11], diff --git a/des/src/des.rs b/des/src/des.rs index e191cf8..fb7745b 100644 --- a/des/src/des.rs +++ b/des/src/des.rs @@ -58,6 +58,7 @@ where for subkey in subkeys { feistel(&mut lr, subkey); } + lr.swap(); lr.into() } diff --git a/des/src/key/des_key.rs b/des/src/key/des_key.rs index f849fdf..aa68dc1 100644 --- a/des/src/key/des_key.rs +++ b/des/src/key/des_key.rs @@ -44,7 +44,7 @@ impl AsRef<[u8]> for Key { impl From for Key { fn from(key: u64) -> Self { - Self(key.to_le_bytes()) + Self(key.to_be_bytes()) } } diff --git a/des/tests/des.rs b/des/tests/des.rs index 996582b..7f2789b 100644 --- a/des/tests/des.rs +++ b/des/tests/des.rs @@ -116,7 +116,7 @@ fn encrypt_decrypt_roundtrip( ) { let des = assert_ok!(Des::new(key), "Valid DES key"); - let ciphertext = assert_ok!(des.encrypt(&plaintext.to_le_bytes())); + let ciphertext = assert_ok!(des.encrypt(&plaintext.to_be_bytes())); let dectrypted = assert_ok!(des.decrypt(&ciphertext)); let re_ciphertext = assert_ok!(des.encrypt(&dectrypted)); @@ -126,7 +126,7 @@ fn encrypt_decrypt_roundtrip( assert_eq!( ciphertext_u64, expected_ciphertext, - "Encyption failed. Expected 0x{expected_ciphertext:016X}, got 0x{decrypted_u64:016X}" + "Encyption failed. Expected 0x{expected_ciphertext:016X}, got 0x{ciphertext_u64:016X}" ); assert_eq!( decrypted_u64, plaintext,