Sie sind auf Seite 1von 11

1.WAP to implement Caesar cipher....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define caesar(x) rot(13, x)
#define decaesar(x) rot(13, x)
#define decrypt_rot(x, y) rot((26-x), y)
void rot(int c, char *str)
{
int l = strlen(str);
const char *alpha[2] = { "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOP
QRSTUVWXYZ"};
int i;
for (i = 0; i < l; i++)
{
if (!isalpha(str[i]))
continue;
str[i] = alpha[isupper(str[i])][((int)(tolower(str[i])-'a')+c)%2
6];
}
}

18 7 4 8 18 11 8 18 19 4 13 8 13 6
15 0 18 2 0 11 15 0 18 2 0 11 15 0
The cipher text is: H H W K S W X S L G N T C G
*/

int main()
{
char str[] = "This is a top secret text message!";
printf("Original: %s\n", str);
caesar(str);
printf("Encrypted: %s\n", str);
decaesar(str);
printf("Decrypted: %s\n", str);
return 0;
}

2.WAP to implement Railfence cipher.,,


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,k,l;
char a[20],c[20],d[20];
clrscr();
printf("\nEnter the input string : ");
gets(a);
l=strlen(a);
/*Ciphering*/
for(i=0,j=0;i<l;i++)
{
if(i%2==0)
c[j++]=a[i];
}
for(i=0;i<l;i++)
{
if(i%2==1)
c[j++]=a[i];
}
c[j]='\0';
printf("\nCipher text after applying rail fence :");
printf("\n%s",c);
/*Deciphering*/
if(l%2==0)
k=l/2;
else
k=(l/2)+1;
for(i=0,j=0;i<k;i++)
{
d[j]=c[i];
j=j+2;
}
for(i=k,j=1;i<l;i++)
{
d[j]=c[i];
j=j+2;
}
d[l]='\0';
printf("\nText after decryption : ");
printf("%s",d);
getch();
}

3.write-c-program-for-hill-cipher...
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char aa[26]="abcdefghijklmnopqrstuvwxyz";
char pt[10];
int m,d,q=0,i,j,k[2][2],p[4],pp[4],t[5];
int k1[2][2],k2[2][2],det;
clrscr();
printf("enter the plaintext:" );
scanf("%s",pt);
m=strlen(pt);
printf("enter the numbers:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&k[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=i;j<26;j++)
{
if(pt[i]==aa[j])
{
t[q]=j%26;
++q;
}
}
}
p[0]=(k[0][0]*t[0])+(k[0][1]*t[1]);
p[1]=(k[1][0]*t[0])+(k[1][1]*t[1]);
p[2]=(k[0][0]*t[2])+(k[0][1]*t[3]);
p[3]=(k[1][0]*t[2])+(k[1][1]*t[3]);
k1[0][0]=k[1][1];
k1[0][1]=-(k[0][1]);
k1[1][0]=-(k[1][0]);
k1[1][1]=k[0][0];
det=(k1[0][0]*k1[1][1])-(k1[0][1]*k1[1][0]);
for(i=0;i<26;i++)
{
if((det*i)%26==1)
{
d=i;
}
}
for(i=0;i<2;i++)
{

for(j=0;j<2;j++)
{
k2[i][j]=(d*k1[i][j])%26;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(k2[i][j]<0)
k2[i][j]+=26;
}
}
pp[0]=((k2[0][0]*p[0])+(k2[0][1]*p[1]))%26;
pp[1]=((k2[1][0]*p[0])+(k2[1][1]*p[1]))%26;
pp[2]=((k2[0][0]*p[2])+(k2[0][1]*p[3]))%26;
pp[3]=((k2[1][0]*p[2])+(k2[1][1]*p[3]))%26;
for(i=0;i<m;i++)
{
printf("\nThe decrypted plain text :%c",aa[pp[i]]);
}
getch();
}

4.WAP to implement playfair cipher...


