SDES.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include <iostream>
  2. using namespace std;
  3. int plain_text[8],ten_bit_key[10],ten_bit_key_after_applying_P10[10],P4[4],eight_bit_key_after_applying_initial_permutation[8],eight_bit_key_after_applying_expansion_permutation[8],ten_bit_key_after_applying_LS1[10],ten_bit_key_after_applying_LS2[10],Key1[8],Key2[8],P10[10],P8[8],initial_permutation[8],expansion_permutation[8],result_on_XOR_operation[8],S0_row,S0_column,S1_row,S1_column,result_from_S0_box,result_from_S1_box,four_bit_key_after_sbox[4],four_bits_of_ip[4],result_on_XOR_operation_after_P4[4],four_bit_key_after_applying_P4[4],cipher_text[8],decrypted_text[8],initial_permutation_inverse[8],* ptr,i,j;
  4. int S0[4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2} , S1[4][4]={0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3},binary_to_decimal[2][2]={0,1,2,3};
  5. void apply_P10()
  6. {
  7. for(i=0;i<10;i++)
  8. ten_bit_key_after_applying_P10[i]=ten_bit_key[P10[i]-1];
  9. }
  10. void apply_P4()
  11. {
  12. for(i=0;i<4;i++)
  13. four_bit_key_after_applying_P4[i]=four_bit_key_after_sbox[P4[i]-1];
  14. }
  15. void apply_initial_permutation()
  16. {
  17. for(i=0;i<8;i++)
  18. eight_bit_key_after_applying_initial_permutation[i]=plain_text[initial_permutation[i]-1];
  19. }
  20. void apply_initial_permutation_for_decryption()
  21. {
  22. for(i=0;i<8;i++)
  23. eight_bit_key_after_applying_initial_permutation[i]=ten_bit_key[initial_permutation[i]-1];
  24. }
  25. void apply_initial_permutation_inverse()
  26. {
  27. for(i=0;i<8;i++)
  28. {
  29. cipher_text[i]=eight_bit_key_after_applying_initial_permutation[initial_permutation_inverse[i]-1];
  30. decrypted_text[i]=cipher_text[i];
  31. }
  32. }
  33. void apply_expansion_permutation()
  34. {
  35. for(i=0;i<8;i++)
  36. eight_bit_key_after_applying_expansion_permutation[i]=eight_bit_key_after_applying_initial_permutation[expansion_permutation[i]-1+4];
  37. }
  38. int * perform_XOR_operation(int temp_Key1[8],int temp_Key2[8])
  39. {
  40. int result[8];
  41. for(i=0;i<8;i++)
  42. result[i]=temp_Key1[i]^temp_Key2[i];
  43. return result;
  44. }
  45. int * perform_XOR_operation_after_P4(int temp_Key1[4],int temp_Key2[4])
  46. {
  47. int result[4];
  48. for(i=0;i<4;i++)
  49. result[i]=temp_Key1[i]^temp_Key2[i];
  50. return result;
  51. }
  52. void apply_LS1()
  53. {
  54. for(i=0;i<4;i++)
  55. ten_bit_key_after_applying_LS1[i]=ten_bit_key_after_applying_P10[i+1];
  56. ten_bit_key_after_applying_LS1[4]=ten_bit_key_after_applying_P10[0];
  57. for(i=5;i<9;i++)
  58. ten_bit_key_after_applying_LS1[i]=ten_bit_key_after_applying_P10[i+1];
  59. ten_bit_key_after_applying_LS1[9]=ten_bit_key_after_applying_P10[5];
  60. }
  61. int * apply_P8(int temp[10],int size)
  62. {
  63. int Key[size];
  64. for(i=0;i<size;i++)
  65. Key[i]=temp[P8[i]-1];
  66. return Key;
  67. }
  68. void apply_LS2()
  69. {
  70. for(i=0;i<3;i++)
  71. ten_bit_key_after_applying_LS2[i]=ten_bit_key_after_applying_LS1[i+2];
  72. ten_bit_key_after_applying_LS2[3]=ten_bit_key_after_applying_LS1[0];
  73. ten_bit_key_after_applying_LS1[4]=ten_bit_key_after_applying_LS1[1];
  74. for(i=5;i<8;i++)
  75. ten_bit_key_after_applying_LS2[i]=ten_bit_key_after_applying_LS1[i+2];
  76. ten_bit_key_after_applying_LS2[8]=ten_bit_key_after_applying_LS1[5];
  77. ten_bit_key_after_applying_LS2[9]=ten_bit_key_after_applying_LS1[6];
  78. }
  79. int main() {
  80. cout<<"Enter the plain text\n";
  81. for(i=0;i<8;i++)
  82. cin>>plain_text[i];
  83. cout<<"Enter the ten bit key\n";
  84. for(i=0;i<10;i++)
  85. cin>>ten_bit_key[i];
  86. cout<<"Enter P10\n";
  87. for(i=0;i<10;i++)
  88. cin>>P10[i];
  89. cout<<"Enter the P8\n";
  90. for(i=0;i<8;i++)
  91. cin>>P8[i];
  92. cout<<"Enter the Initial Permutation\n";
  93. for(i=0;i<8;i++)
  94. cin>>initial_permutation[i];
  95. cout<<"Enter the Expansion Permutation\n";
  96. for(i=0;i<8;i++)
  97. cin>>expansion_permutation[i];
  98. cout<<"Enter P4\n";
  99. for(i=0;i<4;i++)
  100. cin>>P4[i];
  101. cout<<"Enter Initial Permutation Inverse\n";
  102. for(i=0;i<8;i++)
  103. cin>>initial_permutation_inverse[i];
  104. apply_P10();
  105. apply_LS1();
  106. ptr=apply_P8(ten_bit_key_after_applying_LS1,8);
  107. for(i=0;i<8;i++)
  108. Key1[i]=*(ptr+i);
  109. apply_LS2();
  110. ptr=apply_P8(ten_bit_key_after_applying_LS2,8);
  111. for(i=0;i<8;i++)
  112. Key2[i]=*(ptr+i);
  113. apply_initial_permutation();
  114. apply_expansion_permutation();
  115. ptr=perform_XOR_operation(eight_bit_key_after_applying_expansion_permutation,Key1);
  116. for(i=0;i<8;i++)
  117. result_on_XOR_operation[i]=*(ptr+i);
  118. S0_row=binary_to_decimal[result_on_XOR_operation[0]][result_on_XOR_operation[3]];
  119. S0_column=binary_to_decimal[result_on_XOR_operation[1]][result_on_XOR_operation[2]];
  120. S1_row=binary_to_decimal[result_on_XOR_operation[4]][result_on_XOR_operation[7]];
  121. S1_column=binary_to_decimal[result_on_XOR_operation[5]][result_on_XOR_operation[6]];
  122. result_from_S0_box=S0[S0_row][S0_column];
  123. result_from_S1_box=S1[S1_row][S1_column];
  124. for(i=0;i<2;i++)
  125. for(j=0;j<2;j++)
  126. if(result_from_S0_box==binary_to_decimal[i][j])
  127. {
  128. four_bit_key_after_sbox[0]=i;
  129. four_bit_key_after_sbox[1]=j;
  130. }
  131. for(i=0;i<2;i++)
  132. for(j=0;j<2;j++)
  133. if(result_from_S1_box==binary_to_decimal[i][j])
  134. {
  135. four_bit_key_after_sbox[2]=i;
  136. four_bit_key_after_sbox[3]=j;
  137. }
  138. apply_P4();
  139. for(i=0;i<4;i++)
  140. four_bits_of_ip[i]=eight_bit_key_after_applying_initial_permutation[i];
  141. ptr=perform_XOR_operation_after_P4(four_bit_key_after_applying_P4,four_bits_of_ip);
  142. for(i=0;i<4;i++)
  143. result_on_XOR_operation_after_P4[i]=*(ptr+i);
  144. for(i=4;i<8;i++)
  145. eight_bit_key_after_applying_initial_permutation[i-4]=eight_bit_key_after_applying_initial_permutation[i];
  146. for(i=4;i<8;i++)
  147. eight_bit_key_after_applying_initial_permutation[i]=result_on_XOR_operation_after_P4[i-4];
  148. apply_expansion_permutation();
  149. ptr= perform_XOR_operation(eight_bit_key_after_applying_expansion_permutation,Key2);
  150. for(i=0;i<8;i++)
  151. result_on_XOR_operation[i]=*(ptr+i);
  152. S0_row=binary_to_decimal[result_on_XOR_operation[0]][result_on_XOR_operation[3]];
  153. S0_column=binary_to_decimal[result_on_XOR_operation[1]][result_on_XOR_operation[2]];
  154. S1_row=binary_to_decimal[result_on_XOR_operation[4]][result_on_XOR_operation[7]];
  155. S1_column=binary_to_decimal[result_on_XOR_operation[5]][result_on_XOR_operation[6]];
  156. result_from_S0_box=S0[S0_row][S0_column];
  157. result_from_S1_box=S1[S1_row][S1_column];
  158. for(i=0;i<2;i++)
  159. for(j=0;j<2;j++)
  160. if(result_from_S0_box==binary_to_decimal[i][j])
  161. {
  162. four_bit_key_after_sbox[0]=i;
  163. four_bit_key_after_sbox[1]=j;
  164. }
  165. for(i=0;i<2;i++)
  166. for(j=0;j<2;j++)
  167. if(result_from_S1_box==binary_to_decimal[i][j])
  168. {
  169. four_bit_key_after_sbox[2]=i;
  170. four_bit_key_after_sbox[3]=j;
  171. }
  172. apply_P4();
  173. for(i=0;i<4;i++)
  174. four_bits_of_ip[i]=eight_bit_key_after_applying_initial_permutation[i];
  175. ptr=perform_XOR_operation_after_P4(four_bit_key_after_applying_P4,four_bits_of_ip);
  176. for(i=0;i<4;i++)
  177. result_on_XOR_operation_after_P4[i]=*(ptr+i);
  178. for(i=0;i<4;i++)
  179. eight_bit_key_after_applying_initial_permutation[i]=result_on_XOR_operation_after_P4[i];
  180. for(i=4;i<8;i++)
  181. eight_bit_key_after_applying_initial_permutation[i]=eight_bit_key_after_applying_initial_permutation[i];
  182. apply_initial_permutation_inverse();
  183. cout<<"\nCiphered Text is \n";
  184. for(i=0;i<8;i++)
  185. cout<<cipher_text[i];
  186. //-----------------------DECRYPTION STARTS HERE---------------
  187. for(i=0;i<8;i++)
  188. ten_bit_key[i]=cipher_text[i];
  189. apply_initial_permutation_for_decryption();
  190. apply_expansion_permutation();
  191. ptr=perform_XOR_operation(eight_bit_key_after_applying_expansion_permutation,Key2);
  192. for(i=0;i<8;i++)
  193. result_on_XOR_operation[i]=*(ptr+i);
  194. S0_row=binary_to_decimal[result_on_XOR_operation[0]][result_on_XOR_operation[3]];
  195. S0_column=binary_to_decimal[result_on_XOR_operation[1]][result_on_XOR_operation[2]];
  196. S1_row=binary_to_decimal[result_on_XOR_operation[4]][result_on_XOR_operation[7]];
  197. S1_column=binary_to_decimal[result_on_XOR_operation[5]][result_on_XOR_operation[6]];
  198. result_from_S0_box=S0[S0_row][S0_column];
  199. result_from_S1_box=S1[S1_row][S1_column];
  200. for(i=0;i<2;i++)
  201. for(j=0;j<2;j++)
  202. if(result_from_S0_box==binary_to_decimal[i][j])
  203. {
  204. four_bit_key_after_sbox[0]=i;
  205. four_bit_key_after_sbox[1]=j;
  206. }
  207. for(i=0;i<2;i++)
  208. for(j=0;j<2;j++)
  209. if(result_from_S1_box==binary_to_decimal[i][j])
  210. {
  211. four_bit_key_after_sbox[2]=i;
  212. four_bit_key_after_sbox[3]=j;
  213. }
  214. apply_P4();
  215. for(i=0;i<4;i++)
  216. four_bits_of_ip[i]=eight_bit_key_after_applying_initial_permutation[i];
  217. ptr=perform_XOR_operation_after_P4(four_bit_key_after_applying_P4,four_bits_of_ip);
  218. int temp_array[4];
  219. for(i=0;i<4;i++)
  220. {
  221. result_on_XOR_operation_after_P4[i]=*(ptr+i);
  222. temp_array[i]=result_on_XOR_operation_after_P4[i];
  223. }
  224. for(i=4;i<8;i++)
  225. eight_bit_key_after_applying_initial_permutation[i-4]=eight_bit_key_after_applying_initial_permutation[i];
  226. for(i=4;i<8;i++)
  227. eight_bit_key_after_applying_initial_permutation[i]=result_on_XOR_operation_after_P4[i-4];
  228. apply_expansion_permutation();
  229. ptr= perform_XOR_operation(eight_bit_key_after_applying_expansion_permutation,Key1);
  230. for(i=0;i<8;i++)
  231. result_on_XOR_operation[i]=*(ptr+i);
  232. S0_row=binary_to_decimal[result_on_XOR_operation[0]][result_on_XOR_operation[3]];
  233. S0_column=binary_to_decimal[result_on_XOR_operation[1]][result_on_XOR_operation[2]];
  234. S1_row=binary_to_decimal[result_on_XOR_operation[4]][result_on_XOR_operation[7]];
  235. S1_column=binary_to_decimal[result_on_XOR_operation[5]][result_on_XOR_operation[6]];
  236. result_from_S0_box=S0[S0_row][S0_column];
  237. result_from_S1_box=S1[S1_row][S1_column];
  238. for(i=0;i<2;i++)
  239. for(j=0;j<2;j++)
  240. if(result_from_S0_box==binary_to_decimal[i][j])
  241. {
  242. four_bit_key_after_sbox[0]=i;
  243. four_bit_key_after_sbox[1]=j;
  244. }
  245. for(i=0;i<2;i++)
  246. for(j=0;j<2;j++)
  247. if(result_from_S1_box==binary_to_decimal[i][j])
  248. {
  249. four_bit_key_after_sbox[2]=i;
  250. four_bit_key_after_sbox[3]=j;
  251. }
  252. apply_P4();
  253. for(i=0;i<4;i++)
  254. four_bits_of_ip[i]=eight_bit_key_after_applying_initial_permutation[i];
  255. ptr=perform_XOR_operation_after_P4(four_bit_key_after_applying_P4,four_bits_of_ip);
  256. for(i=0;i<4;i++)
  257. result_on_XOR_operation_after_P4[i]=*(ptr+i);
  258. for(i=0;i<4;i++)
  259. eight_bit_key_after_applying_initial_permutation[i]=result_on_XOR_operation_after_P4[i];
  260. for(i=4;i<8;i++)
  261. eight_bit_key_after_applying_initial_permutation[i]=temp_array[i-4];
  262. apply_initial_permutation_inverse();
  263. cout<<"\nDecrypted Text is \n";
  264. for(i=0;i<8;i++)
  265. cout<<decrypted_text[i];
  266. return 0;
  267. }
  268. /*
  269. OUTPUT:-
  270. Enter the plain text
  271. 1 0 1 0 0 1 0 1
  272. Enter the ten bit key
  273. 0 0 1 0 0 1 0 1 1 1
  274. Enter P10
  275. 3 5 2 7 4 10 1 9 8 6
  276. Enter the P8
  277. 6 3 7 4 8 5 10 9
  278. Enter the Initial Permutation
  279. 2 6 3 1 4 8 5 7
  280. Enter the Expansion Permutation
  281. 4 1 2 3 2 3 4 1
  282. Enter P4
  283. 2 4 3 1
  284. Enter Initial Permutation Inverse
  285. 4 1 3 5 7 2 8 6
  286. Ciphered Text is
  287. 00110110
  288. Decrypted Text is
  289. 10100101
  290. */