Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.43 KB | None | 0 0
  1. #-*- coding: utf8 -*-
  2. import base64
  3. '''
  4. Sources :
  5. https://www.tutorialspoint.com/cryptography/data_encryption_standard.htm
  6. https://en.wikipedia.org/wiki/DES_supplementary_material
  7. http://homepage.usask.ca/~dtr467/400/
  8. '''
  9.  
  10. '''Constantes du DES'''
  11.  
  12. #Permutation initiale (IP)
  13.  
  14. PI = [58, 50, 42, 34, 26, 18, 10, 2,60, 52, 44, 36, 28, 20, 12, 4,62, 54, 46, 38, 30, 22, 14, 6,64, 56, 48, 40, 32, 24, 16, 8,57, 49, 41, 33, 25, 17, 9, 1,59, 51, 43, 35, 27, 19, 11, 3,61, 53, 45, 37, 29, 21, 13, 5,63, 55, 47, 39, 31, 23, 15, 7]
  15.  
  16. #Permutation finale (IP-1)
  17. PI_1 = [40, 8, 48, 16, 56, 24, 64, 32,39, 7, 47, 15, 55, 23, 63, 31,38, 6, 46, 14, 54, 22, 62, 30,37, 5, 45, 13, 53, 21, 61, 29,36, 4, 44, 12, 52, 20, 60, 28,35, 3, 43, 11, 51, 19, 59, 27,34, 2, 42, 10, 50, 18, 58, 26,33, 1, 41, 9, 49, 17, 57, 25]
  18.  
  19. #Développement (E) matrice d'expansion (expand)
  20. E = [32, 1, 2, 3, 4, 5,4, 5, 6, 7, 8, 9,8, 9, 10, 11, 12, 13,12, 13, 14, 15, 16, 17,16, 17, 18, 19, 20, 21,20, 21, 22, 23, 24, 25,24, 25, 26, 27, 28, 29,28, 29, 30, 31, 32, 1]
  21.  
  22. #Permutation (P)
  23. P = [16, 7, 20, 21, 29, 12, 28, 17,1, 15, 23, 26, 5, 18, 31, 10,2, 8, 24, 14, 32, 27, 3, 9,19, 13, 30, 6, 22, 11, 4, 25]
  24.      
  25. #Permuted choice 1 (PC-1)
  26. CP1 = [57, 49, 41, 33, 25, 17, 9,1, 58, 50, 42, 34, 26, 18,10, 2, 59, 51, 43, 35, 27,19, 11, 3, 60, 52, 44, 36,63, 55, 47, 39, 31, 23, 15,7, 62, 54, 46, 38, 30, 22,14, 6, 61, 53, 45, 37, 29,21, 13, 5, 28, 20, 12, 4]
  27.        
  28. #Permuted choice 2 (PC-2)
  29.  
  30. CP_2 = [14, 17, 11, 24, 1, 5, 3, 28,15, 6, 21, 10, 23, 19, 12, 4,26, 8, 16, 7, 27, 20, 13, 2,41, 52, 31, 37, 47, 55, 30, 40,51, 45, 33, 48, 44, 49, 39, 56,34, 53, 46, 42, 50, 36, 29, 32]
  31.        
  32. #Ces deux permutations PC1 et PC2 serviront à créer les 16 sous-clés de 48 bits qui seront utiles dans le réseau de Feistel.
  33.  
  34.  
  35. #Boîtes de substitution SBOX
  36. SBOX = [
  37.          
  38. [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
  39.  [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
  40.  [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
  41.  [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13],
  42. ],
  43.  
  44. [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
  45.  [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
  46.  [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
  47.  [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9],
  48. ],
  49.  
  50. [[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
  51.  [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
  52.  [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
  53.  [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12],
  54. ],
  55.  
  56. [[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
  57.  [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
  58.  [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
  59.  [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14],
  60. ],  
  61.  
  62. [[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
  63.  [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
  64.  [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
  65.  [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3],
  66. ],
  67.  
  68. [[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
  69.  [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
  70.  [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
  71.  [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13],
  72. ],
  73.  
  74. [[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
  75.  [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
  76.  [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
  77.  [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12],
  78. ],
  79.    
  80. [[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
  81.  [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
  82.  [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
  83.  [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11],
  84. ]
  85. ]
  86.  
  87.  
  88.  
  89. #Décalage dans la création des sous-clés selon le numero d'iteration (round)
  90. SHIFT = [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1]
  91.  
  92.  
  93.  
  94. '''Convertir une string à une liste de bits'''
  95. def stringToBit(text):
  96.     l = list()
  97.     for char in text:
  98.         binval = binvalue(char, 8)
  99.         l.extend([int(x) for x in list(binval)])
  100.     return l
  101.  
  102.  
  103. '''Convertir une liste de bits à une string'''
  104. def bitToString(liste):
  105.     result = ''.join([chr(int(j,2)) for j in [''.join([str(i) for i in bytes]) for bytes in  splitList(liste,8)]])
  106.     return result
  107.  
  108.  
  109. '''Diviser une liste à un ensemble de liste de taille n'''
  110. def splitList (lst, n):
  111.     it = iter(lst)
  112.     new = [[next(it) for _ in range(n)] for _ in range(len(lst) // n)]
  113.     for i, x in enumerate(it):
  114.         new[i].append(x)
  115.     return new
  116.  
  117.  
  118.  
  119. '''Donner la valeur binaire de taille n d'une string '''
  120. def binvalue(string, n):
  121.     binval = bin(string)[2:] if isinstance(string, int) else bin(ord(string))[2:]
  122.     while len(binval) < n:
  123.         binval = "0"+binval
  124.     return binval
  125.  
  126.  
  127.  
  128. '''Permuter un block en utilisant une liste donné en paramétre'''
  129. def permut(block, liste):#
  130.     p = []
  131.     for i in liste:
  132.         p.append(block[i - 1])
  133.     return p
  134.  
  135.  
  136.  
  137. '''Decalage gauche selon n bit'''
  138. def shift(g, d, n):
  139.     shifted = (g[n:] + g[:n], d[n:] + d[:n])
  140.     return shifted
  141.  
  142. '''Comme permuter juste une redefinition pour la clareté'''
  143. def expand(block, liste):#
  144.     p = []
  145.     for i in liste:
  146.         p.append(block[i - 1])
  147.     return p
  148.  
  149.  
  150. '''Fonction XOR'''
  151. def xor(t1, t2):
  152.     return [x^y for x,y in zip(t1,t2)]
  153.  
  154.  
  155. '''Substituer en utilisant les s-box 6 bits ==> 4 bits'''
  156. def substitute(de):
  157.     subblocks = splitList(de, 6)
  158.     result = list()
  159.     for i in range(len(subblocks)):
  160.         block = subblocks[i]
  161.         row = int(str(block[0])+str(block[5]),2)
  162.         column = int(''.join([str(x) for x in block[1:][:-1]]),2)
  163.         val = SBOX[i][row][column]
  164.         bin = binvalue(val, 4)
  165.         result += [int(x) for x in bin]
  166.     return result
  167.  
  168. '''PKCS5 padding longuer block = 8'''
  169. def addPadd(data):
  170.     pad_len = 8- (len(data)%8)
  171.     data += pad_len * chr(pad_len)
  172.     return data
  173.  
  174.  
  175. '''Supprimer le padding en cas d'utilisation'''
  176. def removePadd(data):
  177.     pad_len = ord(data[-1])
  178.     return data[:-pad_len]
  179.  
  180.  
  181. #liste pour sauvegarder les clés généreés
  182. keys = list()
  183.  
  184. '''Génération des clés'''
  185. def generate_keys(key):
  186.     key = stringToBit(key)
  187.     key = permut(key, CP1)
  188.     g, d = splitList(key, 28)
  189.     for i in range(16):
  190.         g, d = shift(g, d, SHIFT[i])
  191.         merge = g + d
  192.         permut2 = permut(merge, CP_2)
  193.         keys.append(permut2)
  194.  
  195.  
  196. '''Opération de chiffrement'''
  197. def encrypt(key ,text):
  198.     if padding:
  199.         text = addPadd(text)
  200.     # Generation des clés
  201.     generate_keys(key)
  202.  
  203.     text_blocks = splitList(text, 8)
  204.     print(text_blocks)
  205.     result = list()
  206.     for block in text_blocks:
  207.         block = stringToBit(block)
  208.         block = permut(block, PI)
  209.         g, d = splitList(block, 32)
  210.         for i in range(16):
  211.             de = expand(d, E)
  212.             t = xor(keys[i], de)
  213.  
  214.             t = substitute(t)
  215.             t = permut(t, P)
  216.             t = xor(g, t)
  217.             g = d
  218.             d = t
  219.         result += permut(d + g, PI_1)
  220.     finalRes = bitToString(result)
  221.     return finalRes
  222.  
  223.  
  224.    
  225. def cbc_enc(text,key,IV):
  226.    
  227.     f = ""
  228.  
  229.     text_blocks = splitList(text, 8)
  230.     for block in text_blocks:
  231.         block = stringToBit(block)
  232.  
  233.         print(block)
  234.         tmp = xor(block,IV)
  235.         c =encrypt(key,tmp)
  236.         f+=c
  237.         IV = c
  238.     return f
  239.  
  240.  
  241. test1 = list()
  242.  
  243. def cbc_dec(f,IV):
  244.     ol = ""
  245.     text_blocks = splitList(f, 8)
  246.     for block in text_blocks:
  247.         print(block)
  248.         a = decrypt(block)
  249.         tmp = xor(stringToBit(block),IV)
  250.         ol+=bitToString(tmp)
  251.         IV = block
  252.     return ol
  253.  
  254.    
  255.  
  256.  
  257.  
  258. '''Opération de déchiffrement'''
  259. def decrypt(text):
  260.     text_blocks = splitList(text, 8)
  261.     #print(text_blocks)
  262.     result = list()
  263.     for block in text_blocks:
  264.         block = stringToBit(block)
  265.         block = permut(block, PI)
  266.         g, d = splitList(block, 32)
  267.         for i in range(16):
  268.             de = expand(d, E)
  269.             t = xor(keys[15 - i], de)
  270.             t = substitute(t)
  271.             t = permut(t, P)
  272.             t = xor(g, t)
  273.             g = d
  274.             d = t
  275.         result += permut(d + g, PI_1)
  276.     finalRes = bitToString(result)
  277.     if padding:
  278.         return removePadd(finalRes)
  279.     else:
  280.         return finalRes
  281.  
  282.  
  283. key = "secret_k"
  284. text= "TESTTEST"
  285. IV = stringToBit('a')
  286. print(IV)
  287. padding = False
  288.  
  289. a = cbc_enc(text,key,IV)
  290. cipher = base64.b64encode(a)
  291. b = cbc_dec(a,IV)
  292.  
  293. print(a)
  294. print(b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement