Cryptography Algorithms
Cryptography Algorithms
C Programs
Caeser Cipher
#include<stdio.h>
#include<string.h>
int main()
{
char p[100],c[100],r[100];
int key=3,i;
printf("Enter the text");
scanf("%s",p);
//loop to access all the characters of text
for(i=0;i<strlen(p);i++)
{
c[i]=p[i]+key;
}
printf("\n cipher text %s:",c);
for(i=0;i<strlen(p);i++)
{
r[i]=c[i]-key;
}
Vernam cipher
// Encryption
#include<stdio.h> for (i=0;key[i]!=NULL;i++) {
char arr[26][26]; c=findRow(key[i]);
char message[22],key[22],emessage[22],retMessage[22];r=findColumn(message[i]);
int findRow(char); emessage[i]=arr[r][c];
int findColumn(char); }
int findDecRow(char,int); emessage[i]='\0';
int main() {
printf("\n Encrypted message is:\n\n");
int i=0,j,k,r,c;
for (i=0;emessage[i]!=NULL;i++)
clrscr();
printf("%c",emessage[i]);
k=96;
//decryption
for (i=0;i<26;i++) {
for (i=0;key[i]!=NULL;i++) {
k++;
c=findColumn(key[i]);
for (j=0;j<26;j++) {
r=findDecRow(emessage[i],c);
arr[i][j]=k++;
retMessage[i]=arr[r][0];
if(k==123)
k=97;
}
} retMessage[i]='\0';
} printf("\n\nMessage Retrieved is:\n\n");
printf("\nEnter message\n"); for (i=0;retMessage[i]!=NULL;i++)
gets(message); printf("%c",retMessage[i]);
int findRow(char c) {
int i;
for (i=0;i<26;i++) {
if(arr[0][i]==c)
return(i);
}
}
int findColumn(char c) {
int i;
for (i=0;i<26;i++) {
if(arr[i][0]==c)
return(i);
}
}
int findDecRow(char c,int j) {
int i;
for (i=0;i<26;i++) {
if(arr[i][j]==c)
return(i);
}
Rail Fence Technique
•
#include<stdio.h> if(l%2==0)
#include<string.h>
void main() k=l/2;
{ else
int i,j,k,l;
char a[20],c[20],d[20]; k=(l/2)+1;
printf("\n\t\t RAIL FENCE TECHNIQUE"); for(i=0,j=0;i<k;i++)
printf("\n\nEnter the input string : "); {
gets(a);
l=strlen(a); d[j]=c[i];
/*Ciphering*/ j=j+2;
for(i=0,j=0;i<l;i++)
{ }
if(i%2==0) for(i=k,j=1;i<l;i++)
c[j++]=a[i]; {
}
for(i=0;i<l;i++) d[j]=c[i];
{ j=j+2;
if(i%2==1)
c[j++]=a[i]; }
} d[l]='\0';
c[j]='\0'; printf("\nText after decryption : ");
printf("\nCipher text after applying rail fence :");
printf("\n%s",c); printf("%s",d);
/*Deciphering*/ }
Simple Columnar Transposition
strcpy(temp,key);
#include<stdio.h>
klen=strlen(key);
#include<string.h>
k=0;
void cipher(int i,int c);
for(i=0; ;i++)
int findMin();
{
void makeArray(int,int);
if(flag==1)
char arr[22][22],darr[22][22],emessage[111],retmessage[111],key[55];
break;
char temp[55],temp2[55];
for(j=0;key[j]!=NULL;j++)
int k=0;
{
int main()
if(message[k]==NULL)
{
{
char *message,*dmessage;
flag=1;
int i,j,klen,emlen,flag=0;
arr[i][j]='-';
int r,c,index,min,rows;
}
clrscr();
else
printf("Enetr the key\n"); {
fflush(stdin); arr[i][j]=message[k++];
gets(key); }
printf("\nEnter message to be ciphered\n"); }
fflush(stdin); }
emlen=strlen(emessage);
for(i=0;i<r;i++) //emlen is length of encrypted message
{
for(j=0;j<c;j++) strcpy(temp,key);
{
printf("%c ",arr[i][j]); rows=emlen/klen;
} //rows is no of row of the array to made from ciphered message
printf("\n"); rows;
} j=0;
k=0;
for(i=0;i<klen;i++)
for(i=0,k=1;emessage[i]!=NULL;i++,k++)
{
{
index=findMin();
//printf("\nEmlen=%d",emlen);
cipher(index,r);
temp2[j++]=emessage[i];
} if((k%rows)==0)
emessage[k]='\0'; {
printf("\nEncrypted message is\n"); temp2[j]='\0';
for(i=0;emessage[i]!=NULL;i++) index=findMin();
printf("%c",emessage[i]); makeArray(index,rows);
printf("\n\n"); j=0;
printf("\nArray Retrieved is\n"); void cipher(int i,int r)
k=0; {
for(i=0;i<r;i++) int j;
{ for(j=0;j<r;j++)
{
for(j=0;j<c;j++)
{
{
emessage[k++]=arr[j][i];
printf("%c ",darr[i][j]);
}
//retrieving message }
retmessage[k++]=darr[i][j]; // emessage[k]='\0';
} }
printf("\n");
} void makeArray(int col,int row)
retmessage[k]='\0'; {
printf("\nMessage retrieved is\n"); int i,j;
for(i=0;retmessage[i]!=NULL;i++)
for(i=0;i<row;i++)
printf("%c",retmessage[i]);
{
getch();
darr[i][col]=temp2[i];
return(0);
}
int findMin()
{
int i,j,min,index;
min=temp[0];
index=0;
for(j=0;temp[j]!=NULL;j++)
{
if(temp[j]<min)
{
min=temp[j];
index=j;
}
}
temp[index]=123;
return(index);
}
Diffie Hellman Key Exchange
#include <math.h>
#include <stdio.h> // Alice will choose the private key a
// Power function to return value of a ^ b mod P a = 4; // a is the chosen private key
long long int power(long long int a, long long int b, printf("The private key a for Alice : %lld\n", a);
long long int P) x = power(G, a, P); // gets the generated key
{
if (b == 1) // Bob will choose the private key b
return a; b = 3; // b is the chosen private key
else printf("The private key b for Bob : %lld\n\n", b);
return (((long long int)pow(a, b)) % P); y = power(G, b, P); // gets the generated key
}
// Driver program // Generating the secret key after the exchange
int main() // of keys
{ ka = power(y, a, P); // Secret key for Alice
long long int P, G, x, a, y, b, ka, kb; kb = power(x, b, P); // Secret key for Bob
// Both the persons will be agreed upon the
// public keys G and P printf("Secret key for the Alice is : %lld\n", ka);
P = 23; // A prime number P is taken printf("Secret Key for the Bob is : %lld\n", kb);
printf("The value of P : %lld\n", P);
G = 9; // A primitive root for P, G is taken return 0;