Beruflich Dokumente
Kultur Dokumente
#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;
}
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();
}
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);
}
}
}
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));
}
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()
{
1.000000
0.000000
1.000000
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