#include <stdio.h>
#define siz 5
void encrypt(int *i, int *j)
{
(*i)++,(*j)++;
if((*i)==siz) *i=0;
else if((*j)==siz) *j=0;
}
void playfair(char ch1,char ch2, char mat[siz][siz])
{
int j,m,n,p,q,c,k;
for(j=0,c=0;(c<2)||(j<siz);j++)
for(k=0;k<siz;k++)
if(mat[j][k] == ch1)
m=j,n=k,c++;
else if(mat[j][k] == ch2)
p=j,q=k,c++;
if(m==p)
encrypt(&n,&q);
else if(n==q)
encrypt(&m,&p);
else
n+=q,q=n-q,n-=q;
printf("%c%c",mat[m][n],mat[p][q]);
}
void main()
{
char mat[siz][siz],key[10],str[25]={0};
int m,n,i,j;
char temp;

printf("Enter Key:");
gets(key);
m=n=0;
for(i=0;key[i]!='\0';i++)
{
for(j=0;j<i;j++)
if(key[j] == key[i]) break;
if(key[i]=='j') key[i]='i';
if(j>=i)
{
mat[m][n++] = key[i];
if(n==siz)
n=0,m++;
}
}
for(i=97;i<=122;i++)
{
for(j=0;key[j]!='\0';j++)
if(key[j] == i)
break;
else if(i=='j')
break;
if(key[j]=='\0')
{
mat[m][n++] = i;
if(n==siz) n=0,m++;
}
}
printf("Enter input String:");
gets(str);
printf("\n\nMatrix :\n");
for(i=0;i<siz;i++)
{
for(j=0;j<siz;j++)
printf("%c\t",mat[i][j]);
printf("\n");
}
printf("\n\nEntered text :%s\nCipher Text :",str);
for(i=0;str[i]!='\0';i++)
{
temp = str[i++];
if(temp == 'j') temp='i';
if(str[i]=='\0')
playfair(temp,'x',mat);
else
{
if(str[i]=='j') str[i]='i';
if(temp == str[i])
{
playfair(temp,'x',mat);
playfair('x',str[i],mat);
}
else
playfair(temp,str[i],mat);
}
}
}

5.RSA algorithm in C...


#include <stdio.h>
int ex_gcd(int a,int b,int n) //computes the GCD using the Extended Euclid metho
d
{
int x=0,y=1,lastx=1,lasty=0;
int temp,q;
while(b!=0)
{
temp =b;
q = a/b;
b = a%b;
a = temp;
temp=x;
x = lastx - q*x;
lastx = temp;
temp =y;
y = lasty - q*y;
lasty = temp;
}
if(n==1) return a;
else return lasty;
}
long en_de(int base, int exp,int n)
{
int b[30],i,c=0;
long d=1;
for(i=0;exp!=0;exp/=2,i++)
b[i]=exp%2;
i--;
for(;i>=0;i--)
{
c = 2*c;
d = (d*d) %n;
if(b[i]==1)
{
c = c+1;
d = (d*base)%n;
}
}
return d;
}
void main()
{
int p,q,i;
int pt,ct;
int e,d,et,n,temp;
printf("Enter prime No.s p,q :");

scanf("%d %d",&p,&q);
n = p*q;
et=(p-1)*(q-1);
for(i=2;i<et;i++)
if(ex_gcd(et,i,1) == 1)
printf("%d\t",i);
printf("\nSelect e value:");
scanf("%d",&e);
temp = ex_gcd(et,e,2);
d = et+temp;
printf("\nPublic Key KU = {%d,%d}\n",e,n);
printf("Private Key KR = {%d,%d}\n",d,n);
printf("Enter Plain text M Integer (0<M<%d):",n);
scanf("%d",&pt);
ct = en_de(pt,e,n);
printf("\nCipher Text = %d",ct);
printf("\n\nPlan Text After decrtion :%d",en_de(ct,d,n));
}

6.WAP to implement Diffie-Hellman in C...


#include <stdio.h>
#include <math.h>
void main()
{
int q,alpha,xa,xb,ya,yb,ka,kb, x,y,z,count,ai[20][20];
printf("Enter a Prime Number \"q\":");
scanf("%d",&q);
printf("Enter a No \"xa\" which is lessthan value of q:");
scanf("%d",&xa);
printf("Enter a No \"xb\" which is lessthan value of q:");
scanf("%d",&xb);
for(x=0;x<q-1;x++) //Primitive Root Calculation
for(y=0;y<q-1;y++)
ai[x][y] = ((int)pow(x+1,y+1))%q;
for(x=0;x<q-1;x++)
{
count = 0;
for(y=0;y<q-2;y++)
{
for(z=y+1;z<q-1;z++)
if(ai[x][y] == ai[x][z])
{
count = 1;
break;
}
if(count == 1)
break;
}

if (count == 0 )
{
alpha = x+1;
break;
}
}
printf("alpha = %d\n",alpha);
ya = ((int)pow(alpha,xa))%q; yb = ((int)pow(alpha,xb))%q;
ka = ((int)pow(yb,xa))%q; kb = ((int)pow(yb,xb))%q;
printf("ya = %d\nyb = %d\nka = %d\nkb = %d\n",ya,yb,ka,kb);
if(ka == kb) printf("The keys exchanged are same");
else printf("The keys exchanged are not same");
}

