SDES.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. import java.util.Scanner;
  2. public class SDES {
  3. static int[] key1=new int[8];
  4. static int[] key2=new int[8];
  5. static int[] IP=new int[8];
  6. static int[] EP=new int[8];
  7. static int[] P4=new int[4];
  8. public static void main(String args[])
  9. {
  10. int i=0;
  11. int[] ptext=new int[8];
  12. int[] ctext=new int[8];
  13. int[] key=new int[10];
  14. int[] p10=new int[10];
  15. int[] p8=new int[10];
  16. Scanner sc=new Scanner(System.in);
  17. System.out.println("Enter the Plaintext");
  18. for(i=0;i<8;i++)
  19. ptext[i]=sc.nextInt();
  20. System.out.println("Enter the Key");
  21. for(i=0;i<10;i++)
  22. key[i]=sc.nextInt();
  23. System.out.println("Enter P10");
  24. for(i=0;i<10;i++)
  25. p10[i]=sc.nextInt();
  26. System.out.println("Enter P8");
  27. for(i=0;i<8;i++)
  28. p8[i]=sc.nextInt();
  29. makeKeys(p8,p10,key,ptext);
  30. ctext=encrypt(key,ptext);
  31. decrypt(key,ctext);
  32. }
  33. static void makeKeys(int[] p8,int[] p10,int[] key,int[] ptext)
  34. {
  35. int[] key_p10=new int[10];
  36. int temp1=0,temp2=0,i=0;
  37. //Apply p10 on the key[] array
  38. for(i=0;i<10;i++)
  39. {
  40. key_p10[i]=key[p10[i]-1];
  41. }
  42. //After applying p10, the key becomes:
  43. System.out.print("After applying p10, the key is ");
  44. for(i=0;i<10;i++)
  45. {
  46. System.out.print(key_p10[i]+" ");
  47. }
  48. //Apply LS1
  49. temp1=key_p10[0];
  50. for(i=0;i<4;i++)
  51. {
  52. key_p10[i]=key_p10[i+1];
  53. }
  54. key_p10[i]=temp1;
  55. temp1=key_p10[key_p10.length/2];
  56. for(i=key_p10.length/2;i<key_p10.length-1;i++)
  57. {
  58. key_p10[i]=key_p10[i+1];
  59. }
  60. key_p10[i]=temp1;
  61. System.out.print("\nAfter LS1, the key is ");
  62. for(i=0;i<10;i++)
  63. {
  64. System.out.print(key_p10[i]+" ");
  65. }
  66. //End of LS1
  67. //Applying P8
  68. for(i=0;i<8;i++)
  69. {
  70. key1[i]=key_p10[p8[i]-1];
  71. }
  72. System.out.print("\nKey1 is ");
  73. for(i=0;i<8;i++)
  74. {
  75. System.out.print(key1[i]+" ");
  76. }
  77. //End of P8
  78. //Apply LS2
  79. temp1=key_p10[0];
  80. temp2=key_p10[1];
  81. for(i=0;i<3;i++)
  82. {
  83. key_p10[i]=key_p10[i+2];
  84. }
  85. key_p10[i]=temp1;
  86. key_p10[i+1]=temp2;
  87. temp1=key_p10[key_p10.length/2];
  88. temp2=key_p10[(key_p10.length/2)+1];
  89. for(i=key_p10.length/2;i<key_p10.length-2;i++)
  90. {
  91. key_p10[i]=key_p10[i+2];
  92. }
  93. key_p10[i]=temp1;
  94. key_p10[i+1]=temp2;
  95. System.out.print("\nAfter LS2, the key is ");
  96. for(i=0;i<10;i++)
  97. {
  98. System.out.print(key_p10[i]+" ");
  99. }
  100. //End of LS2
  101. //Applying P8
  102. for(i=0;i<8;i++)
  103. {
  104. key2[i]=key_p10[p8[i]-1];
  105. }
  106. System.out.print("\nKey2 is ");
  107. for(i=0;i<8;i++)
  108. {
  109. System.out.print(key2[i]+" ");
  110. }
  111. //End of P8
  112. }
  113. static int[] encrypt(int[] key,int[] ptext) {
  114. Scanner sc=new Scanner(System.in);
  115. int i=0,r1=0,c1=0,r2=0,c2=0,s0_val=0,s1_val=0,temp=0;
  116. int[] ptext_ip=new int[8];
  117. int[] ptext_ep=new int[8];
  118. int[] sbox=new int[8];
  119. int[] sbox4bits=new int[4];
  120. int[] sbox4bits_p4=new int[4];
  121. int[] ptext_p4=new int[8];
  122. int[] sbox_p4=new int[8];
  123. int[] sbox_p4_bits=new int[8];
  124. int[] IP_Inverse=new int[8];
  125. int[] ctext=new int[8];
  126. int[] sbox4bits_p4_ep=new int[4];
  127. int[][] s0={
  128. {1,0,3,2},
  129. {3,2,1,0},
  130. {0,2,3,1},
  131. {3,1,3,2}
  132. };
  133. int[][] s1={
  134. {0,1,2,3},
  135. {2,0,1,3},
  136. {3,0,1,0},
  137. {2,1,0,3}
  138. };
  139. System.out.println("\nEnter Initial Permutation");
  140. for(i=0;i<8;i++)
  141. IP[i]=sc.nextInt();
  142. //Applying IP on PTEXT
  143. for(i=0;i<8;i++)
  144. ptext_ip[i]=ptext[IP[i]-1];
  145. //Hence ptext becomes:
  146. System.out.print("\nptext after IP is ");
  147. for(i=0;i<8;i++)
  148. {
  149. System.out.print(ptext_ip[i]+" ");
  150. }
  151. //Take Expansion permutation from user
  152. System.out.println("Enter Expansion Permutation");
  153. for(i=0;i<8;i++)
  154. EP[i]=sc.nextInt();
  155. //Applying EP on PTEXT
  156. for(i=0;i<8;i++)
  157. ptext_ep[i]=ptext_ip[EP[i]+4-1];
  158. //After EP
  159. System.out.print("\nptext after EP is ");
  160. for(i=0;i<8;i++)
  161. {
  162. System.out.print(ptext_ep[i]+" ");
  163. }
  164. //S-BOX
  165. for(i=0;i<8;i++)
  166. sbox[i]=ptext_ep[i]^key1[i];
  167. //The new value
  168. System.out.print("\nSbox value is ");
  169. for(i=0;i<8;i++)
  170. {
  171. System.out.print(sbox[i]+" ");
  172. }
  173. r1=sbox[0]*2+sbox[3];
  174. c1=sbox[1]*2+sbox[2];
  175. r2=sbox[4]*2+sbox[7];
  176. c2=sbox[5]*2+sbox[6];
  177. //print sbox
  178. System.out.println("\nSbox values are ");
  179. System.out.println("r1= "+r1+" c1="+c1+" r2="+r2+" c2="+c2);
  180. s0_val=s0[r1][c1];
  181. s1_val=s1[r2][c2];
  182. System.out.println("s0_val= "+s0_val+" s1_val="+s1_val);
  183. i=0;
  184. if(s0_val==0)
  185. {
  186. sbox4bits[0]=0;
  187. sbox4bits[1]=0;
  188. }
  189. else if(s0_val==1)
  190. {
  191. sbox4bits[0]=0;
  192. sbox4bits[1]=1;
  193. }
  194. else if(s0_val==2)
  195. {
  196. sbox4bits[0]=1;
  197. sbox4bits[1]=0;
  198. }
  199. else if(s0_val==3)
  200. {
  201. sbox4bits[0]=1;
  202. sbox4bits[1]=1;
  203. }
  204. //For s1
  205. if(s1_val==0)
  206. {
  207. sbox4bits[2]=0;
  208. sbox4bits[3]=0;
  209. }
  210. else if(s1_val==1)
  211. {
  212. sbox4bits[2]=0;
  213. sbox4bits[1]=1;
  214. }
  215. else if(s1_val==2)
  216. {
  217. sbox4bits[2]=1;
  218. sbox4bits[1]=0;
  219. }
  220. else if(s1_val==3)
  221. {
  222. sbox4bits[2]=1;
  223. sbox4bits[3]=1;
  224. }
  225. System.out.println("\nResulting 4 bits are ");
  226. for(i=0;i<4;i++)
  227. {
  228. System.out.print(sbox4bits[i]+" ");
  229. }
  230. //Take P4 from user
  231. System.out.println("Enter P4");
  232. for(i=0;i<4;i++)
  233. P4[i]=sc.nextInt();
  234. //Apply P4
  235. for(i=0;i<4;i++)
  236. sbox4bits_p4[i]=sbox4bits[P4[i]-1];
  237. /*System.out.println("\nResulting 4 bits after applying P4 are ");
  238. for(i=0;i<4;i++)
  239. {
  240. System.out.print(sbox4bits_p4[i]+" ");
  241. }*/
  242. for(i=0;i<8;i++)
  243. {
  244. if(i<4)
  245. {
  246. ptext_p4[i]=sbox4bits_p4[i]^ptext_ip[i];
  247. }
  248. else
  249. {
  250. ptext_p4[i]=ptext_ip[i];
  251. }
  252. }
  253. System.out.println("\nP4rd array is: ");
  254. for(i=0;i<8;i++)
  255. {
  256. System.out.print(ptext_p4[i]+" ");
  257. }
  258. //Swap the nibbles
  259. for(i=0;i<4;i++)
  260. {
  261. temp=ptext_p4[i];
  262. ptext_p4[i]=ptext_p4[i+4];
  263. ptext_p4[i+4]=temp;
  264. }
  265. System.out.println("\nThe swapped array is: ");
  266. for(i=0;i<8;i++)
  267. {
  268. System.out.print(ptext_p4[i]+" ");
  269. }
  270. //Applying EP on PTEXT
  271. for(i=0;i<8;i++)
  272. ptext_ep[i]=ptext_p4[EP[i]+4-1];
  273. //After EP
  274. System.out.print("\nptext after EP is ");
  275. for(i=0;i<8;i++)
  276. {
  277. System.out.print(ptext_ep[i]+" ");
  278. }
  279. //SBOX 2
  280. //S-BOX
  281. for(i=0;i<8;i++)
  282. sbox_p4[i]=ptext_ep[i]^key2[i];
  283. //The new value
  284. System.out.print("\nSbox value is ");
  285. for(i=0;i<8;i++)
  286. {
  287. System.out.print(sbox_p4[i]+" ");
  288. }
  289. r1=sbox_p4[0]*2+sbox_p4[3];
  290. c1=sbox_p4[1]*2+sbox_p4[2];
  291. r2=sbox_p4[4]*2+sbox_p4[7];
  292. c2=sbox_p4[5]*2+sbox_p4[6];
  293. //----------------------
  294. System.out.println("\nSbox values are ");
  295. System.out.println("r1= "+r1+" c1="+c1+" r2="+r2+" c2="+c2);
  296. s0_val=s0[r1][c1];
  297. s1_val=s1[r2][c2];
  298. System.out.println("s0_val= "+s0_val+" s1_val="+s1_val);
  299. i=0;
  300. for(i=0;i<4;i++)
  301. sbox4bits[i]=0;
  302. if(s0_val==0)
  303. {
  304. sbox4bits[0]=0;
  305. sbox4bits[1]=0;
  306. }
  307. else if(s0_val==1)
  308. {
  309. sbox4bits[0]=0;
  310. sbox4bits[1]=1;
  311. }
  312. else if(s0_val==2)
  313. {
  314. sbox4bits[0]=1;
  315. sbox4bits[1]=0;
  316. }
  317. else if(s0_val==3)
  318. {
  319. sbox4bits[0]=1;
  320. sbox4bits[1]=1;
  321. }
  322. //For s1
  323. if(s1_val==0)
  324. {
  325. sbox4bits[2]=0;
  326. sbox4bits[3]=0;
  327. }
  328. else if(s1_val==1)
  329. {
  330. sbox4bits[2]=0;
  331. sbox4bits[1]=1;
  332. }
  333. else if(s1_val==2)
  334. {
  335. sbox4bits[2]=1;
  336. sbox4bits[1]=0;
  337. }
  338. else if(s1_val==3)
  339. {
  340. sbox4bits[2]=1;
  341. sbox4bits[3]=1;
  342. }
  343. /*DON'T DELETE
  344. * while(s0_val > 0){
  345. sbox4bits[i++] = s0_val%2;
  346. s0_val = s0_val/2;
  347. }
  348. if(i==1)
  349. {
  350. temp=sbox4bits[0];
  351. sbox4bits[0]=sbox4bits[1];
  352. sbox4bits[1]=temp;
  353. i++;
  354. }
  355. while(s1_val > 0){
  356. sbox4bits[i++] = s1_val%2;
  357. s1_val = s1_val/2;
  358. }
  359. if(i==2)
  360. {
  361. temp=sbox4bits[2];
  362. sbox4bits[2]=sbox4bits[3];
  363. sbox4bits[3]=temp;
  364. i++;
  365. }*/
  366. System.out.println("\nResulting 4 bits are ");
  367. for(i=0;i<4;i++)
  368. {
  369. System.out.print(sbox4bits[i]+" ");
  370. }
  371. //XOR 4 bits of sbox4bits and 4 bits of plaintext_ip
  372. //Applying EP on sbox4bits_p4
  373. for(i=0;i<4;i++)
  374. sbox4bits_p4_ep[i]=sbox4bits[P4[i]-1];
  375. for(i=0;i<8;i++)
  376. {
  377. if(i<4)
  378. sbox_p4_bits[i]=sbox4bits_p4_ep[i]^ptext_p4[i];
  379. else
  380. sbox_p4_bits[i]=ptext_p4[i];
  381. }
  382. System.out.println("\nResulting 8 bits are ");
  383. for(i=0;i<8;i++)
  384. {
  385. System.out.print(sbox_p4_bits[i]+" ");
  386. }
  387. System.out.println("\nEnter IP Inverse");
  388. for(i=0;i<8;i++)
  389. IP_Inverse[i]=sc.nextInt();
  390. for(i=0;i<8;i++)
  391. ctext[i]=sbox_p4_bits[IP_Inverse[i]-1];
  392. System.out.println("IP_Inverse is ");
  393. for(i=0;i<8;i++)
  394. {
  395. System.out.print(IP_Inverse[i]+" ");
  396. }
  397. System.out.println("\nCiphertext is ");
  398. for(i=0;i<8;i++)
  399. {
  400. System.out.print(ctext[i]+" ");
  401. }
  402. return ctext;
  403. }
  404. static void decrypt(int[] key,int[] ctext) {
  405. Scanner sc=new Scanner(System.in);
  406. int i=0,r1=0,c1=0,r2=0,c2=0,s0_val=0,s1_val=0,temp=0;
  407. int[] ctext_ip=new int[8];
  408. int[] ctext_ep=new int[8];
  409. int[] sbox=new int[8];
  410. int[] sbox4bits=new int[4];
  411. int[] sbox4bits_p4=new int[4];
  412. int[] ctext_p4=new int[8];
  413. int[] sbox_p4=new int[8];
  414. int[] sbox_p4_bits=new int[8];
  415. int[] IP_Inverse=new int[8];
  416. int[] ptext=new int[8];
  417. int[] sbox4bits_p4_ep=new int[4];
  418. int[][] s0={
  419. {1,0,3,2},
  420. {3,2,1,0},
  421. {0,2,3,1},
  422. {3,1,3,2}
  423. };
  424. int[][] s1={
  425. {0,1,2,3},
  426. {2,0,1,3},
  427. {3,0,1,0},
  428. {2,1,0,3}
  429. };
  430. System.out.println("\nEnter Initial Permutation");
  431. for(i=0;i<8;i++)
  432. IP[i]=sc.nextInt();
  433. //Applying IP on ctext
  434. for(i=0;i<8;i++)
  435. ctext_ip[i]=ctext[IP[i]-1];
  436. //Hence ctext becomes:
  437. System.out.print("\nctext after IP is ");
  438. for(i=0;i<8;i++)
  439. {
  440. System.out.print(ctext_ip[i]+" ");
  441. }
  442. //Take Expansion permutation from user
  443. System.out.println("Enter Expansion Permutation");
  444. for(i=0;i<8;i++)
  445. EP[i]=sc.nextInt();
  446. //Applying EP on ctext
  447. for(i=0;i<8;i++)
  448. ctext_ep[i]=ctext_ip[EP[i]+4-1];
  449. //After EP
  450. System.out.print("\nctext after EP is ");
  451. for(i=0;i<8;i++)
  452. {
  453. System.out.print(ctext_ep[i]+" ");
  454. }
  455. //S-BOX
  456. for(i=0;i<8;i++)
  457. sbox[i]=ctext_ep[i]^key2[i];
  458. //The new value
  459. System.out.print("\nSbox value is ");
  460. for(i=0;i<8;i++)
  461. {
  462. System.out.print(sbox[i]+" ");
  463. }
  464. r1=sbox[0]*2+sbox[3];
  465. c1=sbox[1]*2+sbox[2];
  466. r2=sbox[4]*2+sbox[7];
  467. c2=sbox[5]*2+sbox[6];
  468. //print sbox
  469. System.out.println("\nSbox values are ");
  470. System.out.println("r1= "+r1+" c1="+c1+" r2="+r2+" c2="+c2);
  471. s0_val=s0[r1][c1];
  472. s1_val=s1[r2][c2];
  473. System.out.println("s0_val= "+s0_val+" s1_val="+s1_val);
  474. i=0;
  475. if(s0_val==0)
  476. {
  477. sbox4bits[0]=0;
  478. sbox4bits[1]=0;
  479. }
  480. else if(s0_val==1)
  481. {
  482. sbox4bits[0]=0;
  483. sbox4bits[1]=1;
  484. }
  485. else if(s0_val==2)
  486. {
  487. sbox4bits[0]=1;
  488. sbox4bits[1]=0;
  489. }
  490. else if(s0_val==3)
  491. {
  492. sbox4bits[0]=1;
  493. sbox4bits[1]=1;
  494. }
  495. //For s1
  496. if(s1_val==0)
  497. {
  498. sbox4bits[2]=0;
  499. sbox4bits[3]=0;
  500. }
  501. else if(s1_val==1)
  502. {
  503. sbox4bits[2]=0;
  504. sbox4bits[1]=1;
  505. }
  506. else if(s1_val==2)
  507. {
  508. sbox4bits[2]=1;
  509. sbox4bits[1]=0;
  510. }
  511. else if(s1_val==3)
  512. {
  513. sbox4bits[2]=1;
  514. sbox4bits[3]=1;
  515. }
  516. System.out.println("\nResulting 4 bits are ");
  517. for(i=0;i<4;i++)
  518. {
  519. System.out.print(sbox4bits[i]+" ");
  520. }
  521. //Take P4 from user
  522. System.out.println("Enter P4");
  523. for(i=0;i<4;i++)
  524. P4[i]=sc.nextInt();
  525. //Apply P4
  526. for(i=0;i<4;i++)
  527. sbox4bits_p4[i]=sbox4bits[P4[i]-1];
  528. /*System.out.println("\nResulting 4 bits after applying P4 are ");
  529. for(i=0;i<4;i++)
  530. {
  531. System.out.print(sbox4bits_p4[i]+" ");
  532. }*/
  533. for(i=0;i<8;i++)
  534. {
  535. if(i<4)
  536. {
  537. ctext_p4[i]=sbox4bits_p4[i]^ctext_ip[i];
  538. }
  539. else
  540. {
  541. ctext_p4[i]=ctext_ip[i];
  542. }
  543. }
  544. System.out.println("\nP4rd array is: ");
  545. for(i=0;i<8;i++)
  546. {
  547. System.out.print(ctext_p4[i]+" ");
  548. }
  549. //Swap the nibbles
  550. for(i=0;i<4;i++)
  551. {
  552. temp=ctext_p4[i];
  553. ctext_p4[i]=ctext_p4[i+4];
  554. ctext_p4[i+4]=temp;
  555. }
  556. System.out.println("\nThe swapped array is: ");
  557. for(i=0;i<8;i++)
  558. {
  559. System.out.print(ctext_p4[i]+" ");
  560. }
  561. //Applying EP on ctext
  562. for(i=0;i<8;i++)
  563. ctext_ep[i]=ctext_p4[EP[i]+4-1];
  564. //After EP
  565. System.out.print("\nctext after EP is ");
  566. for(i=0;i<8;i++)
  567. {
  568. System.out.print(ctext_ep[i]+" ");
  569. }
  570. //SBOX 2
  571. //S-BOX
  572. for(i=0;i<8;i++)
  573. sbox_p4[i]=ctext_ep[i]^key1[i];
  574. //The new value
  575. System.out.print("\nSbox value is ");
  576. for(i=0;i<8;i++)
  577. {
  578. System.out.print(sbox_p4[i]+" ");
  579. }
  580. r1=sbox_p4[0]*2+sbox_p4[3];
  581. c1=sbox_p4[1]*2+sbox_p4[2];
  582. r2=sbox_p4[4]*2+sbox_p4[7];
  583. c2=sbox_p4[5]*2+sbox_p4[6];
  584. //----------------------
  585. System.out.println("\nSbox values are ");
  586. System.out.println("r1= "+r1+" c1="+c1+" r2="+r2+" c2="+c2);
  587. s0_val=s0[r1][c1];
  588. s1_val=s1[r2][c2];
  589. System.out.println("s0_val= "+s0_val+" s1_val="+s1_val);
  590. i=0;
  591. for(i=0;i<4;i++)
  592. sbox4bits[i]=0;
  593. if(s0_val==0)
  594. {
  595. sbox4bits[0]=0;
  596. sbox4bits[1]=0;
  597. }
  598. else if(s0_val==1)
  599. {
  600. sbox4bits[0]=0;
  601. sbox4bits[1]=1;
  602. }
  603. else if(s0_val==2)
  604. {
  605. sbox4bits[0]=1;
  606. sbox4bits[1]=0;
  607. }
  608. else if(s0_val==3)
  609. {
  610. sbox4bits[0]=1;
  611. sbox4bits[1]=1;
  612. }
  613. //For s1
  614. if(s1_val==0)
  615. {
  616. sbox4bits[2]=0;
  617. sbox4bits[3]=0;
  618. }
  619. else if(s1_val==1)
  620. {
  621. sbox4bits[2]=0;
  622. sbox4bits[1]=1;
  623. }
  624. else if(s1_val==2)
  625. {
  626. sbox4bits[2]=1;
  627. sbox4bits[1]=0;
  628. }
  629. else if(s1_val==3)
  630. {
  631. sbox4bits[2]=1;
  632. sbox4bits[3]=1;
  633. }
  634. /*DON'T DELETE
  635. * while(s0_val > 0){
  636. sbox4bits[i++] = s0_val%2;
  637. s0_val = s0_val/2;
  638. }
  639. if(i==1)
  640. {
  641. temp=sbox4bits[0];
  642. sbox4bits[0]=sbox4bits[1];
  643. sbox4bits[1]=temp;
  644. i++;
  645. }
  646. while(s1_val > 0){
  647. sbox4bits[i++] = s1_val%2;
  648. s1_val = s1_val/2;
  649. }
  650. if(i==2)
  651. {
  652. temp=sbox4bits[2];
  653. sbox4bits[2]=sbox4bits[3];
  654. sbox4bits[3]=temp;
  655. i++;
  656. }*/
  657. System.out.println("\nResulting 4 bits are ");
  658. for(i=0;i<4;i++)
  659. {
  660. System.out.print(sbox4bits[i]+" ");
  661. }
  662. //XOR 4 bits of sbox4bits and 4 bits of plaintext_ip
  663. for(i=0;i<4;i++)
  664. sbox4bits_p4_ep[i]=sbox4bits[P4[i]-1];
  665. for(i=0;i<8;i++)
  666. {
  667. if(i<4)
  668. sbox_p4_bits[i]=sbox4bits_p4_ep[i]^ctext_p4[i];
  669. else
  670. sbox_p4_bits[i]=ctext_p4[i];
  671. }
  672. System.out.println("\nResulting 8 bits are ");
  673. for(i=0;i<8;i++)
  674. {
  675. System.out.print(sbox_p4_bits[i]+" ");
  676. }
  677. System.out.println("\nEnter IP Inverse");
  678. for(i=0;i<8;i++)
  679. IP_Inverse[i]=sc.nextInt();
  680. for(i=0;i<8;i++)
  681. ptext[i]=sbox_p4_bits[IP_Inverse[i]-1];
  682. System.out.println("IP_Inverse is ");
  683. for(i=0;i<8;i++)
  684. {
  685. System.out.print(IP_Inverse[i]+" ");
  686. }
  687. System.out.println("\nPlaintext is ");
  688. for(i=0;i<8;i++)
  689. {
  690. System.out.print(ptext[i]+" ");
  691. }
  692. }
  693. }
  694. /*
  695. Enter the Plaintext
  696. 1 0 1 0 0 1 0 1
  697. Enter the Key
  698. 0 0 1 0 0 1 0 1 1 1
  699. Enter P10
  700. 3 5 2 7 4 10 1 9 8 6
  701. Enter P8
  702. 6 3 7 4 8 5 10 9
  703. After applying p10, the key is 1 0 0 0 0 1 0 1 1 1
  704. After LS1, the key is 0 0 0 0 1 0 1 1 1 1
  705. Key1 is 0 0 1 0 1 1 1 1
  706. After LS2, the key is 0 0 1 0 0 1 1 1 0 1
  707. Key2 is 1 1 1 0 1 0 1 0
  708. Enter Initial Permutation
  709. 2 6 3 1 4 8 5 7
  710. ptext after IP is 0 1 1 1 0 1 0 0 Enter Expansion Permutation
  711. 4 1 2 3 2 3 4 1
  712. ptext after EP is 0 0 1 0 1 0 0 0
  713. Sbox value is 0 0 0 0 0 1 1 1
  714. Sbox values are
  715. r1= 0 c1=0 r2=1 c2=3
  716. s0_val= 1 s1_val=3
  717. Resulting 4 bits are
  718. 0 1 1 1 Enter P4
  719. 2 4 3 1
  720. P4rd array is:
  721. 1 0 0 1 0 1 0 0
  722. The swapped array is:
  723. 0 1 0 0 1 0 0 1
  724. ptext after EP is 1 1 0 0 0 0 1 1
  725. Sbox value is 0 0 1 0 1 0 0 1
  726. Sbox values are
  727. r1= 0 c1=1 r2=3 c2=0
  728. s0_val= 0 s1_val=2
  729. Resulting 4 bits are
  730. 0 0 1 0
  731. Resulting 8 bits are
  732. 0 1 1 0 1 0 0 1
  733. Enter IP Inverse
  734. 4 1 3 5 7 2 8 6
  735. IP_Inverse is
  736. 4 1 3 5 7 2 8 6
  737. Ciphertext is
  738. 0 0 1 1 0 1 1 0
  739. Enter Initial Permutation
  740. 2 6 3 1 4 8 5 7
  741. ctext after IP is 0 1 1 0 1 0 0 1 Enter Expansion Permutation
  742. 4 1 2 3 2 3 4 1
  743. ctext after EP is 1 1 0 0 0 0 1 1
  744. Sbox value is 0 0 1 0 1 0 0 1
  745. Sbox values are
  746. r1= 0 c1=1 r2=3 c2=0
  747. s0_val= 0 s1_val=2
  748. Resulting 4 bits are
  749. 0 0 1 0 Enter P4
  750. 2 4 3 1
  751. P4rd array is:
  752. 0 1 0 0 1 0 0 1
  753. The swapped array is:
  754. 1 0 0 1 0 1 0 0
  755. ctext after EP is 0 0 1 0 1 0 0 0
  756. Sbox value is 0 0 0 0 0 1 1 1
  757. Sbox values are
  758. r1= 0 c1=0 r2=1 c2=3
  759. s0_val= 1 s1_val=3
  760. Resulting 4 bits are
  761. 0 1 1 1
  762. Resulting 8 bits are
  763. 0 1 1 1 0 1 0 0
  764. Enter IP Inverse
  765. 4 1 3 5 7 2 8 6
  766. IP_Inverse is
  767. 4 1 3 5 7 2 8 6
  768. Plaintext is
  769. 1 0 1 0 0 1 0 1
  770. */