|
| 1 | +#This is a code is for solving data encryption standard (des) using python3 |
| 2 | + |
| 3 | +# Hexadecimal to binary conversion |
| 4 | + |
| 5 | +def hex2bin(s): |
| 6 | + mp = {'0': "0000", |
| 7 | + '1': "0001", |
| 8 | + '2': "0010", |
| 9 | + '3': "0011", |
| 10 | + '4': "0100", |
| 11 | + '5': "0101", |
| 12 | + '6': "0110", |
| 13 | + '7': "0111", |
| 14 | + '8': "1000", |
| 15 | + '9': "1001", |
| 16 | + 'A': "1010", |
| 17 | + 'B': "1011", |
| 18 | + 'C': "1100", |
| 19 | + 'D': "1101", |
| 20 | + 'E': "1110", |
| 21 | + 'F': "1111"} |
| 22 | + bin = "" |
| 23 | + for i in range(len(s)): |
| 24 | + bin = bin + mp[s[i]] |
| 25 | + return bin |
| 26 | + |
| 27 | +# Binary to hexadecimal conversion |
| 28 | + |
| 29 | + |
| 30 | +def bin2hex(s): |
| 31 | + mp = {"0000": '0', |
| 32 | + "0001": '1', |
| 33 | + "0010": '2', |
| 34 | + "0011": '3', |
| 35 | + "0100": '4', |
| 36 | + "0101": '5', |
| 37 | + "0110": '6', |
| 38 | + "0111": '7', |
| 39 | + "1000": '8', |
| 40 | + "1001": '9', |
| 41 | + "1010": 'A', |
| 42 | + "1011": 'B', |
| 43 | + "1100": 'C', |
| 44 | + "1101": 'D', |
| 45 | + "1110": 'E', |
| 46 | + "1111": 'F'} |
| 47 | + hex = "" |
| 48 | + for i in range(0, len(s), 4): |
| 49 | + ch = "" |
| 50 | + ch = ch + s[i] |
| 51 | + ch = ch + s[i + 1] |
| 52 | + ch = ch + s[i + 2] |
| 53 | + ch = ch + s[i + 3] |
| 54 | + hex = hex + mp[ch] |
| 55 | + |
| 56 | + return hex |
| 57 | + |
| 58 | +# Binary to decimal conversion |
| 59 | + |
| 60 | + |
| 61 | +def bin2dec(binary): |
| 62 | + |
| 63 | + binary1 = binary |
| 64 | + decimal, i, n = 0, 0, 0 |
| 65 | + while(binary != 0): |
| 66 | + dec = binary % 10 |
| 67 | + decimal = decimal + dec * pow(2, i) |
| 68 | + binary = binary//10 |
| 69 | + i += 1 |
| 70 | + return decimal |
| 71 | + |
| 72 | +# Decimal to binary conversion |
| 73 | + |
| 74 | + |
| 75 | +def dec2bin(num): |
| 76 | + res = bin(num).replace("0b", "") |
| 77 | + if(len(res) % 4 != 0): |
| 78 | + div = len(res) / 4 |
| 79 | + div = int(div) |
| 80 | + counter = (4 * (div + 1)) - len(res) |
| 81 | + for i in range(0, counter): |
| 82 | + res = '0' + res |
| 83 | + return res |
| 84 | + |
| 85 | +# Permute function to rearrange the bits |
| 86 | + |
| 87 | + |
| 88 | +def permute(k, arr, n): |
| 89 | + permutation = "" |
| 90 | + for i in range(0, n): |
| 91 | + permutation = permutation + k[arr[i] - 1] |
| 92 | + return permutation |
| 93 | + |
| 94 | +# shifting the bits towards left by nth shifts |
| 95 | + |
| 96 | + |
| 97 | +def shift_left(k, nth_shifts): |
| 98 | + s = "" |
| 99 | + for i in range(nth_shifts): |
| 100 | + for j in range(1, len(k)): |
| 101 | + s = s + k[j] |
| 102 | + s = s + k[0] |
| 103 | + k = s |
| 104 | + s = "" |
| 105 | + return k |
| 106 | + |
| 107 | +# calculating xow of two strings of binary number a and b |
| 108 | + |
| 109 | + |
| 110 | +def xor(a, b): |
| 111 | + ans = "" |
| 112 | + for i in range(len(a)): |
| 113 | + if a[i] == b[i]: |
| 114 | + ans = ans + "0" |
| 115 | + else: |
| 116 | + ans = ans + "1" |
| 117 | + return ans |
| 118 | + |
| 119 | + |
| 120 | +# Table of Position of 64 bits at initial level: Initial Permutation Table |
| 121 | +initial_perm = [58, 50, 42, 34, 26, 18, 10, 2, |
| 122 | + 60, 52, 44, 36, 28, 20, 12, 4, |
| 123 | + 62, 54, 46, 38, 30, 22, 14, 6, |
| 124 | + 64, 56, 48, 40, 32, 24, 16, 8, |
| 125 | + 57, 49, 41, 33, 25, 17, 9, 1, |
| 126 | + 59, 51, 43, 35, 27, 19, 11, 3, |
| 127 | + 61, 53, 45, 37, 29, 21, 13, 5, |
| 128 | + 63, 55, 47, 39, 31, 23, 15, 7] |
| 129 | + |
| 130 | +# Expansion D-box Table |
| 131 | +exp_d = [32, 1, 2, 3, 4, 5, 4, 5, |
| 132 | + 6, 7, 8, 9, 8, 9, 10, 11, |
| 133 | + 12, 13, 12, 13, 14, 15, 16, 17, |
| 134 | + 16, 17, 18, 19, 20, 21, 20, 21, |
| 135 | + 22, 23, 24, 25, 24, 25, 26, 27, |
| 136 | + 28, 29, 28, 29, 30, 31, 32, 1] |
| 137 | + |
| 138 | +# Straight Permutation Table |
| 139 | +per = [16, 7, 20, 21, |
| 140 | + 29, 12, 28, 17, |
| 141 | + 1, 15, 23, 26, |
| 142 | + 5, 18, 31, 10, |
| 143 | + 2, 8, 24, 14, |
| 144 | + 32, 27, 3, 9, |
| 145 | + 19, 13, 30, 6, |
| 146 | + 22, 11, 4, 25] |
| 147 | + |
| 148 | +# S-box Table |
| 149 | +sbox = [[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7], |
| 150 | + [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8], |
| 151 | + [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0], |
| 152 | + [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]], |
| 153 | + |
| 154 | + [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10], |
| 155 | + [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5], |
| 156 | + [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15], |
| 157 | + [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]], |
| 158 | + |
| 159 | + [[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8], |
| 160 | + [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1], |
| 161 | + [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7], |
| 162 | + [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]], |
| 163 | + |
| 164 | + [[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15], |
| 165 | + [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9], |
| 166 | + [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4], |
| 167 | + [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]], |
| 168 | + |
| 169 | + [[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9], |
| 170 | + [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6], |
| 171 | + [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14], |
| 172 | + [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]], |
| 173 | + |
| 174 | + [[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11], |
| 175 | + [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8], |
| 176 | + [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6], |
| 177 | + [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]], |
| 178 | + |
| 179 | + [[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1], |
| 180 | + [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6], |
| 181 | + [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2], |
| 182 | + [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]], |
| 183 | + |
| 184 | + [[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7], |
| 185 | + [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2], |
| 186 | + [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8], |
| 187 | + [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]] |
| 188 | + |
| 189 | +# Final Permutation Table |
| 190 | +final_perm = [40, 8, 48, 16, 56, 24, 64, 32, |
| 191 | + 39, 7, 47, 15, 55, 23, 63, 31, |
| 192 | + 38, 6, 46, 14, 54, 22, 62, 30, |
| 193 | + 37, 5, 45, 13, 53, 21, 61, 29, |
| 194 | + 36, 4, 44, 12, 52, 20, 60, 28, |
| 195 | + 35, 3, 43, 11, 51, 19, 59, 27, |
| 196 | + 34, 2, 42, 10, 50, 18, 58, 26, |
| 197 | + 33, 1, 41, 9, 49, 17, 57, 25] |
| 198 | + |
| 199 | + |
| 200 | +def encrypt(pt, rkb, rk): |
| 201 | + pt = hex2bin(pt) |
| 202 | + |
| 203 | + # Initial Permutation |
| 204 | + pt = permute(pt, initial_perm, 64) |
| 205 | + print("After initial permutation", bin2hex(pt)) |
| 206 | + |
| 207 | + # Splitting |
| 208 | + left = pt[0:32] |
| 209 | + right = pt[32:64] |
| 210 | + for i in range(0, 16): |
| 211 | + # Expansion D-box: Expanding the 32 bits data into 48 bits |
| 212 | + right_expanded = permute(right, exp_d, 48) |
| 213 | + |
| 214 | + # XOR RoundKey[i] and right_expanded |
| 215 | + xor_x = xor(right_expanded, rkb[i]) |
| 216 | + |
| 217 | + # S-boxex: substituting the value from s-box table by calculating row and column |
| 218 | + sbox_str = "" |
| 219 | + for j in range(0, 8): |
| 220 | + row = bin2dec(int(xor_x[j * 6] + xor_x[j * 6 + 5])) |
| 221 | + col = bin2dec( |
| 222 | + int(xor_x[j * 6 + 1] + xor_x[j * 6 + 2] + xor_x[j * 6 + 3] + xor_x[j * 6 + 4])) |
| 223 | + val = sbox[j][row][col] |
| 224 | + sbox_str = sbox_str + dec2bin(val) |
| 225 | + |
| 226 | + # Straight D-box: After substituting rearranging the bits |
| 227 | + sbox_str = permute(sbox_str, per, 32) |
| 228 | + |
| 229 | + # XOR left and sbox_str |
| 230 | + result = xor(left, sbox_str) |
| 231 | + left = result |
| 232 | + |
| 233 | + # Swapper |
| 234 | + if(i != 15): |
| 235 | + left, right = right, left |
| 236 | + print("Round ", i + 1, " ", bin2hex(left), |
| 237 | + " ", bin2hex(right), " ", rk[i]) |
| 238 | + |
| 239 | + # Combination |
| 240 | + combine = left + right |
| 241 | + |
| 242 | + # Final permutation: final rearranging of bits to get cipher text |
| 243 | + cipher_text = permute(combine, final_perm, 64) |
| 244 | + return cipher_text |
| 245 | + |
| 246 | + |
| 247 | +pt = "123456ABCD132536" |
| 248 | +key = "AABB09182736CCDD" |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | +# Key generation |
| 253 | +# --hex to binary |
| 254 | +key = hex2bin(key) |
| 255 | + |
| 256 | +# --parity bit drop table |
| 257 | +keyp = [57, 49, 41, 33, 25, 17, 9, |
| 258 | + 1, 58, 50, 42, 34, 26, 18, |
| 259 | + 10, 2, 59, 51, 43, 35, 27, |
| 260 | + 19, 11, 3, 60, 52, 44, 36, |
| 261 | + 63, 55, 47, 39, 31, 23, 15, |
| 262 | + 7, 62, 54, 46, 38, 30, 22, |
| 263 | + 14, 6, 61, 53, 45, 37, 29, |
| 264 | + 21, 13, 5, 28, 20, 12, 4] |
| 265 | + |
| 266 | +# getting 56 bit key from 64 bit using the parity bits |
| 267 | +key = permute(key, keyp, 56) |
| 268 | + |
| 269 | +# Number of bit shifts |
| 270 | +shift_table = [1, 1, 2, 2, |
| 271 | + 2, 2, 2, 2, |
| 272 | + 1, 2, 2, 2, |
| 273 | + 2, 2, 2, 1] |
| 274 | + |
| 275 | +# Key- Compression Table : Compression of key from 56 bits to 48 bits |
| 276 | +key_comp = [14, 17, 11, 24, 1, 5, |
| 277 | + 3, 28, 15, 6, 21, 10, |
| 278 | + 23, 19, 12, 4, 26, 8, |
| 279 | + 16, 7, 27, 20, 13, 2, |
| 280 | + 41, 52, 31, 37, 47, 55, |
| 281 | + 30, 40, 51, 45, 33, 48, |
| 282 | + 44, 49, 39, 56, 34, 53, |
| 283 | + 46, 42, 50, 36, 29, 32] |
| 284 | + |
| 285 | +# Splitting |
| 286 | +left = key[0:28] # rkb for RoundKeys in binary |
| 287 | +right = key[28:56] # rk for RoundKeys in hexadecimal |
| 288 | + |
| 289 | +rkb = [] |
| 290 | +rk = [] |
| 291 | +for i in range(0, 16): |
| 292 | + # Shifting the bits by nth shifts by checking from shift table |
| 293 | + left = shift_left(left, shift_table[i]) |
| 294 | + right = shift_left(right, shift_table[i]) |
| 295 | + |
| 296 | + # Combination of left and right string |
| 297 | + combine_str = left + right |
| 298 | + |
| 299 | + # Compression of key from 56 to 48 bits |
| 300 | + round_key = permute(combine_str, key_comp, 48) |
| 301 | + |
| 302 | + rkb.append(round_key) |
| 303 | + rk.append(bin2hex(round_key)) |
| 304 | + |
| 305 | +print("Encryption") |
| 306 | +cipher_text = bin2hex(encrypt(pt, rkb, rk)) |
| 307 | +print("Cipher Text : ", cipher_text) |
| 308 | + |
| 309 | +print("Decryption") |
| 310 | +rkb_rev = rkb[::-1] |
| 311 | +rk_rev = rk[::-1] |
| 312 | +text = bin2hex(encrypt(cipher_text, rkb_rev, rk_rev)) |
| 313 | +print("Plain Text : ", text) |
| 314 | + |
| 315 | + |
0 commit comments