Important alert: (current site time 8/1/2014 3:28:19 AM EDT)
 

VB icon

Simplified DES (SDES) [Encryption & Decryption]

Email
Submitted on: 1/9/2009 9:05:11 AM
By: Girish979  
Level: Beginner
User Rating: Unrated
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 18832
(About the author)
 
     To encrypt & Decrypt Binary Information

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Simplified DES (SDES) [Encryption & Decryption]
// Description:To encrypt & Decrypt Binary Information
// By: Girish979
//
// Inputs:binary Input 8- Bits
Ex: 1 0 0 0 1 0 1 1
10 Bit Binary Key
Ex: 0 0 0 0 0 1 1 0 1 1
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=12538&lngWId=3//for details.//**************************************

#include <stdio.h>
int l[4],r[4],keys[2][8],ct[8];
void sbox(int sip[],int p[],int sbno,int i)
{
	int sbox[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
	int rw,c,sop;
	rw = sip[3]+sip[0]*2;
	c = sip[2]+sip[1]*2;
	sop = sbox[sbno][rw][c]; //sop gives decimal value of S-Box Output
	for(;sop!=0;sop/=2)
		p[i--]=sop%2;
}
void cmp_fun(int round)
{
	int EP[]={4,1,2,3,2,3,4,1},i,epd[8];
	int slip[4],srip[4];
	int p[4]={0},p4[]={2,4,3,1},np[4];
	for(i=0;i<8;i++) // E/P Permutation
		epd[i]=r[EP[i]-1];
	for(i=0;i<8;i++)//Performing XOR with Key
		if(i<4)
			slip[i] = epd[i]^keys[round][i]; // Using Key _ 1=>0
		else
			srip[i-4] = epd[i]^keys[round][i];
	sbox(slip,p,0,1);//Calling SBox 1, 0->SBOX 1
	sbox(srip,p,1,3);//Calling SBox 1, 1->SBOX 2
	for(i=0;i<4;i++) //P4 permutation
		np[i]=p[p4[i]-1];
	for(i=0;i<4;i++)
		l[i] = l[i]^np[i];
}
void left_shift(int keyip[],int nob)
{
	int t1,t2,i;
	while(nob>0)
	{
		t1=keyip[0],t2=keyip[5];
		for(i=0;i<9;i++)
			if(i<4)
				keyip[i] =keyip[i+1];
			else if(i>4)
				keyip[i] = keyip[i+1];
		keyip[4]=t1,keyip[9]=t2;
		nob--;
	}
}
void gen_keys()
{
	int key[10],i,keyip[10];
	int p10[]={3,5,2,7,4,10,1,9,8,6},p8[]={6,3,7,4,8,5,10,9};
	printf("Enter Key :");
	for(i=0;i<10;i++)
		scanf("%d", &key[i]);
	for(i=0;i<10;i++) // Permutation P10
		keyip[i] = key[p10[i]-1];
	left_shift(keyip,1);	 // Left Shifting (Array,No of bts)
printf("\nKey1 :");
	for(i=0;i<8;i++){ 			//Permuting P8 on key1
	 keys[0][i] = keyip[p8[i]-1];// Key1 Generated!!
	 printf("%d",keys[0][i]);
	}
	left_shift(keyip,2);// Generating Key2 . .
	printf("\nKey2 :");
	for(i=0;i<8;i++){
		keys[1][i] = keyip[p8[i]-1];// Key2 Generated!!
		printf("%d",keys[1][i]);
	}
}
void En_De(int pt[],int c)
{
		int ip[]={2,6,3,1,4,8,5,7},ipi[]={4,1,3,5,7,2,8,6},t[8],i;
	for(i=0;i<8;i++)// Performing Permutation on input bits!!
		if(i<4)
			l[i]=pt[ip[i]-1];
		else
			r[i-4] = pt[ip[i]-1];
	cmp_fun(c);//Round 0+1 using key 0+1
	for(i=0;i<4;i++) //Swapping left & right
		r[i]=l[i]+r[i],l[i]=r[i]-l[i],r[i]=r[i]-l[i];
	printf("\n\n");
	cmp_fun(!c); // Round 1+1 wid key1+1 wid swapped bits
	for(i=0;i<8;i++)
		if(i<4)	t[i]=l[i];
		else	t[i]=r[i-4];
	for(i=0;i<8;i++)
		ct[i] = t[ipi[i]-1];
}
void main()
{
	int pt[8]={0},i;
	printf("Enter plain text binary bits:");
	for(i=0;i<8;i++)
		scanf("%d",&pt[i]);
	gen_keys(); // Generating Keys key1 & key2
	En_De(pt,0);
		printf("\nCipher Text :");
			for(i=0;i<8;i++)
				printf("%d",ct[i]);
	//Decrypting - - -
	En_De(ct,1);
		printf("\nPlain Text (After Decrypting):");
				for(i=0;i<8;i++)
				printf("%d",ct[i]);
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.