SAES.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. import java.util.*;
  2. public class SAES {
  3. static int[][] w=new int[6][8];
  4. static int[] tempval=new int[8];
  5. static int[] subNib={9,4,10,11,13,1,8,5,6,2,0,3,12,14,15,7};
  6. static int[] xorval1={1,0,0,0,0,0,0,0};
  7. static int[] xorval2={0,0,1,1,0,0,0,0};
  8. static int[][][] SBOX_Encryption={
  9. {{1,0,0,1},{0,1,0,0},{1,0,1,0},{1,0,1,1},},
  10. {{1,1,0,1},{0,0,0,1},{1,0,0,0},{0,1,0,1},},
  11. {{0,1,1,0},{0,0,1,0},{0,0,0,0},{0,0,1,1},},
  12. {{1,1,0,0},{1,1,1,0},{1,1,1,1},{0,1,1,1},},
  13. };
  14. static int[][] subnib1={{1,0,0,1},
  15. {0,1,0,0},
  16. {1,0,1,0},
  17. {1,0,1,1},
  18. {1,1,0,1},
  19. {0,0,0,1},
  20. {1,0,0,0},
  21. {0,1,0,1},
  22. {0,1,1,0},
  23. {0,0,1,0},
  24. {0,0,0,0},
  25. {0,0,1,1},
  26. {1,1,0,0},
  27. {1,1,1,0},
  28. {1,1,1,0},
  29. {0,1,1,1},
  30. };
  31. static int[][] subnib2={{0,0,0,0},
  32. {0,0,0,0},
  33. {0,0,0,0},
  34. {1,0,1,1},
  35. {0,0,0,1},
  36. {0,0,0,0},
  37. {0,0,0,0},
  38. {0,0,0,0},
  39. {0,0,0,0},
  40. {0,0,0,0},
  41. {0,0,1,0},
  42. {0,0,0,0},
  43. {0,0,0,0},
  44. {0,0,0,0},
  45. {0,0,0,0},
  46. {0,0,0,0},
  47. };
  48. static int[] K0=new int[16];
  49. static int[] K1=new int[16];
  50. static int[] K2=new int[16];
  51. static int[] ptext={1,1,0,1,0,1,1,1,0,0,1,0,1,0,0,0};//new int[16];
  52. static int[] ctext=new int[16];
  53. static int[] k={0,1,0,0,1,0,1,0,1,1,1,1,0,1,0,1};//new int[16];
  54. static int[] w0=new int[8];
  55. static int[] w1=new int[8];
  56. static int[] w2=new int[8];
  57. static int[] w3=new int[8];
  58. static int[] w4=new int[8];
  59. static int[] w5=new int[8];
  60. static int[] wp=new int[8];
  61. public static void main(String args[])
  62. {
  63. int i=0,N=0,x=0,y=0,z=0;;
  64. int[] xored;
  65. String[] ws = new String[10];
  66. Scanner sc=new Scanner(System.in);
  67. for(i=0;i<8;i++)
  68. {
  69. w0[i]=k[i];
  70. w1[i]=k[i+8];
  71. }
  72. for(i=0;i<8;i++)
  73. {
  74. w[0][i]=w0[i];
  75. w[1][i]=w1[i];
  76. }
  77. N=2;
  78. while(N<6)
  79. {
  80. if(N%2==0)
  81. {
  82. for(i=0;i<8;i++)
  83. wp[i]=w[N-1][i];
  84. tempval=subNib(rotNib(wp));
  85. if(N==2)
  86. {
  87. for(i=0;i<8;i++)
  88. w[N][i]=w[N-2][i]^xorval1[i]^tempval[i];
  89. }
  90. else if(N==4)
  91. {
  92. for(i=0;i<8;i++)
  93. w[N][i]=w[N-2][i]^xorval2[i]^tempval[i];
  94. }
  95. }
  96. else
  97. {
  98. for(i=0;i<8;i++)
  99. {
  100. w[N][i]=w[N-1][i]^w[N-2][i];
  101. }
  102. }
  103. N++;
  104. }
  105. for(i=0;i<16;i++)
  106. {
  107. if(i<8)
  108. {
  109. K0[i]=w[0][i];
  110. K1[i]=w[2][i];
  111. K2[i]=w[4][i];
  112. }
  113. else
  114. {
  115. K0[i]=w[1][x];
  116. K1[i]=w[3][x];
  117. K2[i]=w[5][x];
  118. x++;
  119. }
  120. }
  121. System.out.println("K0 is: ");
  122. for(i=0;i<16;i++)
  123. {
  124. System.out.print(K0[i]+",");
  125. }
  126. System.out.println("\nK1 is: ");
  127. for(i=0;i<16;i++)
  128. {
  129. System.out.print(K1[i]+",");
  130. }
  131. System.out.println("\nK2 is: ");
  132. for(i=0;i<16;i++)
  133. {
  134. System.out.print(K2[i]+",");
  135. }
  136. ctext=encrypt();
  137. System.out.println("\n\nThe Cipher Text is: ");
  138. for(int f = 0;f<16;f++)
  139. {
  140. System.out.print(ctext[f]+",");
  141. }
  142. ptext=decrypt();
  143. }
  144. static int[] encrypt()
  145. {
  146. int [][][] Me={{{0,0,0,1},{0,1,0,0}},{{0,1,0,0},{0,0,0,1}}};
  147. int[] xored = new int[ptext.length];
  148. int[] Result=new int[16];
  149. xored=XOR(ptext,K0);
  150. Result=round(ptext,xored);
  151. Result=Final_Round(Result);
  152. return Result;
  153. }
  154. static int[] decrypt()
  155. {
  156. int [][] IMe={{2,9},{9,2}};
  157. int[] Result=new int[16];
  158. Result=ctext;
  159. Result=ARK2(Result);
  160. Result=swapNib(Result, 2, 4);
  161. Result=subNib_for_decryption(Result);
  162. Result=ARK1(Result);
  163. Result=MixColumn(Result,IMe);
  164. System.out.println("Result Array after Inverse Mix Column is: ");
  165. for(int i=0;i<16;i++)
  166. {
  167. System.out.print(Result[i]+",");
  168. }
  169. return Result;
  170. }
  171. static int[] Final_Round(int[] Output)
  172. {
  173. Output=subNib(Output);
  174. swapNib(Output, 2, 4);
  175. Output=ARK2(Output);
  176. return Output;
  177. }
  178. static int[] ARK2(int[] temp)
  179. {
  180. int[] XOR_With={1,0,0,0,0,1,1,1,1,0,1,0,1,1,1,1};
  181. temp=XOR(temp,XOR_With);
  182. return temp;
  183. }
  184. static int[] round(int[] ptext,int[] xored)
  185. {
  186. int [][] Me={{1,4},{4,1}};
  187. int[] output=new int[16];
  188. int[] temp1=new int[8];
  189. int[] temp2=new int[8];
  190. int[][][] S=new int[2][2][4];
  191. int[] Result=new int[16];
  192. int i=0;
  193. for(i=0;i<8;i++)
  194. {
  195. temp1[i]=xored[i];
  196. temp2[i]=xored[i+8];
  197. }
  198. temp1=subNib(temp1);
  199. temp2=subNib(temp2);
  200. for(i=0;i<8;i++)
  201. {
  202. output[i]=temp1[i];
  203. output[i+8]=temp2[i];
  204. }
  205. /*System.out.println("\nOutput is: ");
  206. for(i=0;i<16;i++)
  207. {
  208. System.out.print(output[i]+",");
  209. }*/
  210. output=swapNib(output,2,4);
  211. /*System.out.println("\nOutput after swapping 2nd and 4th nibbles is: ");
  212. for(i=0;i<16;i++)
  213. {
  214. System.out.print(output[i]+",");
  215. }*/
  216. int f=0;
  217. /*for(i=0;i<2;i++)
  218. {
  219. for(int j=0;j<2;j++)
  220. {
  221. for(int k=0;k<4;k++)
  222. {
  223. S[i][j][k]=output[f];
  224. f++;
  225. }
  226. }
  227. }
  228. mulmat(S,Me);*/
  229. Result=MixColumn(output,Me);
  230. Result=ARK1(Result);
  231. /*System.out.println("Result After ARK1 is : ");
  232. for(int j=0;j<16;j++)
  233. {
  234. System.out.print(Result[j]+",");
  235. }*/
  236. return Result;
  237. /*System.out.println("\nThe S array is: ");
  238. for(i=0;i<2;i++)
  239. {
  240. for(int j=0;j<2;j++)
  241. {
  242. for(int k=0;k<4;k++)
  243. {
  244. System.out.print("S["+i+"]"+"["+j+"]"+"["+k+"]"+S[i][j][k]+" ");
  245. }
  246. System.out.println("");
  247. }
  248. }*/
  249. }
  250. static int[] ARK1(int[] temp)
  251. {
  252. int[] XOR_With={1,1,0,1,1,1,0,1,0,0,1,0,1,0,0,0};//{1},{1},{0},{1},{1},{1},{0},{1},{0},{0},{1},{0},{1},{0},{0},{0}};
  253. temp=XOR(temp,XOR_With);
  254. return temp;
  255. }
  256. static int[] MixColumn(int[] output,int[][] Me)
  257. {
  258. int[] S_val=new int[4];
  259. int[] divident=new int[8];
  260. int[] A=new int[8],B=new int[8];
  261. int[] S_New=new int[8];
  262. int[] Result=new int[16];
  263. String temp;
  264. for (int k= 0; k < output.length; k += 4) {
  265. S_val[k / 4] = output[k] * 1000 + output[k + 1] * 100 + output[k + 2] * 10 + output[k + 3];
  266. }
  267. int s1=BToD(S_val[0]);
  268. int s2=BToD(S_val[1]);
  269. int s3=BToD(S_val[2]);
  270. int s4=BToD(S_val[3]);
  271. int k1=3,h=0;
  272. // to multiply with M matrix
  273. int[][] S = { { s1, s3 }, { s2, s4 } };
  274. int[][] multiply = new int[2][2];
  275. int sum = 0,count=0;
  276. for (int c = 0; c < 2; c++) {
  277. for (int d = 0; d < 2; d++) {
  278. for (int k = 0; k < 2; k++) {
  279. count++;
  280. //System.out.println("Count= "+count);
  281. if(k==0)
  282. {
  283. k1=3;
  284. temp=String.format("%4s",Integer.toBinaryString(Me[c][k] * S[k][d])).replace(' ', '0');
  285. if((Me[c][k] * S[k][d])<=15)
  286. {
  287. for(int i=7;i>=4;i--)
  288. {
  289. if(k1>=0)
  290. {
  291. A[i]=Integer.parseInt(String.valueOf((temp.charAt(k1))));
  292. k1--;
  293. }
  294. }
  295. }
  296. else if(Me[c][k]>1 && (Me[c][k] * S[k][d])>15){
  297. //String a=Integer.toBinaryString(Me[c][k] * S[k][d]);
  298. //System.out.println("\ntemp="+temp);
  299. A=polydiv(temp,"10011");
  300. }
  301. }
  302. else if(k==1)
  303. {
  304. //B=Me[c][k] * S[k][d];
  305. temp=String.format("%4s",Integer.toBinaryString(Me[c][k] * S[k][d])).replace(' ', '0');
  306. k1=3;
  307. if((Me[c][k] * S[k][d])<=15)
  308. {
  309. for(int i=7;i>=4;i--)
  310. {
  311. if(k1>=0)
  312. {
  313. B[i]=Integer.parseInt(String.valueOf(temp.charAt(k1)));
  314. k1--;
  315. }
  316. }
  317. }
  318. else if(Me[c][k]>1 && (Me[c][k] * S[k][d])>15){
  319. String a=Integer.toBinaryString( (Me[c][k] * S[k][d]));
  320. //System.out.println("\na="+a);
  321. B=polydiv(temp,"10011");
  322. }
  323. //sum = sum + Me[c][k] * S[k][d];
  324. }
  325. /*multiply[c][d] = sum;
  326. System.out.println("\nmultiply[" + c + "][" + d + "]= " + sum);
  327. sum = 0;*/
  328. }
  329. S_New=XOR(A,B);
  330. System.out.println("S_New is : ");
  331. for(int i=0;i<8;i++)
  332. {
  333. System.out.print(S_New[i]+",");
  334. }
  335. /*for(int y=0,g=7;y<8;y++,g--)
  336. {
  337. Result[h]=(int) (sum+S_New[y]*Math.pow(2,g));
  338. h++;
  339. }*/
  340. for(int y=4;y<=7;y++)
  341. {
  342. Result[h]=S_New[y];
  343. h++;
  344. }
  345. }
  346. }
  347. Result=swapNib(Result, 2, 3);
  348. System.out.println("Result is: ");
  349. for(int y=0;y<16;y++)
  350. {
  351. System.out.print(Result[y]+",");
  352. }
  353. return Result;
  354. }
  355. static int[] polydiv(String divid,String divis)
  356. {
  357. int[] dividend=new int[8];
  358. int[] divisor=new int[8];
  359. int degDivisor=0,degDividend=0;
  360. int i=0,k=divid.length()-1,l=divis.length()-1;
  361. //System.out.println("\nDividend"+divid);
  362. //System.out.println("divisor"+divis);
  363. int diff=0;
  364. int temp=0;
  365. int[] multiplied=new int[8];
  366. for(i=7;i>=0;i--)
  367. {
  368. if(k>=0)
  369. {
  370. dividend[i]=Integer.parseInt(String.valueOf(divid.charAt(k)));
  371. k--;
  372. }
  373. }
  374. for(i=0;i<8;i++)
  375. {
  376. //System.out.println("dividend["+i+"]"+dividend[i]);
  377. }
  378. for(i=7;i>=0;i--)
  379. {
  380. if(l>=0)
  381. {
  382. divisor[i]=Integer.parseInt(String.valueOf(divis.charAt(l)));
  383. l--;
  384. }
  385. }
  386. for(i=0;i<8;i++)
  387. {
  388. //System.out.println("divisor["+i+"]"+divisor[i]);
  389. }
  390. while(true)
  391. {
  392. for(i=0;i<8;i++)
  393. {
  394. if(dividend[i]==1)
  395. {
  396. degDividend=7-i;
  397. break;
  398. }
  399. }
  400. for(i=0;i<8;i++)
  401. {
  402. if(divisor[i]==1)
  403. {
  404. degDivisor=7-i;
  405. break;
  406. }
  407. }
  408. if(degDivisor>degDividend)
  409. break;
  410. System.out.println("degree of dividend is= "+degDividend);
  411. System.out.println("degree of divisor is= "+degDivisor);
  412. diff=degDividend-degDivisor;
  413. if(diff<0)
  414. diff=-diff;
  415. for(i=0;i<8;i++)
  416. {
  417. multiplied[i]=divisor[i];
  418. }
  419. for(i=0;i<diff;i++)
  420. {
  421. if(diff!=0)
  422. multiplied=leftShift(multiplied);
  423. }
  424. System.out.println("Array after left shift");
  425. for(i=0;i<8;i++)
  426. {
  427. System.out.print(multiplied[i]+" ");
  428. }
  429. dividend=XOR(multiplied,dividend);
  430. System.out.println("\nArray after XOR");
  431. for(i=0;i<8;i++)
  432. {
  433. System.out.print(dividend[i]+" ");
  434. }
  435. }
  436. return dividend;
  437. }
  438. public static int[] leftShift(int[] var) {
  439. int temp=0;
  440. //temp=var[0];
  441. for(int i=0;i<var.length;i++)
  442. {
  443. if(i!=var.length-1)
  444. {
  445. var[i]=var[i+1];
  446. }
  447. else if(i==var.length-1)
  448. {
  449. var[i]=0;
  450. }
  451. }
  452. return var;
  453. }
  454. public static int BToD(int bNum) {
  455. int dec=0;
  456. int f=0;
  457. while (true) {
  458. if (bNum == 0) {
  459. break;
  460. } else {
  461. int temp = bNum % 10;
  462. dec += temp * Math.pow(2, f);
  463. bNum = bNum / 10;
  464. f++;
  465. }
  466. }
  467. return dec;
  468. }
  469. static int[] swapNib(int[] input,int i,int j)
  470. {
  471. int temp=0;
  472. for(int h=0;h<4;h++)
  473. {
  474. temp=input[(i-1)*4 + h];
  475. input[(i-1)*4 + h]=input[(j-1)*4 + h];
  476. input[(j-1)*4 + h]=temp;
  477. }
  478. return input;
  479. }
  480. static int[] XOR(int[] ptext,int[] k0)
  481. {
  482. int[] kxp=new int[ptext.length];
  483. int i=0;
  484. for(i=0;i<ptext.length;i++)
  485. kxp[i]=ptext[i]^k0[i];
  486. return kxp;
  487. }
  488. static int[] rotNib(int [] c)
  489. {
  490. int temp=0;
  491. for(int i=0;i<4;i++)
  492. {
  493. temp=c[i];
  494. c[i]=c[i+4];
  495. c[i+4]=temp;
  496. }
  497. return c;
  498. }
  499. /*static int[] subNib(int[] val)
  500. {
  501. int i=val.length-1,temp=0,j=0,k=val.length-1,index1=0;
  502. int[] result=new int[val.length];
  503. while(i>=0)
  504. {
  505. if((i+1)%4!=0 || temp==0)
  506. {
  507. temp=temp+val[i]*(int)Math.pow(2,index1);
  508. index1++;
  509. }
  510. else if((i+1)%4==0 && temp!=0)
  511. {
  512. k=i;
  513. for(j=0;j<4;j++,k++)
  514. result[k]=subnib1[temp][j];
  515. index1=0;
  516. temp=0;
  517. i++;
  518. }
  519. i--;
  520. }
  521. for(i=0;i<val.length;i++)
  522. {
  523. System.out.print(result[i]+",");
  524. }
  525. System.out.println("");
  526. return result;
  527. }*/
  528. static int[] subNib_for_decryption(int[] val)
  529. {
  530. int temp1=0,temp2=0,index1=0,index2=0,i=0,j=0,j1=0,temp3=0,temp4=0,index3=0,index4=0,j3=0,j2=0;
  531. if(val.length==8)
  532. {
  533. for(i=7;i>=0;i--)
  534. {
  535. if(i<4)
  536. {
  537. temp1=temp1+val[i]*(int)Math.pow(2,index1);
  538. index1++;
  539. }
  540. else{
  541. temp2=temp2+val[i]*(int)Math.pow(2,index2);
  542. index2++;
  543. }
  544. }
  545. //System.out.println("temp1== "+temp1+" & "+"temp2=="+temp2);
  546. for(i=0;i<8;i++)
  547. {
  548. if(i<4)
  549. {
  550. val[i]=subnib2[temp1][j];
  551. j++;
  552. }
  553. else{
  554. val[i]=subnib2[temp2][j1];
  555. j1++;
  556. }
  557. }
  558. /*for(i=0;i<8;i++)
  559. {
  560. System.out.print(val[i]+",");
  561. }
  562. System.out.println("");*/
  563. }
  564. else if(val.length==16)
  565. {
  566. for(i=15;i>=0;i--)
  567. {
  568. if(i<4)
  569. {
  570. temp1=temp1+val[i]*(int)Math.pow(2,index1);
  571. index1++;
  572. }
  573. else if(i>=4 && i<8){
  574. temp2=temp2+val[i]*(int)Math.pow(2,index2);
  575. index2++;
  576. }
  577. else if(i>=8 && i<12)
  578. {
  579. temp3=temp3+val[i]*(int)Math.pow(2,index3);
  580. index3++;
  581. }
  582. else if(i>=12 && i<16)
  583. {
  584. temp4=temp4+val[i]*(int)Math.pow(2,index4);
  585. index4++;
  586. }
  587. }
  588. //System.out.println("temp1== "+temp1+" & "+"temp2=="+temp2+" & "+"temp3=="+temp3+" & "+"temp4=="+temp4);
  589. for(i=0;i<16;i++)
  590. {
  591. if(i<4)
  592. {
  593. val[i]=subnib2[temp1][j];
  594. j++;
  595. }
  596. else if(i>=4 && i<8){
  597. val[i]=subnib2[temp2][j1];
  598. j1++;
  599. }
  600. else if(i>=8 && i<12)
  601. {
  602. val[i]=subnib2[temp3][j2];
  603. j2++;
  604. }
  605. else if(i>=12 && i<16)
  606. {
  607. val[i]=subnib2[temp4][j3];
  608. j3++;
  609. }
  610. }
  611. /*for(i=0;i<val.length;i++)
  612. {
  613. System.out.print(val[i]+",");
  614. }
  615. System.out.println("");*/
  616. }
  617. return val;
  618. /*
  619. System.out.println("temp1== "+temp1+" & "+"temp2=="+temp2);
  620. for(i=0;i<15;i++)
  621. {
  622. if(temp1==subNib[i])
  623. {
  624. index1=subNib[i];
  625. }
  626. if(temp2==subNib[i])
  627. {
  628. index2=subNib[i];
  629. }
  630. }
  631. for(i=0;i<8;i++)
  632. {
  633. if(i<4)
  634. {
  635. while(index1 > 0){
  636. val[i++] = index1%2;
  637. index1 = index1/2;
  638. }
  639. }
  640. else
  641. {
  642. while(index2 > 0){
  643. val[i++] = index2%2;
  644. index2 = index2/2;
  645. }
  646. }
  647. }
  648. System.out.println("Val array is: ");
  649. for(i=0;i<8;i++)
  650. {
  651. System.out.print(val[i]+",");
  652. }
  653. System.out.println("");
  654. return val;
  655. */
  656. }
  657. static int[] subNib(int[] val)
  658. {
  659. int temp1=0,temp2=0,index1=0,index2=0,i=0,j=0,j1=0,temp3=0,temp4=0,index3=0,index4=0,j3=0,j2=0;
  660. if(val.length==8)
  661. {
  662. for(i=7;i>=0;i--)
  663. {
  664. if(i<4)
  665. {
  666. temp1=temp1+val[i]*(int)Math.pow(2,index1);
  667. index1++;
  668. }
  669. else{
  670. temp2=temp2+val[i]*(int)Math.pow(2,index2);
  671. index2++;
  672. }
  673. }
  674. //System.out.println("temp1== "+temp1+" & "+"temp2=="+temp2);
  675. for(i=0;i<8;i++)
  676. {
  677. if(i<4)
  678. {
  679. val[i]=subnib1[temp1][j];
  680. j++;
  681. }
  682. else{
  683. val[i]=subnib1[temp2][j1];
  684. j1++;
  685. }
  686. }
  687. /*for(i=0;i<8;i++)
  688. {
  689. System.out.print(val[i]+",");
  690. }
  691. System.out.println("");*/
  692. }
  693. else if(val.length==16)
  694. {
  695. for(i=15;i>=0;i--)
  696. {
  697. if(i<4)
  698. {
  699. temp1=temp1+val[i]*(int)Math.pow(2,index1);
  700. index1++;
  701. }
  702. else if(i>=4 && i<8){
  703. temp2=temp2+val[i]*(int)Math.pow(2,index2);
  704. index2++;
  705. }
  706. else if(i>=8 && i<12)
  707. {
  708. temp3=temp3+val[i]*(int)Math.pow(2,index3);
  709. index3++;
  710. }
  711. else if(i>=12 && i<16)
  712. {
  713. temp4=temp4+val[i]*(int)Math.pow(2,index4);
  714. index4++;
  715. }
  716. }
  717. //System.out.println("temp1== "+temp1+" & "+"temp2=="+temp2+" & "+"temp3=="+temp3+" & "+"temp4=="+temp4);
  718. for(i=0;i<16;i++)
  719. {
  720. if(i<4)
  721. {
  722. val[i]=subnib1[temp1][j];
  723. j++;
  724. }
  725. else if(i>=4 && i<8){
  726. val[i]=subnib1[temp2][j1];
  727. j1++;
  728. }
  729. else if(i>=8 && i<12)
  730. {
  731. val[i]=subnib1[temp3][j2];
  732. j2++;
  733. }
  734. else if(i>=12 && i<16)
  735. {
  736. val[i]=subnib1[temp4][j3];
  737. j3++;
  738. }
  739. }
  740. /*for(i=0;i<val.length;i++)
  741. {
  742. System.out.print(val[i]+",");
  743. }
  744. System.out.println("");*/
  745. }
  746. return val;
  747. /*
  748. System.out.println("temp1== "+temp1+" & "+"temp2=="+temp2);
  749. for(i=0;i<15;i++)
  750. {
  751. if(temp1==subNib[i])
  752. {
  753. index1=subNib[i];
  754. }
  755. if(temp2==subNib[i])
  756. {
  757. index2=subNib[i];
  758. }
  759. }
  760. for(i=0;i<8;i++)
  761. {
  762. if(i<4)
  763. {
  764. while(index1 > 0){
  765. val[i++] = index1%2;
  766. index1 = index1/2;
  767. }
  768. }
  769. else
  770. {
  771. while(index2 > 0){
  772. val[i++] = index2%2;
  773. index2 = index2/2;
  774. }
  775. }
  776. }
  777. System.out.println("Val array is: ");
  778. for(i=0;i<8;i++)
  779. {
  780. System.out.print(val[i]+",");
  781. }
  782. System.out.println("");
  783. return val;
  784. */
  785. }
  786. }
  787. /*
  788. Enter the plaintext
  789. 1 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0
  790. Enter the Key
  791. 0 1 0 0 1 0 1 0 1 1 1 1 0 1 0 1
  792. W0 is :0 1 0 0 1 0 1 0
  793. W1 is :1 1 1 1 0 1 0 1 temp1== 5 & temp2==15
  794. 0,0,0,1,0,1,1,1,
  795. w[2]is:
  796. 1,1,0,1,1,1,0,1,w[3]is:
  797. 0,0,1,0,1,0,0,0,temp1== 8 & temp2==2
  798. 0,1,1,0,1,0,1,0,
  799. w[4]is:
  800. 1,0,0,0,0,1,1,1,w[5]is:
  801. 1,0,1,0,1,1,1,1,K0 is:
  802. 0,1,0,0,1,0,1,0,1,1,1,1,0,1,0,1,
  803. K1 is:
  804. 1,1,0,1,1,1,0,1,0,0,1,0,1,0,0,0,
  805. K2 is:
  806. 1,0,0,0,0,1,1,1,1,0,1,0,1,1,1,1,/nAdd Round 0 key
  807. 1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,1,temp1== 9 & temp2==13
  808. 0,0,1,0,1,1,1,0,
  809. temp1== 13 & temp2==13
  810. 1,1,1,0,1,1,1,0,
  811. Output is:
  812. 0,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,
  813. Output after swapping 2nd and 4th nibbles is:
  814. 0,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,
  815. */