Des in C and Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Network Security & Cryptography

ASSIGNMENT

Anusha Mannem Shruthi Kantheti


04241A0507 04241A0561
CSE-B CSE-B
1) The RSA(Rivest, Shamir and Adleman) Algorithm

RSA Algorithm is based on the exponentials, modulus numbers and prime


numbers. The algorithm can be explained from the following example.

Example:
a.
Select two prime numbers p=3 and q=11.
b.
Calculate n=p*q=3*11=33.
c.
Calculate p(n)=(p-1)*(q-1)=20.
d.
Select integer e such that relatively prime to p(n)=20 and less than
p(n).Let the value of e=7.
e. Calculate d that is d*e=1 mod 20, d*7=1 mod 20 and d<20 and 3*u=21 (1
mod 20) hence d=3.
f. Resulting public key KU={7,33} and Resulting Private key KR={3,33}.
Encryption Process:
Consider character D(assigned value is 4) is plain text
Encryption : 47 mod 33= 16384 mod 33=16
Cipher text is 16 (alphabetic value is P) means cipher text is P.
Decryption Process:
Received P=16 is cipher text
Decryption : 163 mod 33 = 4096 mod 33 = 4
Plain Text is 4 means corresponding value is D.

Source Code for RSA in C:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void en_de();
int k=0,i,j,p,q,pn,d,e,c,n,C,M,temp,t,a;
int br[20],b[20];
int
pr[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
main()
{
clrscr();
printf("Enter any two prime numbers : ");
scanf("%d%d",&p,&q);
printf("\nEnter the plaintext block : ");
scanf("%d",&M);
n=p*q;
pn=((p-1)*(q-1))+1;
for(i=0;i<25;i++)
{
e=pr[i];
for(j=i;j<25;j++)
{
d=pr[j];
c=e*d;
if(c==pn)
{
printf("\n\nPrivate Key {%d,%d}\n\t",e,n);
printf("\n\nPublic Key {%d,%d}\n\t",d,n);
k++;
temp=1;
break;
}
}
if(temp==1)
break;
}
temp=e;t=1;a=M;
en_de();
C=t;
printf("\n\nENCRYPTION\n\tEncrypted plain text block : %d",C);
temp=d;t=1;a=C;
en_de();
M=t;
printf("\n\nDECRYPTION\n\tDecrypted cipher text block : %d",M);
getch();
}
void en_de()
{
i=0;
while(temp!=0)
{
br[i]=temp%2;
temp=temp/2;
i++;
}
for(j=i-1,k=0;j>=0;j--)
{
b[k]=br[j];
k++;
}
temp=0;
for(j=0;j<i;j++)
{
temp=2*temp;
t=(t*t)%n;
if(b[j]==1)
{
temp=temp+1;
t=(t*a)%n;
}
}
}

Output:

Enter any two prime numbers : 17 11


Enter the plaintext block : 88
Private Key {7,187}
Public Key {23,187}
ENCRYPTION
Encrypted plain text block : 11
DECRYPTION
Dencrypted cipher text block : 88

Source Code for RSA in JAVA:

import java.lang.*;
public class RSA_S
{
public static int k=0,i,j,p,q,pn,d,e,c,n,C,M,temp,t,a;
RSA_S()
{
int
pr[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
p=17;q=11;M=88;
n=p*q;
pn=((p-1)*(q-1))+1;
for(i=0;i<25;i++)
{
e=pr[i];
for(j=i;j<25;j++)
{
d=pr[j];
c=e*d;
if(c==pn)
{
System.out.println("\n\nPrivate Key
{"+e+","+n+"}\n\t");
System.out.println("\n\nPublic Key
{"+d+","+n+"}\n\t");
k++;
temp=1;
break;
}
}
if(temp==1)
break;
}
temp=e;t=1;a=M;
en_de();
C=t;
System.out.println("\n\nENCRYPTION\n\t”);
System.out.println(“Encrypted plain text block "+C);
temp=d;t=1;a=C;
en_de();
M=t;
System.out.println("\n\nDECRYPTION\n\t”);
System.out.println(“Decrypted cipher text block "+C);
}
public static void en_de()
{
int br[]=new int[20];
int b[]=new int[20];
i=0;
while(temp!=0)
{
br[i]=temp%2;
temp=temp/2;
i++;
}
for(j=i-1,k=0;j>=0;j--)
{
b[k]=br[j];
k++;
}
temp=0;
for(j=0;j<i;j++)
{
temp=2*temp;
t=(t*t)%n;
if(b[j]==1)
{
temp=temp+1;
t=(t*a)%n;
}
}
}
public static void main(String args[])
{
new RSA_S();
}
}

Output:

Private Key {7,187}


Public Key {23,187}
ENCRYPTION
Encrypted plain text block : 11
DECRYPTION
Dencrypted cipher text block : 88
2) DES (Data Encryption Standard) Algorithm

The Data Encryption Standard (DES) is one of the best block cipher algorithm
used widely in the IT sector for privacy and security purpose.
Structure of DES :
DES Encryption standard takes 64 bits plain text as input and with the help of
56 bit key and produces 64 bits cipher text. First initial permutation takes the entire
64 bits and gives for the first round of operation.
There are 16 rounds in the DES and round takes key of 48 bits that is
generated with the help of 56 bits key.
After completion of 16 rounds a 32 bits swap function is applied for the bits in
final round. Finally inverse initial permutation is applied so that it generates 64-bit
cipher text.

DES Encryption Scheme:


a. In key generation process first 56 bits are given for Permuted Choice 1 (PC1).
The PC1 takes 56 bits as input and produces permuted result of 56 bits. It is
left circular shifted and finally Permuted Choice (PC2), which takes 56 bits as
input and produces permuted result of 48 bits, is applied to get key 1 in the
first round.The same procedure is applied to get the other 15 keys.
b. A 64-bit plain text is taken as input and arranges in permuted model using
Initial Permutation (IP). The result of IP is given to the round 1 which takes
Key 1 from the Key generation algorithm, like these 16 rounds are repeated.
Next a swap of 32 bits is applied for the result of round 16. Finally Inverse
Initial Permutation is applied which results a 64-bit cipher text.
c. Details of Round :
a. The procedure is following by using the following equations.
Li=Ri-1
Ri=Li-1(+)F(Ri-1,Ki)
b. The 64-bit plain text is divided into 2 halves, the second half is
expanded using Expansion/Permutation or E/P resulting 48-bits.
c. The result then passes through 8 S-Boxes, each takes 6-bits as input and
results a 4-bit output. The results from 8 S-Boxes are concatenated. The
output is 32-bits.
d. Then a Permutation is applied on this result which produces a
permuted result of 32-bits.
e. This permuted result is XORed with the left half of the plain text and
this is taken as the right half input for the next round and the right half
is taken as the left half input for the next round.
f. This is repeated for all the other 15 rounds.
Source Code for DES in C:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 100
void round();
void exp_per();
void box();
int plain[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int key[]={0,0,0,0,0,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,0,1,1,0,0,0,1,0,
0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,1,0};
int sbox1[4][16]={{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}};
int sbox2[4][16]={{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}};
int sbox3[4][16]={{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}};
int sbox4[4][16]={{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}};
int sbox5[4][16]={{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}};
int sbox6[4][16]={{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}};
int sbox7[4][16]={{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}};
int sbox8[4][16]={{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}};
int pc1[56]={57,49,41,33,25,17,9,1,58,50,42,34,26,18,
10,2,59,51,43,35,27,19,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,
14,6,61,53,45,37,29,21,13,5,28,20,12,4};
int pc2[48]={14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,

41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};
int per[32]={16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25};
int ip[64]={40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25};
int
pkey[max],ptemp[max],pk[max],ep[max],epx[max],perm[max],pe[max],cipher[
max],shift[30];
int i,j,k,l,m,temp,a,va,v,val,value,y,z,x,n;
void main()
{
clrscr();
for(i=0;i<32;i++)
ptemp[i]=plain[i+32];
for(i=0;i<56;i++)
pkey[i]=key[pc1[i]];
for(i=0;i<16;i++)
round();
for(i=0;i<32;i++)
perm[i]=plain[i+32];
for(i=32;i<63;i++)
perm[i]=plain[i-32];
printf("CIPHER TEXT");
for(i=0;i<64;i++)
{
cipher[i]=perm[ip[i]-1];
if(i%8==0)
printf("\n");
printf("%d\t",cipher[i]);
}
getch();
}
void leftshift()
{
for(m=0;m<l;m++)
{
temp=pkey[j];
for(k=j;k<(j+27);k++)
pkey[k]=pkey[k+1];
pkey[k]=temp;
}
}
void round()
{
l=shift[i];
j=0;
leftshift();
j=28;
leftshift();
for(j=0;j<48;j++)
pk[j]=pkey[pc2[j]-1];
exp_per();
for(j=0;j<48;j++)
{
if((pk[j]==0 && ep[j]==0) || (pk[j]==1 && ep[j]==1))
epx[j]=0;
if((pk[j]==0 && ep[j]==1) || (pk[j]==1 && ep[j]==0))
epx[j]=1;
}
a=0;
va=1;
val=0;
while(1)
{
m=33+a;
z=0;
y=(2*ep[m-1])+ep[m+4];
for(j=3;j>=0;j--)
{
z=z+(pow(2,j))*ep[m];
m++;
}
box();
va++;
a=a+6;
if(va==8)
break;
}
for(j=0;j<32;j++)
{
perm[j]=pe[per[j]-1];
}
for(j=0;j<32;j++)
{
if((plain[j]==1 && perm[j]==1) || (plain[j]==0 && perm[j]==0))
plain[j+32]=0;
else
plain[j+32]=1;
}
for(j=0;j<32;j++)
plain[j]=ptemp[j];
}
void exp_per()
{
ep[0]=plain[63];
n=32;
x=1;
for(j=0;j<8;j++)
{
if(x!=1)
{
ep[x]=plain[n-1];
x++;
}
for(m=0;m<4;m++)
{
ep[x]=plain[n];
x++;
n++;
}
if(x!=47)
{
ep[x]=plain[n];
x++;
}
}
ep[47]=plain[32];
}
void box()
{
switch(va)
{
case 1 :
value=sbox1[y][z];
break;
case 2 :
value=sbox2[y][z];
break;
case 3 :
value=sbox3[y][z];
break;
case 4 :
value=sbox4[y][z];
break;
case 5 :
value=sbox5[y][z];
break;
case 6 :
value=sbox6[y][z];
break;
case 7 :
value=sbox7[y][z];
break;
case 8 :
value=sbox8[y][z];
break;
}
for(v=3;v>=0;v--)
{
pe[val+v]=value%2;
value=value-(value%2);
value=value/2;
}
val=val+4;
}

Output:
Plain text is : 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000
Key used is : 00000001 1001011 0100100 1100010 0011100 0011000 0011100
0110010
Cipher text is : 01101000 10000101 0010111 01111010 00010011 01110110
11101011 10100100

Source Code for DES in JAVA:

import java.lang.*;
public class DES
{
int plain[]={00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000};
int key[]={00000001 1001011 0100100 1100010 0011100 0011000 0011100
0110010};
int sbox1[4][16]={{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}};
int sbox2[4][16]={{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}};
int sbox3[4][16]={{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}};
int sbox4[4][16]={{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}};
int sbox5[4][16]={{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}};
int sbox6[4][16]={{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}};
int sbox7[4][16]={{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}};
int sbox8[4][16]={{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}};
int pc1[56]={57,49,41,33,25,17,9,1,58,50,42,34,26,18,
10,2,59,51,43,35,27,19,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,
14,6,61,53,45,37,29,21,13,5,28,20,12,4};
int pc2[48]={14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,

41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};
int per[32]={16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25};
int ip[64]={40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25};
public static int
pkey[max],ptemp[max],pk[max],ep[max],epx[max],perm[max],pe[max],cipher[
max];
public static int i,j,k,l,m,temp,a,va,v,val,value,y,z,x,n;
DES()
{
for(i=0;i<32;i++)
ptemp[i]=plain[i+32];
for(i=0;i<56;i++)
pkey[i]=key[pc1[i]];
for(i=0;i<16;i++)
round();
for(i=0;i<32;i++)
perm[i]=plain[i+32];
for(i=32;i<63;i++)
perm[i]=plain[i-32];
System.out.println("CIPHER TEXT");
for(i=0;i<64;i++)
{
cipher[i]=perm[ip[i]-1];
if(i%8==0)
System.out.println("\n");
Systm.out.println(cipher[i]+"\t");
}
}
public static void leftshift()
{
for(m=0;m<l;m++)
{
temp=pkey[j];
for(k=j;k<(j+27);k++)
pkey[k]=pkey[k+1];
pkey[k]=temp;
}
}
public static void round()
{
l=shift[i];
j=0;
leftshift();
j=28;
leftshift();
for(j=0;j<48;j++)
pk[j]=pkey[pc2[j]-1];
exp_per();
for(j=0;j<48;j++)
{
if((pk[j]==0 && ep[j]==0) || (pk[j]==1 && ep[j]==1))
epx[j]=0;
if((pk[j]==0 && ep[j]==1) || (pk[j]==1 && ep[j]==0))
epx[j]=1;
}
a=0;
va=1;
val=0;
while(1)
{
m=33+a;
z=0;
y=(2*ep[m-1])+ep[m+4];
for(j=3;j>=0;j--)
{
z=z+(pow(2,j))*ep[m];
m++;
}
box();
va++;
a=a+6;
if(va==8)
break;
}
for(j=0;j<32;j++)
{
perm[j]=pe[per[j]-1];
}
for(j=0;j<32;j++)
{
if(plain[j]==1 && perm[j]==1) || (plain[j]==0 && perm[j]==0))
plain[j+32]=0;
else
plain[j+32]=1;
}
for(j=0;j<32;j++)
plain[j]=ptemp[j];
}
public static void exp_per()
{
ep[0]=plain[63];
n=32;
x=1;
for(j=0;j<8;j++)
{
if(x!=1)
{
ep[x]=plain[n-1];
x++;
}
for(m=0;m<4;m++)
{
ep[x]=plain[n];
x++;
n++;
}
if(x!=47)
{
ep[x]=plain[n];
x++;
}
}
ep[47]=plain[32];
}
public static void box()
{
switch(va)
{
case 1 :
value=sbox1[y][z];
break;
case 2 :
value=sbox2[y][z];
break;
case 3 :
value=sbox3[y][z];
break;
case 4 :
value=sbox4[y][z];
break;
case 5 :
value=sbox5[y][z];
break;
case 6 :
value=sbox6[y][z];
break;
case 7 :
value=sbox7[y][z];
break;
case 8 :
value=sbox8[y][z];
break;
}
for(v=3;v>=0;v--)
{
pe[val+v]=value%2;
value=value-(value%2);
value=value/2;
}
val=val+4;
}

Output:

Plain text is : 00000000 00000000 00000000 00000000 00000000 00000000


00000000 00000000
Key used is : 00000001 1001011 0100100 1100010 0011100 0011000 0011100
0110010
Cipher text is : 01101000 10000101 0010111 01111010 00010011 01110110
11101011 10100100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy