Advertisement
Onigiri10

DES шифрование

Nov 2nd, 2020 (edited)
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #перстановка бит (IP)
  2. a = [7,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,0,0,0,0,1,0,0,1,0,1,1,1]
  3. b = [7]
  4. rez = ""
  5.  
  6. b.insert(1,a[58]);b.insert(2,a[50]);b.insert(3,a[42])
  7. b.insert(4,a[34]);b.insert(5,a[26]);b.insert(6,a[18])
  8. b.insert(7,a[10]);b.insert(8,a[2]);
  9. b.insert(9,a[60]);b.insert(10,a[52]);b.insert(11,a[44]);
  10. b.insert(12,a[36]);b.insert(13,a[28]);b.insert(14,a[20]);
  11. b.insert(15,a[12]);b.insert(16,a[4]);
  12. b.insert(17,a[62]);b.insert(18,a[54]) ;b.insert(19,a[46]);
  13. b.insert(20,a[38]);b.insert(21,a[30]);b.insert(22,a[22]);
  14. b.insert(23,a[14]);b.insert(24,a[6])
  15. b.insert(25,a[64]);b.insert(26,a[56]);b.insert(27,a[48])
  16. b.insert(28,a[40]);b.insert(29,a[32]);b.insert(30,a[24])
  17. b.insert(31,a[16]);b.insert(32,a[8]);
  18. b.insert(33,a[57]);b.insert(34,a[49]);b.insert(35,a[41]);
  19. b.insert(36,a[33]);b.insert(37,a[25]);b.insert(38,a[17]);
  20. b.insert(39,a[9]);b.insert(40,a[1]);
  21. b.insert(41,a[59]);b.insert(42,a[51]);b.insert(43,a[43]);
  22. b.insert(44,a[35]);b.insert(45,a[27]); b.insert(46,a[19]);
  23. b.insert(47,a[11]);b.insert(48,a[3]);
  24. b.insert(49,a[61]);b.insert(50,a[53]);b.insert(51,a[45]);
  25. b.insert(52,a[37]);b.insert(53,a[29]); b.insert(54,a[21]);
  26. b.insert(55,a[13]);b.insert(56,a[5]);
  27. b.insert(57,a[63]);b.insert(58,a[55]);b.insert(59,a[47]);
  28. b.insert(60,a[39]);b.insert(61,a[31]); b.insert(62,a[23]);
  29. b.insert(63,a[15]);b.insert(64,a[7]);
  30.  
  31. for i in range(len(b)):
  32. rez += str(b[i])
  33. print(rez)
  34.  
  35. #Перестановка PC-1
  36. a = [7,0,1,0,0,0,1,1,0,1,0,1,0,0,1,1,1,1,1,0,1,0,0,1,1,1,0,0,0,1,0,1,0,0,1,0,1,0,1,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1,1,0,1,1,0,1,0,1,1,0,1]
  37. b = [7]
  38. rez = ""
  39.  
  40. b.insert(1,a[57]);b.insert(2,a[49]);b.insert(3,a[41])
  41. b.insert(4,a[33]);b.insert(5,a[25]);b.insert(6,a[17])
  42. b.insert(7,a[9]);
  43.  
  44. b.insert(8,a[1]);b.insert(9,a[58]);b.insert(10,a[50]);
  45. b.insert(11,a[42]);b.insert(12,a[34]);b.insert(13,a[26]);
  46. b.insert(14,a[18]);
  47.  
  48. b.insert(15,a[10]);b.insert(16,a[2]);b.insert(17,a[59]);
  49. b.insert(18,a[51]);b.insert(19,a[43]);b.insert(20,a[35]);
  50. b.insert(21,a[27]);
  51.  
  52. b.insert(22,a[19]);b.insert(23,a[11]);b.insert(24,a[3])
  53. b.insert(25,a[60]);b.insert(26,a[52]);b.insert(27,a[44])
  54. b.insert(28,a[36]);
  55.  
  56. b.insert(29,a[63]);b.insert(30,a[55]);b.insert(31,a[47]);
  57. b.insert(32,a[39]);b.insert(33,a[31]);b.insert(34,a[23]);
  58. b.insert(35,a[15]);
  59.  
  60. b.insert(36,a[7]);b.insert(37,a[62]);b.insert(38,a[54]);
  61. b.insert(39,a[46]);b.insert(40,a[38]); b.insert(41,a[30]);
  62. b.insert(42,a[22]);
  63.  
  64. b.insert(43,a[14]);b.insert(44,a[6]);b.insert(45,a[61]);
  65. b.insert(46,a[53]);b.insert(47,a[45]);b.insert(48,a[37]);
  66. b.insert(49,a[29]);
  67.  
  68. b.insert(50,a[21]);b.insert(51,a[13]);b.insert(52,a[5]);
  69. b.insert(53,a[28]);b.insert(54,a[20]);b.insert(55,a[12]);
  70. b.insert(56,a[4]);
  71.  
  72.  
  73. for i in range(len(b)):
  74. rez += str(b[i])
  75. print(rez)
  76.  
  77. #Перестановка PC-2
  78.  
  79. a = [7,1,1,0,0,0,1,0,1,0,1,1,1,0,0,0,0,1,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0,0,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1,1]
  80. b = [7]
  81. rez = ""
  82.  
  83. b.insert(1,a[14]);b.insert(2,a[17]);b.insert(3,a[11])
  84. b.insert(4,a[24]);b.insert(5,a[1]);b.insert(6,a[5])
  85. b.insert(7,a[3]);b.insert(8,a[28]);b.insert(9,a[15])
  86. b.insert(10,a[6]);b.insert(11,a[21]);b.insert(12,a[10])
  87. b.insert(13,a[23]);b.insert(14,a[19]);b.insert(15,a[12])
  88. b.insert(16,a[4]);b.insert(17,a[26]);b.insert(18,a[8])
  89. b.insert(19,a[16]); b.insert(20,a[7]);b.insert(21,a[27])
  90. b.insert(22,a[20]);b.insert(23,a[13]);b.insert(24,a[2])
  91. b.insert(25,a[41]);b.insert(26,a[52]);b.insert(27,a[31])
  92. b.insert(28,a[37]);b.insert(29,a[47]);b.insert(30,a[55])
  93. b.insert(31,a[30]);b.insert(32,a[40]);b.insert(33,a[51])
  94. b.insert(34,a[45]);b.insert(35,a[33]);b.insert(36,a[48])
  95. b.insert(37,a[44]);b.insert(38,a[49]);b.insert(39,a[39])
  96. b.insert(40,a[56]); b.insert(41,a[34]);b.insert(42,a[53])
  97. b.insert(43,a[46]);b.insert(44,a[42]);b.insert(45,a[50])
  98. b.insert(46,a[36]); b.insert(47,a[29]);b.insert(48,a[32])
  99.  
  100. for i in range(len(b)):
  101. rez += str(b[i])
  102. print(rez)
  103.  
  104. #Расширение подблока R:
  105.  
  106. a = [7,1,1,1,0,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0,1,0,0]
  107. b = [7]
  108. rez = ""
  109.  
  110. b.insert(1,a[32]);b.insert(2,a[1]);b.insert(3,a[2])
  111. b.insert(4,a[3]);b.insert(5,a[4]);b.insert(6,a[5])
  112. b.insert(7,a[4]);b.insert(8,a[5]);b.insert(9,a[6])
  113. b.insert(10,a[7]);b.insert(11,a[8]);b.insert(12,a[9])
  114. b.insert(13,a[8]);b.insert(14,a[9]);b.insert(15,a[10])
  115. b.insert(16,a[11]);b.insert(17,a[12]);b.insert(18,a[13])
  116. b.insert(19,a[12]); b.insert(20,a[13]);b.insert(21,a[14])
  117. b.insert(22,a[15]);b.insert(23,a[16]);b.insert(24,a[17])
  118. b.insert(25,a[16]);b.insert(26,a[17]);b.insert(27,a[18])
  119. b.insert(28,a[19]);b.insert(29,a[20]);b.insert(30,a[21])
  120. b.insert(31,a[20]);b.insert(32,a[21]);b.insert(33,a[22])
  121. b.insert(34,a[23]);b.insert(35,a[24]);b.insert(36,a[25])
  122. b.insert(37,a[24]);b.insert(38,a[25]);b.insert(39,a[26])
  123. b.insert(40,a[27]); b.insert(41,a[28]);b.insert(42,a[29])
  124. b.insert(43,a[28]);b.insert(44,a[29]);b.insert(45,a[30])
  125. b.insert(46,a[31]); b.insert(47,a[32]);b.insert(48,a[1])
  126.  
  127. for i in range(len(b)):
  128. rez += str(b[i])
  129. print(rez)
  130.  
  131.  
  132. #P-перестановка
  133.  
  134. a = [7,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,1,0,0,1,1,1,1,0,1,0,0,0,0,0,1,1,0]
  135. b = [7]
  136. rez = ""
  137.  
  138. b.insert(1,a[16]);b.insert(2,a[7]);b.insert(3,a[20])
  139. b.insert(4,a[21]);b.insert(5,a[29]);b.insert(6,a[12])
  140. b.insert(7,a[28]);b.insert(8,a[17]);b.insert(9,a[1])
  141. b.insert(10,a[15]);b.insert(11,a[23]);b.insert(12,a[26])
  142. b.insert(13,a[5]);b.insert(14,a[18]);b.insert(15,a[31])
  143. b.insert(16,a[10]);b.insert(17,a[2]);b.insert(18,a[8])
  144. b.insert(19,a[24]); b.insert(20,a[14]);b.insert(21,a[32])
  145. b.insert(22,a[27]);b.insert(23,a[3]);b.insert(24,a[9])
  146. b.insert(25,a[19]);b.insert(26,a[13]);b.insert(27,a[30])
  147. b.insert(28,a[6]);b.insert(29,a[22]);b.insert(30,a[11])
  148. b.insert(31,a[4]);b.insert(32,a[25]);
  149.  
  150. for i in range(len(b)):
  151. rez += str(b[i])
  152. print(rez)
  153.  
  154. #Инверсная перестановка
  155.  
  156. a = [7,1,0,1,0,1,1,0,0,0,0,1,0,1,1,0,0,1,1,0,0,1,0,1,1,1,0,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,0,1,0,0,1,0,1,0,1,1,0,0,0,0,1,1,0]
  157. b = [7]
  158. rez = ""
  159.  
  160. b.insert(1,a[40]);b.insert(2,a[8]);b.insert(3,a[48])
  161. b.insert(4,a[16]);b.insert(5,a[56]);b.insert(6,a[24])
  162. b.insert(7,a[64]);b.insert(8,a[32]);b.insert(9,a[39])
  163. b.insert(10,a[7]);b.insert(11,a[47]);b.insert(12,a[15])
  164. b.insert(13,a[55]);b.insert(14,a[23]);b.insert(15,a[63])
  165. b.insert(16,a[31]);b.insert(17,a[38]);b.insert(18,a[6])
  166. b.insert(19,a[46]); b.insert(20,a[14]);b.insert(21,a[54])
  167. b.insert(22,a[22]);b.insert(23,a[62]);b.insert(24,a[30])
  168. b.insert(25,a[37]);b.insert(26,a[5]);b.insert(27,a[45])
  169. b.insert(28,a[13]);b.insert(29,a[53]);b.insert(30,a[21])
  170. b.insert(31,a[61]);b.insert(32,a[29]);b.insert(33,a[36])
  171. b.insert(34,a[4]);b.insert(35,a[44]);b.insert(36,a[12])
  172. b.insert(37,a[52]);b.insert(38,a[20]);b.insert(39,a[60])
  173. b.insert(40,a[28]); b.insert(41,a[35]);b.insert(42,a[3])
  174. b.insert(43,a[43]);b.insert(44,a[11]);b.insert(45,a[51])
  175. b.insert(46,a[19]); b.insert(47,a[59]);b.insert(48,a[27])
  176. b.insert(37,a[34]);b.insert(38,a[2]);b.insert(39,a[42])
  177. b.insert(40,a[10]); b.insert(41,a[50]);b.insert(42,a[18])
  178. b.insert(43,a[58]);b.insert(44,a[26]);b.insert(45,a[33])
  179. b.insert(46,a[1]); b.insert(47,a[41]);b.insert(48,a[9])
  180. b.insert(45,a[49]);b.insert(46,a[17]); b.insert(47,a[57]);
  181. b.insert(48,a[25])
  182.  
  183. for i in range(len(b)):
  184. rez += str(b[i])
  185. print(rez)
  186.  
  187. #XOR
  188. a = "10010101"
  189. b = "01000010"
  190. y = int(a,2)^int(b,2)
  191.  
  192. print (bin(y)[2:].zfill(len(a)))
Advertisement
RAW Paste Data Copied
Advertisement