Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.58 KB | None | 0 0
  1. <?php
  2.  
  3. class DataEncryptionStandard {
  4. private $show_log = true;
  5. private $log = array();
  6. private $k = array();
  7. private $PC1 = array(57, 49, 41, 33, 25, 17, 9,
  8. 1, 58, 50, 42, 34, 26, 18,
  9. 10, 2, 59, 51, 43, 35, 27,
  10. 19, 11, 3, 60, 52, 44, 36,
  11. 63, 55, 47, 39, 31, 23, 15,
  12. 7, 62, 54, 46, 38, 30, 22,
  13. 14, 6, 61, 53, 45, 37, 29,
  14. 21, 13, 5, 28, 20, 12, 4);
  15.  
  16. private $PC2 = array(14, 17, 11, 24, 1, 5,
  17. 3, 28, 15, 6, 21, 10,
  18. 23, 19, 12, 4, 26, 8,
  19. 16, 7, 27, 20, 13, 2,
  20. 41, 52, 31, 37, 47, 55,
  21. 30, 40, 51, 45, 33, 48,
  22. 44, 49, 39, 56, 34, 53,
  23. 46, 42, 50, 36, 29, 32);
  24.  
  25. private $IP = array(58, 50, 42, 34, 26, 18, 10, 2,
  26. 60, 52, 44, 36, 28, 20, 12, 4,
  27. 62, 54, 46, 38, 30, 22, 14, 6,
  28. 64, 56, 48, 40, 32, 24, 16, 8,
  29. 57, 49, 41, 33, 25, 17, 9, 1,
  30. 59, 51, 43, 35, 27, 19, 11, 3,
  31. 61, 53, 45, 37, 29, 21, 13, 5,
  32. 63, 55, 47, 39, 31, 23, 15, 7);
  33.  
  34. private $IP1 = array(40, 8, 48, 16, 56, 24, 64, 32,
  35. 39, 7, 47, 15, 55, 23, 63, 31,
  36. 38, 6, 46, 14, 54, 22, 62, 30,
  37. 37, 5, 45, 13, 53, 21, 61, 29,
  38. 36, 4, 44, 12, 52, 20, 60, 28,
  39. 35, 3, 43, 11, 51, 19, 59, 27,
  40. 34, 2, 42, 10, 50, 18, 58, 26,
  41. 33, 1, 41, 9, 49, 17, 57, 25);
  42.  
  43. private $E = array(32, 1, 2, 3, 4, 5,
  44. 4, 5, 6, 7, 8, 9,
  45. 8, 9, 10, 11, 12, 13,
  46. 12, 13, 14, 15, 16, 17,
  47. 16, 17, 18, 19, 20, 21,
  48. 20, 21, 22, 23, 24, 25,
  49. 24, 25, 26, 27, 28, 29,
  50. 28, 29, 30, 31, 32, 1);
  51.  
  52. private $P = array(16, 7, 20, 21,
  53. 29, 12, 28, 17,
  54. 1, 15, 23, 26,
  55. 5, 18, 31, 10,
  56. 2, 8, 24, 14,
  57. 32, 27, 3, 9,
  58. 19, 13, 30, 6,
  59. 22, 11, 4, 25);
  60.  
  61. Private $r = array( 1 => 1,
  62. 2 => 1,
  63. 3 => 2,
  64. 4 => 2,
  65. 5 => 2,
  66. 6 => 2,
  67. 7 => 2,
  68. 8 => 2,
  69. 9 => 1,
  70. 10 => 2,
  71. 11 => 2,
  72. 12 => 2,
  73. 13 => 2,
  74. 14 => 2,
  75. 15 => 2,
  76. 16 => 1);
  77.  
  78. private function leftShift($keyPC1) {
  79. return substr($keyPC1, 1, (strlen($keyPC1) - 1)) . substr($keyPC1, 0, 1);
  80. }
  81.  
  82. private function permutation($array, $data) {
  83. $rs = '';
  84. foreach($array as $index) {
  85. $rs .= $data[$index-1];
  86. }
  87. return $rs;
  88. }
  89.  
  90. private function dataXOR($data1, $data2) {
  91. if(strlen($data1) != strlen($data2)) exit('xor err');
  92.  
  93. $rs = '';
  94. for($i = 0; $i < strlen($data1); $i++) {
  95. if(($data1[$i] == '1' or $data2[$i] == '1') and ($data1[$i] == '0' or $data2[$i] == '0')) $logic = '1';
  96. else $logic = '0';
  97. $rs .= $logic;
  98. }
  99. return $rs;
  100. }
  101.  
  102. private function sbox($EL) {
  103. $S = array(
  104. array(
  105. array(14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7),
  106. array(0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8),
  107. array(4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0),
  108. array(15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13),
  109. ),
  110. array(
  111. array(15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10),
  112. array(3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5),
  113. array(0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15),
  114. array(13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9),
  115. ),
  116. array(
  117. array(10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8),
  118. array(13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1),
  119. array(13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7),
  120. array(1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12),
  121. ),
  122. array(
  123. array(7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15),
  124. array(13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9),
  125. array(10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4),
  126. array(3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14),
  127. ),
  128. array(
  129. array(2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9),
  130. array(14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6),
  131. array(4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14),
  132. array(11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3),
  133. ),
  134. array(
  135. array(12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11),
  136. array(10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8),
  137. array(9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6),
  138. array(4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13),
  139. ),
  140. array(
  141. array(4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1),
  142. array(13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6),
  143. array(1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2),
  144. array(6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12),
  145. ),
  146. array(
  147. array(13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7),
  148. array(1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2),
  149. array(7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8),
  150. array(2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11),
  151. ),
  152. );
  153.  
  154. $BI = array();
  155. $it = 0;
  156. for($i = 0; $i < 8; $i++){
  157. $row = substr($EL, $it, 6);
  158.  
  159. $a = base_convert($row{0}.$row{5}, 2, 10);
  160. $b = base_convert(substr($row, 1, 4), 2, 10);
  161.  
  162. $BICurrent = base_convert($S[$i][$a][$b], 10, 2);
  163.  
  164. $BI[$i] = (strlen($BICurrent) < 4)? str_pad($BICurrent, 4, "0", STR_PAD_LEFT) : $BICurrent;
  165. $it += 6;
  166. }
  167.  
  168. return implode('', $BI);
  169. }
  170.  
  171. private function stringToHexToBin($key) {
  172. $binKey = '';
  173. for($i = 0; $i < strlen($key); $i++) {
  174. $num = ord($key{$i});
  175. $bin = base_convert($num, 10, 2);
  176. if(strlen($bin) < 8) $bin = str_pad($bin, 8, "0", STR_PAD_LEFT);
  177.  
  178. $binKey .= $bin;
  179. }
  180. return $binKey;
  181. }
  182.  
  183. private function stringToHex ($s) {
  184. $r = "";
  185. $hexes = array ("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
  186. for ($i = 0; $i < strlen($s); $i++) {
  187. $r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);
  188. }
  189. return $r;
  190. }
  191.  
  192. private function hexToBin($hex){
  193. $bin="";
  194. for ($i = 0; $i < strlen($hex); $i++){
  195. switch($hex{$i}){
  196. case "0" : $bin .= "0000"; break;
  197. case "1" : $bin .= "0001"; break;
  198. case "2" : $bin .= "0010"; break;
  199. case "3" : $bin .= "0011"; break;
  200. case "4" : $bin .= "0100"; break;
  201. case "5" : $bin .= "0101"; break;
  202. case "6" : $bin .= "0110"; break;
  203. case "7" : $bin .= "0111"; break;
  204. case "8" : $bin .= "1000"; break;
  205. case "9" : $bin .= "1001"; break;
  206. case "A" : $bin .= "1010"; break;
  207. case "B" : $bin .= "1011"; break;
  208. case "C" : $bin .= "1100"; break;
  209. case "D" : $bin .= "1101"; break;
  210. case "E" : $bin .= "1110"; break;
  211. case "F" : $bin .= "1111"; break;
  212. }
  213. }
  214. return $bin;
  215. }
  216.  
  217. private function generateKey($key){
  218. // Step 1 : Create 16 Subkeys
  219. $this->log['key']['key'] = $key;
  220. $this->log['key']['hexKey'] = $this->stringToHex($key); // string key to hex
  221. $binKey = $this->log['key']['binKey'] = $this->stringToHexToBin($key); // string key to hex then to binary
  222.  
  223. $keyPC1 = '';
  224. foreach($this->PC1 as $index) {
  225. // given K is permuted according to PC-1
  226. $this->log['key']['keyPermutation (K+)'] = $keyPC1 .= $binKey[$index-1];
  227. }
  228.  
  229. // Step 2 : K+ splitting
  230. $C[0] = substr($keyPC1, 0, 28);
  231. $D[0] = substr($keyPC1, 28, 28);
  232.  
  233. // step 3 : Creating 16 subkeys using shifting
  234. for($i = 1; $i <= 16; $i++) {
  235. $shiftC = $C[$i-1];
  236. $shiftD = $D[$i-1];
  237.  
  238. for($shiftIndex = 1; $shiftIndex <= $this->r[$i]; $shiftIndex++) {
  239. $shiftC = $this->leftShift($shiftC); // left shift Cn
  240. $shiftD = $this->leftShift($shiftD); // left shift Dn
  241. }
  242. $C[$i] = $shiftC;
  243. $D[$i] = $shiftD;
  244. }
  245.  
  246. // step 4 : PC-2 Permutation
  247. for($i = 1; $i <= 16; $i++) {
  248. $concat = $C[$i] . $D[$i];
  249.  
  250. $this->k[$i] = ''; // Kn (permuted according to the PC-2)
  251. foreach($this->PC2 as $index) {
  252. $this->k[$i] .= $concat[$index-1];
  253. }
  254. }
  255.  
  256. foreach($D as $id => $row) {
  257. $this->log['key']['C-' . $id] = $C[$id];
  258. $this->log['key']['D-' . $id] = $D[$id];
  259. if(isset($this->k[$id])) $this->log['key']['K-' . $id] = $this->k[$id];
  260. }
  261. }
  262.  
  263. private function encryptMessage($message){
  264. // Step 1: IP permutation
  265. $this->log['msg']['message'] = $message;
  266. $this->log['msg']['hexMsg'] = $this->stringToHex($message);
  267. $binMsg = $this->log['msg']['binMsg'] = $this->stringToHexToBin($message);
  268.  
  269. $sourceIP = '';
  270. foreach($this->IP as $IPIndex) {
  271. $this->log['msg']['msgIP'] = $sourceIP .= $binMsg[$IPIndex - 1];
  272. }
  273.  
  274. // Step 2 : IP splitting
  275. $L = $R = array();
  276. $L[0] = $this->log['msg']['L0'] = substr($sourceIP, 0, 32);
  277. $R[0] = $this->log['msg']['R0'] = substr($sourceIP, 32, 32);
  278.  
  279. // Step 3 : Iterations
  280. for($i = 1; $i <= 16; $i++) {
  281. // set Ln
  282. $L[$i] = $this->log['msg']['L' . $i] = $R[$i-1];
  283.  
  284. // Step 3.1 : E permutation
  285. $EL = $this->log['msg']['E(R' . ($i - 1) . ')'] = $this->permutation($this->E, $L[$i]);
  286.  
  287. // Step 3.2 : XOR with a subkey
  288. $this->log['msg']['K' . $i] = $this->k[$i];
  289. $this->log['msg']['K' . $i . ' xor E(R' . ($i - 1) . ')'] = $EL = $this->dataXOR($EL, $this->k[$i]);
  290.  
  291. // Step 3.3 : S box transformation
  292. $this->log['msg']['S(B1)S(B2)S(B3)S(B4)S(B5)S(B6)S(B7)S(B8) ' . $i] = $s = $this->sbox($EL);
  293.  
  294. // Step 3.4 : P permutation
  295. $this->log['msg']['f = P(S(B1)S(B2)S(B3)S(B4)S(B5)S(B6)S(B7)S(B8)) ' . $i] = $f = $this->permutation($this->P, $s);
  296.  
  297. // set Rn
  298. $this->log['msg']['R' . $i] = $R[$i] = $this->dataXOR($L[$i-1], $f);
  299. }
  300.  
  301. // Step 4 : Reverse Connecting
  302. $this->log['msg']['L16 concat R16'] = $concat = $R[16] . $L[16];
  303.  
  304. // Step 5 : IP-1 permutation
  305. $this->log['msg']['IP-1 permutation'] = $encoded = $this->permutation($this->IP1, $concat);
  306. $this->log['msg']['result'] = $result = '';
  307. for($start = 0; $start < strlen($encoded); $start += 4) {
  308. $this->log['msg']['result'] = $result .= strtoupper(base_convert(substr($encoded, $start, 4), 2, 16));
  309. }
  310.  
  311. // result with convert into hex
  312. return $result;
  313. }
  314.  
  315. public function encrypt($message, $key){
  316. {
  317. $this->generateKey($key);
  318. return $this->encryptMessage($message);
  319. }
  320. }
  321.  
  322. public function showLog(){
  323. echo '<pre>';
  324. print_r($this->log, false);
  325. echo '</pre>';
  326. }
  327. }
Add Comment
Please, Sign In to add comment