hozer

[IS] DES Cryptosystem

Nov 20th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.25 KB | None | 0 0
  1. __author__ = 'rezoh'
  2.  
  3. class des():
  4.     # Permutation and translation tables for DES
  5.     __pc1 = [56, 48, 40, 32, 24, 16, 8,
  6.              0, 57, 49, 41, 33, 25, 17,
  7.              9, 1, 58, 50, 42, 34, 26,
  8.              18, 10, 2, 59, 51, 43, 35,
  9.              62, 54, 46, 38, 30, 22, 14,
  10.              6, 61, 53, 45, 37, 29, 21,
  11.              13, 5, 60, 52, 44, 36, 28,
  12.              20, 12, 4, 27, 19, 11, 3
  13.     ]
  14.  
  15.     # number left rotations of pc1
  16.     __left_rotations = [
  17.         1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  18.     ]
  19.  
  20.     # permuted choice key (table 2)
  21.     __pc2 = [
  22.         13, 16, 10, 23, 0, 4,
  23.         2, 27, 14, 5, 20, 9,
  24.         22, 18, 11, 3, 25, 7,
  25.         15, 6, 26, 19, 12, 1,
  26.         40, 51, 30, 36, 46, 54,
  27.         29, 39, 50, 44, 32, 47,
  28.         43, 48, 38, 55, 33, 52,
  29.         45, 41, 49, 35, 28, 31
  30.     ]
  31.  
  32.     # initial permutation IP
  33.     __ip = [57, 49, 41, 33, 25, 17, 9, 1,
  34.             59, 51, 43, 35, 27, 19, 11, 3,
  35.             61, 53, 45, 37, 29, 21, 13, 5,
  36.             63, 55, 47, 39, 31, 23, 15, 7,
  37.             56, 48, 40, 32, 24, 16, 8, 0,
  38.             58, 50, 42, 34, 26, 18, 10, 2,
  39.             60, 52, 44, 36, 28, 20, 12, 4,
  40.             62, 54, 46, 38, 30, 22, 14, 6
  41.     ]
  42.  
  43.     # Expansion table for turning 32 bit blocks into 48 bits
  44.     __expansion_table = [
  45.         31, 0, 1, 2, 3, 4,
  46.         3, 4, 5, 6, 7, 8,
  47.         7, 8, 9, 10, 11, 12,
  48.         11, 12, 13, 14, 15, 16,
  49.         15, 16, 17, 18, 19, 20,
  50.         19, 20, 21, 22, 23, 24,
  51.         23, 24, 25, 26, 27, 28,
  52.         27, 28, 29, 30, 31, 0
  53.     ]
  54.  
  55.     # The (in)famous S-boxes
  56.     __sbox = [  # S1
  57.                 [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  58.                  0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  59.                  4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  60.                  15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13],
  61.                  # S2
  62.                 [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  63.                  3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  64.                  0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  65.                  13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9],
  66.                  # S3
  67.                 [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  68.                  13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  69.                  13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  70.                  1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12],
  71.                  # S4
  72.                 [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  73.                  13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  74.                  10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  75.                  3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14],
  76.                  # S5
  77.                 [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  78.                  14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  79.                  4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  80.                  11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3],
  81.                  # S6
  82.                 [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  83.                  10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  84.                  9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  85.                  4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13],
  86.                  # S7
  87.                 [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  88.                  13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  89.                  1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  90.                  6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12],
  91.                  # S8
  92.                 [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  93.                  1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  94.                  7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  95.                  2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11],
  96.     ]
  97.  
  98.  
  99.     # 32-bit permutation function P used on the output of the S-boxes
  100.     __p = [
  101.         15, 6, 19, 20, 28, 11,
  102.         27, 16, 0, 14, 22, 25,
  103.         4, 17, 30, 9, 1, 7,
  104.         23, 13, 31, 26, 2, 8,
  105.         18, 12, 29, 5, 21, 10,
  106.         3, 24
  107.     ]
  108.  
  109.     # final permutation IP^-1
  110.     __fp = [
  111.         39, 7, 47, 15, 55, 23, 63, 31,
  112.         38, 6, 46, 14, 54, 22, 62, 30,
  113.         37, 5, 45, 13, 53, 21, 61, 29,
  114.         36, 4, 44, 12, 52, 20, 60, 28,
  115.         35, 3, 43, 11, 51, 19, 59, 27,
  116.         34, 2, 42, 10, 50, 18, 58, 26,
  117.         33, 1, 41, 9, 49, 17, 57, 25,
  118.         32, 0, 40, 8, 48, 16, 56, 24
  119.     ]
  120.  
  121.     # Type of crypting being done
  122.     ENCRYPT = 0x00
  123.     DECRYPT = 0x01
  124.  
  125.     # Initialisation
  126.     def __init__(self, key):
  127.         # Sanity checking of arguments.
  128.         self.block_size = 8 #block size
  129.         if len(key) != 8:
  130.             raise ValueError("Invalid DES key size. Key must be exactly 8 bytes long.")
  131.  
  132.         self.key_size = 8
  133.  
  134.         self.L = []
  135.         self.R = []
  136.         self.Kn = [[0] * 48] * 16  # 16 48-bit keys (K1 - K16)
  137.         self.final = []
  138.  
  139.         self.setKey(key)
  140.  
  141.     def setKey(self, key):
  142.         self.__key = key
  143.         self.__create_sub_keys()
  144.  
  145.  
  146.     def getKey(self):
  147.         return self.__key
  148.  
  149.  
  150.     def _padData(self, data):
  151.         # Pad data depending on the mode
  152.  
  153.         pad_len = 8 - (len(data) % self.block_size)
  154.         data += pad_len * chr(pad_len)
  155.  
  156.         return data
  157.  
  158.     def _unpadData(self, data,):
  159.         # Unpad data depending on the mode.
  160.         if not data:
  161.             return data
  162.  
  163.         pad_len = ord(data[-1])
  164.         data = data[:-pad_len]
  165.  
  166.         return data
  167.  
  168.     def __String_to_BitList(self, data):
  169.         data = [ord(c) for c in data]
  170.         l = len(data) * 8
  171.         result = [0] * l
  172.         pos = 0
  173.         for ch in data:
  174.             i = 7
  175.             while i >= 0:
  176.                 if ch & (1 << i) != 0:
  177.                     result[pos] = 1
  178.                 else:
  179.                     result[pos] = 0
  180.                 pos += 1
  181.                 i -= 1
  182.  
  183.         return result
  184.  
  185.     def __BitList_to_String(self, data):
  186.         result = []
  187.         pos = 0
  188.         c = 0
  189.         while pos < len(data):
  190.             c += data[pos] << (7 - (pos % 8))
  191.             if (pos % 8) == 7:
  192.                 result.append(c)
  193.                 c = 0
  194.             pos += 1
  195.  
  196.         return ''.join([chr(c) for c in result])
  197.  
  198.     def __permutate(self, table, block):
  199.         """Permutate this block with the specified table"""
  200.         return list(map(lambda x: block[x], table))
  201.  
  202.     # Transform the secret key, so that it is ready for data processing
  203.     # Create the 16 subkeys, K[1] - K[16]
  204.     def __create_sub_keys(self):
  205.         """Create the 16 subkeys K[1] to K[16] from the given key"""
  206.         key = self.__permutate(des.__pc1, self.__String_to_BitList(self.getKey()))
  207.         i = 0
  208.         # Split into Left and Right sections
  209.         self.L = key[:28]
  210.         self.R = key[28:]
  211.         while i < 16:
  212.             j = 0
  213.             # Perform circular left shifts
  214.             while j < des.__left_rotations[i]:
  215.                 self.L.append(self.L[0])
  216.                 del self.L[0]
  217.  
  218.                 self.R.append(self.R[0])
  219.                 del self.R[0]
  220.  
  221.                 j += 1
  222.  
  223.             # Create one of the 16 subkeys through pc2 permutation
  224.             self.Kn[i] = self.__permutate(des.__pc2, self.L + self.R)
  225.  
  226.             i += 1
  227.  
  228.  
  229.     def __des_crypt(self, block, crypt_type):
  230.         """Crypt the block bit-manipulation"""
  231.         block = self.__permutate(des.__ip, block)
  232.         self.L = block[:32]
  233.         self.R = block[32:]
  234.  
  235.         # Encryption starts from Kn[1] through to Kn[16]
  236.         if crypt_type == des.ENCRYPT:
  237.             iteration = 0
  238.             iteration_adjustment = 1
  239.         # Decryption starts from Kn[16] down to Kn[1]
  240.         else:
  241.             iteration = 15
  242.             iteration_adjustment = -1
  243.  
  244.         #crypt loop
  245.         i = 0
  246.         while i < 16:
  247.             tempR = self.R[:]
  248.             #first permutation
  249.             self.R = self.__permutate(des.__expansion_table, self.R)
  250.             #extended E E(R-1)
  251.             self.R = list(map(lambda x, y: x ^ y, self.R, self.Kn[iteration]))
  252.             #divide for 8 blocks
  253.             B = [self.R[:6], self.R[6:12], self.R[12:18], self.R[18:24], self.R[24:30], self.R[30:36], self.R[36:42],
  254.                  self.R[42:]]
  255.             j = 0
  256.             Bn = [0] * 32
  257.             pos = 0
  258.             #transform B(i) by S-permutation
  259.             while j < 8:
  260.                 m = (B[j][0] << 1) + B[j][5]
  261.                 n = (B[j][1] << 3) + (B[j][2] << 2) + (B[j][3] << 1) + B[j][4]
  262.  
  263.                 v = des.__sbox[j][(m << 4) + n]
  264.  
  265.                 Bn[pos] = (v & 8) >> 3
  266.                 Bn[pos + 1] = (v & 4) >> 2
  267.                 Bn[pos + 2] = (v & 2) >> 1
  268.                 Bn[pos + 3] = v & 1
  269.  
  270.                 pos += 4
  271.                 j += 1
  272.             self.R = self.__permutate(des.__p, Bn)
  273.             #swap blocks
  274.             self.R = list(map(lambda x, y: x ^ y, self.R, self.L))
  275.             self.L = tempR
  276.  
  277.             i += 1
  278.             iteration += iteration_adjustment
  279.  
  280.         #end permutate by P
  281.         self.final = self.__permutate(des.__fp, self.R + self.L)
  282.         return self.final
  283.  
  284.  
  285.     # Data to be encrypted/decrypted
  286.     def crypt(self, data, crypt_type):
  287.         """Crypt the data in blocks, running it through des_crypt()"""
  288.         i = 0
  289.         dict = {}
  290.         result = []
  291.         #devide text on blocks and encrypt
  292.         while i < len(data):
  293.  
  294.             block = self.__String_to_BitList(data[i:i + 8])
  295.  
  296.             processed_block = self.__des_crypt(block, crypt_type)
  297.  
  298.             result.append(self.__BitList_to_String(processed_block))
  299.             i += 8
  300.  
  301.         return ''.join(result)
  302.  
  303.  
  304.     def encrypt(self, data):
  305.         data = data
  306.         data = self._padData(data)
  307.         return self.crypt(data, des.ENCRYPT)
  308.  
  309.  
  310.     def decrypt(self, data):
  311.         data = data
  312.         data = self.crypt(data, des.DECRYPT)
  313.         return self._unpadData(data)
  314.  
  315.  
  316. decr = des("KEYKEYAK")
  317. d = decr.encrypt("OMG Im fuckin pretty good man!!")
  318. en = decr.decrypt(d)
  319. print(en)
Advertisement
Add Comment
Please, Sign In to add comment