1) Caeser Cipher Technique
#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;
printf("\n Recovered text %s:",r);
2) Vernam Cipher encryption
#include <stdio.h>
#include <ctype.h>
char arr[26][26];
char message[22], key[22], emessage[22], retMessage[22];
int findRow(char);
int findColumn(char);
int findDecRow(char, int);
int main() {
int i = 0, j, k, r, c;
// Initialize the substitution table
k = 96;
for (i = 0; i < 26; i++) {
k++;
for (j = 0; j < 26; j++) {
arr[i][j] = k++;
if (k == 123)
k = 97;
}
}
printf("\nEnter message\n");
fgets(message, sizeof(message), stdin);
printf("\nEnter the key\n");
fgets(key, sizeof(key), stdin);
// Encryption
for (i = 0; key[i] != '\0' && message[i] != '\0'; i++) {
c = findRow(key[i]);
r = findColumn(message[i]);
emessage[i] = arr[r][c];
}
emessage[i] = '\0';
printf("\nEncrypted message is:\n\n");
for (i = 0; emessage[i] != '\0'; i++)
printf("%c", emessage[i]);
// Decryption
for (i = 0; key[i] != '\0' && emessage[i] != '\0'; i++) {
c = findColumn(key[i]);
r = findDecRow(emessage[i], c);
retMessage[i] = arr[r][0];
}
retMessage[i] = '\0';
printf("\n\nMessage Retrieved is:\n\n");
for (i = 0; retMessage[i] != '\0'; i++)
printf("%c", retMessage[i]);
return 0;
}
int findRow(char c) {
int i;
for (i = 0; i < 26; i++) {
if (arr[0][i] == c)
return i;
}
return -1; // Character not found
}
int findColumn(char c) {
int i;
for (i = 0; i < 26; i++) {
if (arr[i][0] == c)
return i;
}
return -1; // Character not found
}
int findDecRow(char c, int j) {
int i;
for (i = 0; i < 26; i++) {
if (arr[i][j] == c)
return i;
}
return -1; // Character not found
}
3) Rail fence technique
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,k,l;
char a[20],c[20],d[20];
printf("\n\t\t RAIL FENCE TECHNIQUE");
printf("\n\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);
}