7.KNAPSACK...
KNAPSACK ALGO...
# include<stdio.h>
# include<conio.h>
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp= 0;
int i, j, u;
u=capacity;
for (i=0;i<n;i++)
x[i]=0.0;
for (i=0;i<n;i++)
{
if(weight[i]>u)
break;
else
{
x[i]=1.0;
tp= tp+profit[i];
u=u-weight[i];
}
}
if(i<n)
x[i]=u/weight[i];
tp= tp + (x[i]*profit[i]);
printf("n The result vector is:- ");
for(i=0;i<n;i++)
printf("%ft",x[i]);
printf("m Maximum profit is:- %f", tp);
}
void main()
{

float weight[20], profit[20], capacity;


int n, i ,j;
float ratio[20], temp;
clrscr();
printf ("n Enter the no. of objects:- ");
scanf ("%d", &num);
printf ("n Enter the wts and profits of each object:- ");
for (i=0; i<n; i++)
{
scanf("%f %f", &weight[i], &profit[i]);
}
printf ("n enter the capacityacity of knapsack:- ");
scanf ("%f", &capacity);
for (i=0; i<n; i++)
{
ratio[i]=profit[i]/weight[i];
}
for(i=0; i<n; i++)
{
for(j=i+1;j< n; j++)
{
if(ratio[i]<ratio[j])
{
temp= ratio[j];
ratio[j]= ratio[i];
ratio[i]= temp;
temp= weight[j];
weight[j]= weight[i];
weight[i]= temp;
temp= profit[j];
profit[j]= profit[i];
profit[i]= temp;
}
}
}
knapsack(n, weight, profit, capacity);
getch();
}
Output :
Enter the no. of objects:- 7
Enter the wts and profits of each object:2 10
3 5
5 15
7 7
1 6
4 18
1 3
Enter the capacity of knapsack:- 15

The result vector is:- 1.000000


1.000000
0.666667

1.000000
0.000000

1.000000

Maximum profit is:- 55.333332

8.Vignere Cipher/....
//Aim:To implement vigenere cipher in C.
//Program:
#include<stdio.h>
void main()
{
int i;
int kl;
int pl;
printf("Enter the length of the key stream. ");
scanf("%d",&kl);
printf("Enter the length of the plain text stream.(Without spaces) ");
scanf("%d",&pl);
char p[pl];
char k[kl];
printf("\nEnter the Key. ");
for(i=-1;i<kl;++i)
scanf("%c",&k[i]);
printf("\nEnter the Plain text. ");
for(i=-1;i<pl;++i)
scanf("%c",&p[i]);
int s[3][pl];
for(i=0;i<pl;++i)
{
if(65<=p[i] && p[i]<=91)
s[0][i]=p[i]%65;
else
s[0][i]=p[i]%97;
}
for(i=0;i<pl;++i)
printf("%d ",s[0][i]);
int count=0;
while(count<pl)
{
for(i=0;i<kl;++i)
{
if(65<=k[i] && k[i]<=91)
s[1][count+i]=k[i]%65;
else
s[1][count+i]=k[i]%97;
}
count=count+kl;
}

1.000000

printf("\n");
for(i=0;i<pl;++i)
printf("%d ",s[1][i]);
printf("\n");
for(i=0;i<pl;++i)
printf("%d ",s[2][i]);
printf("\n\nThe cipher text is: ");
char cipher[kl];
for(i=0;i<pl;++i)
{
s[2][i]=(s[0][i]+s[1][i])%26;
cipher[i]=(char)(s[2][i]+65);
printf("%c ",cipher[i]);
}
}
/*Output
lab2@lab2-OptiPlex-330:~/Desktop$ gcc vigenere.c
lab2@lab2-OptiPlex-330:~/Desktop$ ./a.out
Enter the length of the key stream. 6
Enter the length of the plain text stream.(Without spaces) 14
Enter the Key. pascal
Enter the Plain text. sheislistening

Das könnte Ihnen auch gefallen