9797PAD_NORMAL = 1
9898PAD_PKCS5 = 2
9999
100+
100101# PAD_PKCS5: is a method that will unambiguously remove all padding
101102# characters after decryption, when originally encrypted with
102103# this padding mode.
@@ -219,7 +220,7 @@ def _unpadData(self, data, pad, padmode):
219220 pad = self .getPadding ()
220221 if pad :
221222 data = data [:- self .block_size ] + \
222- data [- self .block_size :].rstrip (pad )
223+ data [- self .block_size :].rstrip (pad )
223224
224225 elif padmode == PAD_PKCS5 :
225226 if _pythonMajorVersion < 3 :
@@ -248,6 +249,7 @@ def _guardAgainstUnicode(self, data):
248249 "pyDes can only work with encoded strings, not Unicode." )
249250 return data
250251
252+
251253#############################################################################
252254# DES #
253255#############################################################################
@@ -299,37 +301,37 @@ class des(_baseDes):
299301
300302 # permuted choice key (table 2)
301303 __pc2 = [
302- 13 , 16 , 10 , 23 , 0 , 4 ,
303- 2 , 27 , 14 , 5 , 20 , 9 ,
304- 22 , 18 , 11 , 3 , 25 , 7 ,
305- 15 , 6 , 26 , 19 , 12 , 1 ,
304+ 13 , 16 , 10 , 23 , 0 , 4 ,
305+ 2 , 27 , 14 , 5 , 20 , 9 ,
306+ 22 , 18 , 11 , 3 , 25 , 7 ,
307+ 15 , 6 , 26 , 19 , 12 , 1 ,
306308 40 , 51 , 30 , 36 , 46 , 54 ,
307309 29 , 39 , 50 , 44 , 32 , 47 ,
308310 43 , 48 , 38 , 55 , 33 , 52 ,
309311 45 , 41 , 49 , 35 , 28 , 31
310312 ]
311313
312314 # initial permutation IP
313- __ip = [57 , 49 , 41 , 33 , 25 , 17 , 9 , 1 ,
315+ __ip = [57 , 49 , 41 , 33 , 25 , 17 , 9 , 1 ,
314316 59 , 51 , 43 , 35 , 27 , 19 , 11 , 3 ,
315317 61 , 53 , 45 , 37 , 29 , 21 , 13 , 5 ,
316318 63 , 55 , 47 , 39 , 31 , 23 , 15 , 7 ,
317- 56 , 48 , 40 , 32 , 24 , 16 , 8 , 0 ,
319+ 56 , 48 , 40 , 32 , 24 , 16 , 8 , 0 ,
318320 58 , 50 , 42 , 34 , 26 , 18 , 10 , 2 ,
319321 60 , 52 , 44 , 36 , 28 , 20 , 12 , 4 ,
320322 62 , 54 , 46 , 38 , 30 , 22 , 14 , 6
321323 ]
322324
323325 # Expansion table for turning 32 bit blocks into 48 bits
324326 __expansion_table = [
325- 31 , 0 , 1 , 2 , 3 , 4 ,
326- 3 , 4 , 5 , 6 , 7 , 8 ,
327- 7 , 8 , 9 , 10 , 11 , 12 ,
327+ 31 , 0 , 1 , 2 , 3 , 4 ,
328+ 3 , 4 , 5 , 6 , 7 , 8 ,
329+ 7 , 8 , 9 , 10 , 11 , 12 ,
328330 11 , 12 , 13 , 14 , 15 , 16 ,
329331 15 , 16 , 17 , 18 , 19 , 20 ,
330332 19 , 20 , 21 , 22 , 23 , 24 ,
331333 23 , 24 , 25 , 26 , 27 , 28 ,
332- 27 , 28 , 29 , 30 , 31 , 0
334+ 27 , 28 , 29 , 30 , 31 , 0
333335 ]
334336
335337 # The (in)famous S-boxes
@@ -395,14 +397,14 @@ class des(_baseDes):
395397
396398 # final permutation IP^-1
397399 __fp = [
398- 39 , 7 , 47 , 15 , 55 , 23 , 63 , 31 ,
399- 38 , 6 , 46 , 14 , 54 , 22 , 62 , 30 ,
400- 37 , 5 , 45 , 13 , 53 , 21 , 61 , 29 ,
401- 36 , 4 , 44 , 12 , 52 , 20 , 60 , 28 ,
402- 35 , 3 , 43 , 11 , 51 , 19 , 59 , 27 ,
403- 34 , 2 , 42 , 10 , 50 , 18 , 58 , 26 ,
404- 33 , 1 , 41 , 9 , 49 , 17 , 57 , 25 ,
405- 32 , 0 , 40 , 8 , 48 , 16 , 56 , 24
400+ 39 , 7 , 47 , 15 , 55 , 23 , 63 , 31 ,
401+ 38 , 6 , 46 , 14 , 54 , 22 , 62 , 30 ,
402+ 37 , 5 , 45 , 13 , 53 , 21 , 61 , 29 ,
403+ 36 , 4 , 44 , 12 , 52 , 20 , 60 , 28 ,
404+ 35 , 3 , 43 , 11 , 51 , 19 , 59 , 27 ,
405+ 34 , 2 , 42 , 10 , 50 , 18 , 58 , 26 ,
406+ 33 , 1 , 41 , 9 , 49 , 17 , 57 , 25 ,
407+ 32 , 0 , 40 , 8 , 48 , 16 , 56 , 24
406408 ]
407409
408410 # Type of crypting being done
@@ -528,8 +530,8 @@ def __des_crypt(self, block, crypt_type):
528530 B = [self .R [:6 ], self .R [6 :12 ], self .R [12 :18 ], self .R [18 :24 ],
529531 self .R [24 :30 ], self .R [30 :36 ], self .R [36 :42 ], self .R [42 :]]
530532 # Optimization: Replaced below commented code with above
531- #j = 0
532- #B = []
533+ # j = 0
534+ # B = []
533535 # while j < len(self.R):
534536 # self.R[j] = self.R[j] ^ self.Kn[iteration][j]
535537 # j += 1
@@ -563,7 +565,7 @@ def __des_crypt(self, block, crypt_type):
563565 # Xor with L[i - 1]
564566 self .R = list (map (lambda x , y : x ^ y , self .R , self .L ))
565567 # Optimization: This now replaces the below commented code
566- #j = 0
568+ # j = 0
567569 # while j < len(self.R):
568570 # self.R[j] = self.R[j] ^ self.L[j]
569571 # j += 1
@@ -609,25 +611,25 @@ def crypt(self, data, crypt_type):
609611 i = 0
610612 dict = {}
611613 result = []
612- #cached = 0
613- #lines = 0
614+ # cached = 0
615+ # lines = 0
614616 while i < len (data ):
615617 # Test code for caching encryption results
616- #lines += 1
618+ # lines += 1
617619 # if dict.has_key(data[i:i+8]):
618620 # print "Cached result for: %s" % data[i:i+8]
619621 # cached += 1
620622 # result.append(dict[data[i:i+8]])
621623 # i += 8
622624 # continue
623625
624- block = self .__String_to_BitList (data [i :i + 8 ])
626+ block = self .__String_to_BitList (data [i :i + 8 ])
625627
626628 # Xor with IV if using CBC mode
627629 if self .getMode () == CBC :
628630 if crypt_type == des .ENCRYPT :
629631 block = list (map (lambda x , y : x ^ y , block , iv ))
630- #j = 0
632+ # j = 0
631633 # while j < len(block):
632634 # block[j] = block[j] ^ iv[j]
633635 # j += 1
@@ -637,7 +639,7 @@ def crypt(self, data, crypt_type):
637639 if crypt_type == des .DECRYPT :
638640 processed_block = list (
639641 map (lambda x , y : x ^ y , processed_block , iv ))
640- #j = 0
642+ # j = 0
641643 # while j < len(processed_block):
642644 # processed_block[j] = processed_block[j] ^ iv[j]
643645 # j += 1
@@ -648,10 +650,10 @@ def crypt(self, data, crypt_type):
648650 processed_block = self .__des_crypt (block , crypt_type )
649651
650652 # Add the resulting crypted block to our list
651- #d = self.__BitList_to_String(processed_block)
653+ # d = self.__BitList_to_String(processed_block)
652654 # result.append(d)
653655 result .append (self .__BitList_to_String (processed_block ))
654- #dict[data[i:i+8]] = d
656+ # dict[data[i:i+8]] = d
655657 i += 8
656658
657659 # print "Lines: %d, cached: %d" % (lines, cached)
@@ -811,7 +813,7 @@ def encrypt(self, data, pad=None, padmode=None):
811813 i = 0
812814 result = []
813815 while i < len (data ):
814- block = self .__key1 .crypt (data [i :i + 8 ], ENCRYPT )
816+ block = self .__key1 .crypt (data [i :i + 8 ], ENCRYPT )
815817 block = self .__key2 .crypt (block , DECRYPT )
816818 block = self .__key3 .crypt (block , ENCRYPT )
817819 self .__key1 .setIV (block )
@@ -856,8 +858,8 @@ def decrypt(self, data, pad=None, padmode=None):
856858 i = 0
857859 result = []
858860 while i < len (data ):
859- iv = data [i :i + 8 ]
860- block = self .__key3 .crypt (iv , DECRYPT )
861+ iv = data [i :i + 8 ]
862+ block = self .__key3 .crypt (iv , DECRYPT )
861863 block = self .__key2 .crypt (block , ENCRYPT )
862864 block = self .__key1 .crypt (block , DECRYPT )
863865 self .__key1 .setIV (iv )
0 commit comments