@@ -9,10 +9,8 @@ const KEY : i64 = 0x133457799BBCDFF1;
9
9
fn main ( ) {
10
10
let key_plus = generate_key_plus ( KEY ) ;
11
11
let ( left, right) = split_key ( key_plus, 56 ) ;
12
- let subkeys = create_16_subkeys ( left, right) ;
13
- for idx in 0 ..subkeys. len ( ) {
14
- println ! ( "[{0}]{1:b}\n [{0}]{2:b}\n \n " , idx, subkeys[ idx] . 0 , subkeys[ idx] . 1 ) ;
15
- }
12
+ let subkey_pairs = create_16_subkeys ( left, right) ;
13
+ let subkeys_48_bit = convert_pairs_to_encrypted_48_bit_keys ( subkey_pairs) ;
16
14
//println!("{:b}\n{:b}", left, right);
17
15
}
18
16
@@ -39,9 +37,10 @@ pub fn generate_key_plus(key: i64) -> i64 {
39
37
}
40
38
41
39
42
- pub fn split_key ( key : i64 , key_len : i8 ) -> ( i64 , i64 ) {
43
- let left_half = key >> key_len / 2 ;
44
- let right_half = ( key << 64 - key_len + key_len / 2 ) >> 64 - key_len + key_len / 2 ;
40
+ pub fn split_key ( key : i64 , key_len : u8 ) -> ( i64 , i64 ) {
41
+ let half_size = key_len / 2 ;
42
+ let left_half = ( key >> half_size) & bit_pattern_containing_ones ( half_size) ;
43
+ let right_half = key & bit_pattern_containing_ones ( half_size) ;
45
44
( left_half, right_half)
46
45
}
47
46
@@ -95,5 +94,33 @@ pub fn key_kn_from_pair(left: i64, right: i64) -> i64 {
95
94
96
95
97
96
pub fn convert_pairs_to_encrypted_48_bit_keys ( pairs : Vec < ( i64 , i64 ) > ) -> Vec < i64 > {
98
- Vec :: new ( )
97
+ let mut keys_48_bit : Vec < i64 > = Vec :: new ( ) ;
98
+ for idx in 0 ..pairs. len ( ) {
99
+ keys_48_bit. push ( key_kn_from_pair ( pairs[ idx] . 0 , pairs[ idx] . 1 ) ) ;
100
+ }
101
+ keys_48_bit
102
+ }
103
+
104
+
105
+
106
+ //Step 2: Encode each 64-bit block of data.
107
+ const IP : [ u8 ; 64 ] = [
108
+ 58 , 50 , 42 , 34 , 26 , 18 , 10 , 2 ,
109
+ 60 , 52 , 44 , 36 , 28 , 20 , 12 , 4 ,
110
+ 62 , 54 , 46 , 38 , 30 , 22 , 14 , 6 ,
111
+ 64 , 56 , 48 , 40 , 32 , 24 , 16 , 8 ,
112
+ 57 , 49 , 41 , 33 , 25 , 17 , 9 , 1 ,
113
+ 59 , 51 , 43 , 35 , 27 , 19 , 11 , 3 ,
114
+ 61 , 53 , 45 , 37 , 29 , 21 , 13 , 5 ,
115
+ 63 , 55 , 47 , 39 , 31 , 23 , 15 , 7
116
+ ] ;
117
+ //b = bit
118
+ pub fn initial_permutation_of_64bit_message ( message : i64 ) -> i64 {
119
+ let mut permutation = 0i64 ;
120
+ for idx in 0 ..64 {
121
+ let bit_at_index_in_message = ( message >> ( 64 - IP [ idx] ) ) & 1 ;
122
+ permutation = permutation << 1 ;
123
+ permutation = permutation | bit_at_index_in_message;
124
+ }
125
+ permutation
99
126
}
0 commit comments