943,031 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 10385
  • C++ RSS
0

Simple Data Encryption Standard (SDES) Algorithm for Encryption and Decryption.

by on Mar 23rd, 2009
This is a program for Encryption and Decryption
This program uses the Simple Data Encryption Standard (SDES) Algorithm.
This Algo takes 8-bits of plaintext at a time and produces 8-bits of ciphertext.
It uses 10-bits of key for Encryption and Decryption.
C++ Code Snippet (Toggle Plain Text)
  1. /*
  2.   This is a program for Encryption and Decryption
  3.   This program uses the Simple Data Encryption Standard (SDES) Algorithm.
  4.   This Algo takes 8-bits of plaintext at a time and produces 8-bits of ciphertext.
  5.   It uses 10-bits of key for Encryption and Decryption.
  6.  
  7.   Developed by : Vivek Kumar (vivek_kumar_bt@yahoo.co.in)
  8.  
  9.   Created on : 31 March 2005
  10.   Last Modified on : 10 April 2005
  11.  
  12.   Any sort of suggetions/comments are most welcome at vivek_kumar_bt@yahoo.co.in
  13. */
  14.  
  15. #include<iostream.h>
  16. #include<stdio.h>
  17. #include<conio.h>
  18. #include<string.h>
  19. #include<stdlib.h>
  20. #include<assert.h>
  21. void mainmenu(int *);
  22. void menuEn();
  23. void menuDe();
  24. int DoEnDe(int);
  25.  
  26. class SDES
  27. {
  28. private:
  29. char KEY[11],K1[9],K2[9],IPOutput[9],InvIPOutput[9];
  30. char F1Output[9],F2Output[9];
  31. char INPUT_BIT[9],OUTPUT_BIT[9];
  32.  
  33. public:
  34. unsigned char INPUT,OUTPUT;
  35.  
  36. SDES(char *key);
  37. ~SDES();
  38. void GenerateKeys();
  39. char *Left_Shift(char *,int );
  40. void conv_to_bits(unsigned char );
  41. void IP(char *);
  42. void InvIP(char *);
  43. void DES_Encryption(unsigned char );
  44. void DES_Decryption(unsigned char );
  45. void Function_F(char *,char *,int );
  46. char *EX_OR(char *,int );
  47. char *SBOX0(char *);
  48. char *SBOX1(char *);
  49. void SDES::GetChar();
  50. };
  51. SDES::SDES(char *key) //Initializes the object with 10-bits key
  52. {
  53. int i;
  54. if (strlen(key)!=10) //Checks for valid length key
  55. {
  56. printf("\nInValid Key-Length %s %d",key,strlen(key));
  57. getch();
  58. exit(1);
  59. }
  60. for (i=0;i<10;i++) //Assigning the key privatly
  61. {
  62. KEY[i]=key[i];
  63. }
  64. KEY[10]='\0';
  65. GenerateKeys(); //Key Genaration Starts. Output: (K1/K2)
  66.  
  67. }
  68.  
  69. void SDES::GenerateKeys()
  70. {
  71. int P10[10]={3,5,2,7,4,10,1,9,8,6}; //P10 permutation-array
  72. char P10_OP[11]; //Output of P10 is to be stored here
  73. int P8[8]={6,3,7,4,8,5,10,9}; //P8 permutation-array
  74. char *P10LEFT,*pl,*pl1,*P10RIGHT,*pr,*pr1,*plpr;
  75. int i;
  76.  
  77. /*P10 operation is done on main key*/
  78. for (i=0;i<10;i++)
  79. P10_OP[i]=KEY[P10[i]-1];
  80.  
  81. P10_OP[10]='\0';
  82.  
  83. /*Dividing 10-bit output of P10 operation into
  84.   two parts*/
  85. for (i=0;i<5;i++)
  86. {
  87. P10LEFT[i]=P10_OP[i];
  88. P10RIGHT[i]=P10_OP[i+5];
  89. }
  90. P10LEFT[5]='\0';
  91. P10RIGHT[5]='\0';
  92.  
  93. pl=new char[6];
  94. pr=new char[6];
  95.  
  96. /*Perform Left-Circular shift by 1 bit on the
  97.   two parts of P10 output*/
  98. pl=Left_Shift(P10LEFT,1);
  99. pr=Left_Shift(P10RIGHT,1);
  100.  
  101. /*Combine the above two parts after
  102.   the left-cicular operation into 'plpr' string*/
  103. for (i=0;i<5;i++)
  104. {
  105. plpr[i]=pl[i];
  106. plpr[i+5]=pr[i];
  107. }
  108. plpr[10]='\0';
  109.  
  110. /*Performing P8 Operation on plpr and assigning to K1*/
  111. for (i=0;i<8;i++)
  112. K1[i]=plpr[P8[i]-1];
  113.  
  114. K1[8]='\0'; //This is our first sub-key K1
  115.  
  116. /*Again performing Left-Circular-Shift(LCS) by 2 bits on
  117.   the output of previous Left-Cicular-Shift(LCS)*/
  118. pl1=Left_Shift(pl,2);
  119. pr1=Left_Shift(pr,2);
  120.  
  121. /*Combining the output of above LCS2 into 1 string*/
  122. for (i=0;i<5;i++)
  123. {
  124. plpr[i]=pl1[i];
  125. plpr[i+5]=pr1[i];
  126. }
  127. plpr[10]='\0';
  128.  
  129. /*Again performing P8 operation on the above combined
  130.   string*/
  131. for (i=0;i<8;i++)
  132. {
  133. K2[i]=plpr[P8[i]-1];
  134. }
  135. K2[8]='\0'; //This is our second sub-key K2
  136.  
  137. }
  138.  
  139. /*Method to perform Left-Circular-Shift on bit-string*/
  140. char *SDES::Left_Shift(char *bs,int n)
  141. {
  142. int length=strlen(bs);
  143. char *char_ptr,firstbit,*str;
  144.  
  145. char_ptr = new char[length +1];
  146. str=new char[length+1];
  147. char_ptr=bs;
  148.  
  149. int i,j;
  150. for (j=0;j<n;j++)
  151. {
  152. firstbit=char_ptr[0];
  153.  
  154. for (i=0;i<length-1;i++)
  155. {
  156. str[i]=char_ptr[i+1];
  157. }
  158. str[length-1]=firstbit;
  159. char_ptr[length]='\0';
  160. char_ptr=str;
  161. }
  162. char_ptr[length]='\0';
  163. return(str);
  164. }
  165.  
  166. /*Method to convert unsigned char to bit-string
  167.   For Ex. 1="00000001"*/
  168. void SDES::conv_to_bits(unsigned char ch)
  169. {
  170. int i,bit;
  171. INPUT_BIT[8]='\0';
  172. for (i=7;i>=0;i--)
  173. {
  174. bit=ch%2;
  175. ch=ch/2;
  176.  
  177. if (bit!=0)
  178. INPUT_BIT[i]='1';
  179. else
  180. INPUT_BIT[i]='0';
  181. }
  182.  
  183. }
  184.  
  185. /*Method to perform Initial-Permutation*/
  186. void SDES::IP(char *input)
  187. {
  188. int IPArray[8]={2,6,3,1,4,8,5,7};
  189. int i;
  190. IPOutput[8]='\0';
  191. for (i=0;i<8;i++)
  192. {
  193. IPOutput[i]=input[IPArray[i]-1];
  194. }
  195. }
  196.  
  197. /*Method to perform Inverse of Initial-Permutation*/
  198. void SDES::InvIP(char *input)
  199. {
  200. int InvIPArray[8]={4,1,3,5,7,2,8,6};
  201. int i;
  202. InvIPOutput[8]='\0';
  203. for (i=0;i<8;i++)
  204. {
  205. InvIPOutput[i]=input[InvIPArray[i]-1];
  206. }
  207. }
  208.  
  209. /*Method to perform SDES-Encryption on 8-bit 'input'*/
  210. void SDES::DES_Encryption(unsigned char input)
  211. {
  212. char LIP[5],RIP[5],L1[5],R1[5];
  213. int i;
  214.  
  215. INPUT=input;
  216. conv_to_bits(INPUT); //Converts the input to bit-string
  217. IP(INPUT_BIT); //Initial-Permutation
  218.  
  219. //gotoxy(1,1);
  220. printf("\nEncrpyting.........");
  221.  
  222. /*Dividing the output of IP into 2 parts*/
  223. for (i=0;i<4;i++)
  224. {
  225. LIP[i]=IPOutput[i];
  226. RIP[i]=IPOutput[i+4];
  227. }
  228. LIP[4]='\0';
  229. RIP[4]='\0';
  230.  
  231. /*Sending the above divided parts to Function_F and sub-key K1*/
  232. Function_F(LIP,RIP,1);
  233.  
  234. /*Dividing the output of the Function_F into 2 parts*/
  235. for (i=0;i<4;i++)
  236. {
  237. L1[i]=F1Output[i];
  238. R1[i]=F1Output[4+i];
  239. }
  240. L1[4]='\0';
  241. R1[4]='\0';
  242.  
  243. /*This time the string-parameters swaped and uses sub-key K2*/
  244. Function_F(R1,L1,2);
  245.  
  246. /*Performing the Inverse IP on the output of the Funtion_F*/
  247. InvIP(F1Output); //The output of the function will give us
  248. //Cipher-string
  249.  
  250. /*Cipher string is converted back to unsigned char and stored
  251.   in private-variable OUTPUT of this class*/
  252. GetChar();
  253. }
  254.  
  255.  
  256. /*Decryption is just inverse of Encryption
  257.   Here IP, InvIP, E/P, SBOX1 and SBOX2 are same
  258.   But Function_F first operats on sub-key K2 and
  259.   then on sub-key K1*/
  260. void SDES::DES_Decryption(unsigned char input)
  261. {
  262. char LIP[5],RIP[5],L1[5],R1[5];
  263. int i;
  264.  
  265. INPUT=input;
  266. conv_to_bits(INPUT);
  267. IP(INPUT_BIT); //Initial-Permutation
  268.  
  269. //gotoxy(1,1);
  270. printf("\nDecrpyting.........");
  271.  
  272. for (i=0;i<4;i++)
  273. {
  274. LIP[i]=IPOutput[i];
  275. RIP[i]=IPOutput[i+4];
  276. }
  277. LIP[4]='\0';
  278. RIP[4]='\0';
  279.  
  280. Function_F(LIP,RIP,2);
  281.  
  282. for (i=0;i<4;i++)
  283. {
  284. L1[i]=F1Output[i];
  285. R1[i]=F1Output[4+i];
  286. }
  287. L1[4]='\0';
  288. R1[4]='\0';
  289.  
  290. Function_F(R1,L1,1);
  291. InvIP(F1Output);
  292. GetChar();
  293. }
  294.  
  295. void SDES::Function_F(char *linput,char *rinput,int key)
  296. {
  297. int E_P[8]={4,1,2,3,2,3,4,1}; //E/P Operation-Array
  298. int P4[4]={2,4,3,1}; //P4 Operation-Array
  299. int i;
  300. char E_POutput[9],*EXOR_Output,*LEXOR,*REXOR;
  301. char *SBOX0_Output,*SBOX1_Output;
  302. char SBOX_Output[5];
  303. char P4_Output[5];
  304. char fk_Output[5];
  305. char Main_Output[9];
  306.  
  307. /*E/P Operaion is performed here*/
  308. for (i=0;i<8;i++)
  309. {
  310. E_POutput[i]=rinput[E_P[i]-1];
  311. }
  312. E_POutput[8]='\0';
  313.  
  314. /*Bitwise-EXOR is done on E/P Output and sub-key(K1/K2)*/
  315. EXOR_Output=EX_OR(E_POutput,key);
  316.  
  317. /*Divide the output of Exor in 2 parts*/
  318. LEXOR=new char[strlen(EXOR_Output)/2+1];
  319. REXOR=new char[strlen(EXOR_Output)/2+1];
  320.  
  321. for (i=0;i<strlen(EXOR_Output)/2;i++)
  322. {
  323. LEXOR[i]=EXOR_Output[i];
  324. REXOR[i]=EXOR_Output[i+4];
  325. }
  326. LEXOR[4]=REXOR[4]='\0';
  327.  
  328.  
  329. /*Peforming SBOX0 Operation on left 4 bits*/
  330. SBOX0_Output=SBOX0(LEXOR);
  331.  
  332. /*Peforming SBOX1 Operation on right 4 bits*/
  333. SBOX1_Output=SBOX1(REXOR);
  334.  
  335. /*Combining the 2-bits output of both SBOXES in one string*/
  336. for (i=0;i<2;i++)
  337. {
  338. SBOX_Output[i]=SBOX0_Output[i];
  339. SBOX_Output[i+2]=SBOX1_Output[i];
  340. }
  341. SBOX_Output[4]='\0';
  342.  
  343. /*Performing the P4 operation on SBOX output*/
  344. for (i=0;i<4;i++)
  345. {
  346. P4_Output[i]=SBOX_Output[P4[i]-1];
  347. }
  348. P4_Output[4]='\0';
  349.  
  350. /*Performing the EXOR operation on 4-bits P4-output
  351.   and 4-bits Leftinput of Funtion_F*/
  352. for (i=0;i<4;i++)
  353. {
  354. if (P4_Output[i]==linput[i])
  355. fk_Output[i]='0';
  356. else
  357. fk_Output[i]='1';
  358. }
  359. fk_Output[4]='\0';
  360.  
  361. /*Cancating the 4-bits output of above EXOR-operation
  362.   and 4-bits Right-input of Function_F*/
  363. for (i=0;i<4;i++)
  364. {
  365. Main_Output[i]=fk_Output[i];
  366. Main_Output[i+4]=rinput[i];
  367. }
  368. Main_Output[8]='\0';
  369. /*Assigning this Cucaneted string to Private variable 'F1Output'*/
  370. strcpy(F1Output,Main_Output);
  371. }
  372.  
  373. /*This method EXORS the output ofE/P and sub-keys
  374.   depending on the parameter k.
  375.   k=1:subkey K1 k=2:subkey K2*/
  376. char *SDES::EX_OR(char *ep,int k)
  377. {
  378. char *output,*key;
  379. int i,klen;
  380.  
  381. output=new char[strlen(ep)+1];
  382. key=new char[strlen(K1)+1];
  383. if (k==1)
  384. {
  385. strcpy(key,K1);
  386. } else
  387. {
  388. if (k==2)
  389. {
  390. strcpy(key,K2);
  391. } else
  392. {
  393. printf("\n\nWrong Choice in the key parameter(1/2)");
  394. getch();
  395. exit(1);
  396. }
  397. }
  398. klen=strlen(K1);
  399. if (strlen(ep)!=klen)
  400. {
  401. printf("\ninput=%d is not equal to K=%d",strlen(ep),klen);
  402. printf("\n\nError in the Output of E/P (Length)..Press any key");
  403. getch();
  404. exit(1);
  405. }
  406. for (i=0;i<strlen(ep);i++)
  407. {
  408. if (ep[i]==key[i])
  409. output[i]='0';
  410. else
  411. output[i]='1';
  412. }
  413. output[strlen(ep)]='\0';
  414. return(output);
  415. }
  416.  
  417. /*SBOX0 Operation is defined here*/
  418. char *SDES::SBOX0(char *l)
  419. {
  420. int S0[4][4]={1,0,3,2, //S0 Matrix
  421. 3,2,1,0,
  422. 0,2,1,3,
  423. 3,1,3,2
  424. };
  425.  
  426. char *bits[]={"00","01","10","11"};
  427. char lrow[3],lcol[3];
  428. char *SO;
  429. int i,lr,lc,b;
  430.  
  431. SO=new char[3];
  432.  
  433. lrow[0]=l[0];
  434. lrow[1]=l[3];
  435. lcol[0]=l[1];
  436. lcol[1]=l[2];
  437.  
  438. lrow[2]='\0';
  439. lcol[2]='\0';
  440.  
  441.  
  442. for (i=0;i<4;i++)
  443. {
  444. if (strcmp(lrow,bits[i])==0)
  445. lr=i;
  446. if (strcmp(lcol,bits[i])==0)
  447. lc=i;
  448. }
  449. b=S0[lr][lc];
  450. for (i=0;i<3;i++)
  451. SO[i]=bits[b][i];
  452. SO[3]='\0';
  453. return(SO);
  454. }
  455. /*SBOX1 Operation is defined here*/
  456. char *SDES::SBOX1(char *l)
  457. {
  458. int S0[4][4]={0,1,2,3, //S1 Matrix
  459. 2,0,1,3,
  460. 3,0,1,0,
  461. 2,1,0,3
  462. };
  463.  
  464. char *bits[]={"00","01","10","11"};
  465. char lrow[3],lcol[3];
  466. char *SO;
  467. int i,lr,lc,b;
  468.  
  469. SO=new char[3];
  470.  
  471. lrow[0]=l[0];
  472. lrow[1]=l[3];
  473. lcol[0]=l[1];
  474. lcol[1]=l[2];
  475.  
  476. lrow[2]='\0';
  477. lcol[2]='\0';
  478.  
  479.  
  480. for (i=0;i<4;i++)
  481. {
  482. if (strcmp(lrow,bits[i])==0)
  483. lr=i;
  484. if (strcmp(lcol,bits[i])==0)
  485. lc=i;
  486. }
  487. b=S0[lr][lc];
  488. for (i=0;i<3;i++)
  489. SO[i]=bits[b][i];
  490.  
  491. SO[3]='\0';
  492. return(SO);
  493. }
  494.  
  495. /*Method to get back unsigned char from bit-string*/
  496. void SDES::GetChar()
  497. {
  498. int i,j,in;
  499. unsigned char ch=0;
  500. char *bs;
  501. bs=new char[9];
  502. bs=InvIPOutput;
  503. if (strlen(bs)>8)
  504. {
  505. printf("\nWRONG LENGTH STRING");
  506. exit(0);
  507. }
  508. for (i=0;i<8;i++)
  509. {
  510. if (bs[i]=='1')
  511. {
  512. in=1;
  513. for (j=1;j<8-i;j++)
  514. {
  515. in=in*2;
  516. }
  517. ch=ch+in;
  518. }
  519. }
  520. OUTPUT=ch;
  521. }
  522.  
  523. /*Destructor*/
  524. SDES::~SDES()
  525. {
  526. }
  527.  
  528.  
  529. char *sfname,*tfname;
  530. char *key;//="1010000010";
  531. void main(void)
  532. {
  533. //clrscr();
  534. unsigned char ch,ch1;
  535. int i,n=10,choice;
  536.  
  537. while (1)
  538. {
  539. key = new char[11];
  540. sfname = new char[20];
  541. tfname = new char[20];
  542. mainmenu(&choice);
  543. fflush(stdin);
  544. switch (choice)
  545. {
  546. case 1:
  547. menuEn();
  548. DoEnDe(choice);
  549. break;
  550. case 2:
  551. menuDe();
  552. DoEnDe(choice);
  553. break;
  554. case 3:
  555. exit(0);
  556. default:
  557. printf("\nWrong Choice Enter again\nPress any key to return to Main Menu..");
  558. getch();
  559. break;
  560. }
  561. }
  562. }
  563.  
  564. void mainmenu(int *c)
  565. {
  566. //clrscr();
  567. printf("\nWhat do you want to do..");
  568. printf("\n1. Encryption");
  569. printf("\n2. Decryption");
  570. printf("\n3. Exit");
  571. printf("\n\nEnter the choice? ");
  572. scanf("%d",c);
  573. }
  574. void menuEn()
  575. {
  576. //clrscr();
  577. sfname=new char[20];
  578. tfname=new char[20];
  579. key=new char[11];
  580. printf("\nEncryption Menu\n\n");
  581. printf("\nEnter the filename to be Encrypted: ");
  582. gets(sfname);
  583. printf("\nEnter the Target file name: ");
  584. gets(tfname);
  585. printf("\nEnter the 10-bits KEY: ");
  586. gets(key);
  587. printf("\n\nNotedown this key, as same key is used for Decryption");
  588. //getch();
  589. }
  590.  
  591. void menuDe()
  592. {
  593. //clrscr();
  594. sfname=new char[20];
  595. tfname=new char[20];
  596. key=new char[11];
  597. printf("\nDecryption Menu\n\n");
  598. printf("\nEnter the filename to be Decrypted: ");
  599. gets(sfname);
  600. printf("\nEnter the Target file name: ");
  601. gets(tfname);
  602. printf("\nEnter the 10-bits KEY: ");
  603. gets(key);
  604. }
  605.  
  606. int DoEnDe(int c)
  607. {
  608. SDES S(key);
  609. int i,n;
  610. n=10; //Number of Rounds
  611. unsigned char ch;
  612. FILE *fp;
  613. FILE *ft;
  614. fp=fopen(tfname,"w");
  615. ft=fopen(sfname,"r");
  616. if (fp==NULL)
  617. {
  618. printf("\nTarget File not opened SORRY");
  619. getch();
  620. fclose(fp);
  621. return(0);
  622. }
  623. if (ft==NULL)
  624. {
  625. printf("\nSource File not opened SORRY");
  626. getch();
  627. fclose(ft);
  628. return(0);
  629. }
  630. while (fread(&ch,1,1,ft)==1)
  631. {
  632. S.OUTPUT=ch;
  633.  
  634. for (i=0;i<n;i++)
  635. {
  636. if (c==1)
  637. S.DES_Encryption(S.OUTPUT);
  638. if (c==2)
  639. S.DES_Decryption(S.OUTPUT);
  640. }
  641. fwrite(&S.OUTPUT,1,1,fp);
  642. }
  643. printf("\nCompleted!!!!!");
  644. getch();
  645. fclose(fp);
  646. fclose(ft);
  647. return(1);
  648. }
Comments on this Code Snippet
Apr 4th, 2009
0

Re: Simple Data Encryption Standard (SDES) Algorithm for Encryption and Decryption.

Very nice snippet !
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Message:
Previous Thread in C++ Forum Timeline: Estimate the roots of a number ...
Next Thread in C++ Forum Timeline: Need help understanind recussion in binary search tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC