MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / encrypt

Function encrypt

Python/des.py:200–244  ·  view source on GitHub ↗
(pt, rkb, rk)

Source from the content-addressed store, hash-verified

198
199
200def 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
247pt = "123456ABCD132536"

Callers 1

des.pyFile · 0.85

Calls 7

hex2binFunction · 0.85
permuteFunction · 0.85
bin2hexFunction · 0.85
xorFunction · 0.85
bin2decFunction · 0.85
dec2binFunction · 0.85
printFunction · 0.50

Tested by

no test coverage detected