Implement Ceaser, Playfair and Rail Fence Ciphers.: Tools / Apparatus: O.S.: Microsoft Windows (Any) / Linux / DOS
Implement Ceaser, Playfair and Rail Fence Ciphers.: Tools / Apparatus: O.S.: Microsoft Windows (Any) / Linux / DOS
Implement Ceaser, Playfair and Rail Fence Ciphers.: Tools / Apparatus: O.S.: Microsoft Windows (Any) / Linux / DOS
Aim:Write a program for Ceaser cipher ,Playfair & Railfence encryption and decryption
using files
Procedure:
Algorithm Encryption:
3. Read one by one character of file-1 and call encrypt function write the cipher character in
file2.
Algorithm Decryption:
Note: Use an integer digit from 1-26 key. The same key is used for Encryption and
Decryption.
Encrypt function:
if character is between A to Z .
code = character + key;
cipher_character = to_char(code);
Decryption function:
if character is between A to Z .
code=code+26;
original_character = to_char(code);
Code:
#include<stdio.h>
void decrypt(){
char message[100], ch;
int i, key;
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
message[i] = ch;
}
}
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt:");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s\n", message);
decrypt();
return 0;
}
Output:-
1.b
Procedure:
To Generate the key matrix take any random key of any length and form a 5X5matrix. Go on
filling the rows of the matrix with the key characters (if repeating character occurs then
ignore it). Fill the remaining matrix with alphabets from A to Z(except those already occurred
in the key). For example for the key “monarchy” we have the matrix as follow
Step 2: Encrypt the data using encryption rule and key matrix
To Encrypt the data take two characters at time from plain text file and encrypt itusing one of
the following rules.
Encryption rules
1) Repeating plain text letters that would fall in the same pair are separated withfiller
letter,such as x.( i.e. Balloon becomes Ba, lx, lo, on).
2) If both the characters are in the same raw then replace each with the character toits right,
with
3) If both the characters are in the same column then replace each with the characterbelow it,
withthe bottom character followed by the top, in the matrix.
4) Otherwise each plain text letter is replaced by the letter that lies in its own rowand the
column occupied by the other plain text letter
- Encryption of AR as RM
- Encryption of MU as CM
- Encryption of BP as IM
For this solution we have to implement the following functions given below.
2) Matrix generation.
CODE:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MX 5
char keystr[10];
int ind = 0;
int *removeDuplicate(int n)
int i;
int j;
if (keystr[i] == keystr[j])
break;
if (j == i)
keystr[ind++] = keystr[i];
return 0;
int i, j, w, x, y, z;
FILE * out;
printf("File Currupted.");
}
if (ch1 == key[i][j])
w = i;
x = j;
y = i;
z = j;
if (w == y)
x = (x + 1) % 5;
z = (z + 1) % 5;
printf("%c%c", key[w][x], key[y][z]);
else if (x == z)
w = (w + 1) % 5;
y = (y + 1) % 5;
else
fclose(out);
int i, j, w, x, y, z;
FILE * out;
printf("File Currupted.");
if (ch1 == key[i][j])
w = i;
x = j;
y = i;
z = j;
}
if (w == y)
x = (x - 1) % 5;
z = (z - 1) % 5;
else if (x == z)
w = (w - 1) % 5;
y = (y - 1) % 5;
else
fclose(out);
}
int main()
int i, j, k = 0, m = 0, n;
char alpa[26] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
printf("\nEnter key:");
gets(keystr);
n = strlen(keystr);
removeDuplicate( n);
n = ind;
keystr[i] = toupper(keystr[i]);
gets(str);
//convert all the characters of plaintext to uppertext
str[i] = toupper(str[i]);
j = 0;
if (k == n)
keyminus[j] = alpa[i];
j++;
}
}
k = 0;
if (k < n)
key[i][j] = keystr[k];
k++;
else
key[i][j] = keyminus[m];
m++;
printf("\n");
}
printf("1 encription\n2 decription");
int z;
scanf("%d",&z);
if(z==1)
else
else
i++;
}
}
else if(z==2)
else
else
i++;
}
}
return 0;
Output:
1.3
AIM:- Implementation of encryption and decryption using railfence cipher
Procedure:
Rail Fence cipher is a Transposition cipher. Encryption is the result by changing the position
of the message. In this particular scheme the message is written in two rows. That is the first
character is written in the first row, second character is written in the second row and so on.
To get the cipher read the message off, row by row, first row followed by second row.
Algorithm Encryption:
Algorithm Decryption:
3. k=strlen(CT)/2
4. i=0,j=0;
5. PT[i]=CT[i]
6. PT[i+1]=CT[k]
7. i++,j++,k++
Code:-
#include<stdio.h>
#include<string.h>
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = msg[i];
row = row + k;
if(railMatrix[i][j] != '\n')
printf("%c", railMatrix[i][j]);
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){
railMatrix[row][col++] = '*';
k= k * (-1);
row = row + k;
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
printf("%c", railMatrix[row][col++]);
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
int main(){
char msg[100];
int key,k;
gets(msg);
scanf("%d",&k);
if(k==1)
scanf("%d",&key);
encryptMsg(msg, key);
}
else if(k==2)
scanf("%d",&key);
decryptMsg(msg, key);
else
return 0;
Output:-
2. AIM: Implementation of encryption and decryption using S-DES algorithm.
Procedure:
S-DES is a simplified version of DES. S-DES algorithm is used for the academic purpose.
S-DES uses bit wise operation on message letters to encrypt the data so it is more powerful
against the cryptanalysis attacks. This algorithm takes 8-bitof the message as input, also takes
10 bit key and produces 8 bit cipher text. This algorithm has two rounds. It generates 2, 8-bit
keys that are to be used in each round. Following figure shows the functional details of
S-DES.
Code:
#include <stdio.h>
int l[4],r[4],keys[2][8],ct[8];
void sbox(int sip[],int p[],int sbno,int i)
{
int sbox[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
int rw,c,sop;
rw = sip[3]+sip[0]*2;
c = sip[2]+sip[1]*2;
sop = sbox[sbno][rw][c];
for(;sop!=0;sop/=2)
p[i--]=sop%2;
}
void cmp_fun(int round)
{
int EP[]={4,1,2,3,2,3,4,1},i,epd[8];
int slip[4],srip[4];
int p[4]={0},p4[]={2,4,3,1},np[4];
for(i=0;i<8;i++)
epd[i]=r[EP[i]-1];
for(i=0;i<8;i++)
if(i<4)
slip[i] = epd[i]^keys[round][i];
else
srip[i-4] = epd[i]^keys[round][i];
sbox(slip,p,0,1);
sbox(srip,p,1,3);
for(i=0;i<4;i++)
np[i]=p[p4[i]-1];
for(i=0;i<4;i++)
l[i] = l[i]^np[i];
}
void left_shift(int keyip[],int nob)
{
int t1,t2,i;
while(nob>0)
{
t1=keyip[0],t2=keyip[5];
for(i=0;i<9;i++)
if(i<4)
keyip[i] =keyip[i+1];
else if(i>4)
keyip[i] = keyip[i+1];
keyip[4]=t1,keyip[9]=t2;
nob--;
}
}
void gen_keys()
{
int key[10],i,keyip[10];
int p10[]={3,5,2,7,4,10,1,9,8,6},p8[]={6,3,7,4,8,5,10,9};
printf("Enter Key :");
for(i=0;i<10;i++)
scanf("%d", &key[i]);
for(i=0;i<10;i++)
keyip[i] = key[p10[i]-1];
left_shift(keyip,1);
printf("\nKey1 :");
for(i=0;i<8;i++){
keys[0][i] = keyip[p8[i]-1];
printf("%d",keys[0][i]);
}
left_shift(keyip,2);
printf("\nKey2 :");
for(i=0;i<8;i++){
keys[1][i] = keyip[p8[i]-1];
printf("%d",keys[1][i]);
}
}
void En_De(int pt[],int c)
{
int ip[]={2,6,3,1,4,8,5,7},ipi[]={4,1,3,5,7,2,8,6},t[8],i;
for(i=0;i<8;i++)
if(i<4)
l[i]=pt[ip[i]-1];
else
r[i-4] = pt[ip[i]-1];
cmp_fun(c);
for(i=0;i<4;i++)
r[i]=l[i]+r[i],l[i]=r[i]-l[i],r[i]=r[i]-l[i];
printf("\n\n");
cmp_fun(!c);
for(i=0;i<8;i++)
if(i<4) t[i]=l[i];
else t[i]=r[i-4];
for(i=0;i<8;i++)
ct[i] = t[ipi[i]-1];
}
void main()
{
int pt[8]={0},i;
printf("Enter plain text binary bits:");
for(i=0;i<8;i++)
scanf("%d",&pt[i]);
gen_keys();
En_De(pt,0);
printf("\nCipher Text :");
for(i=0;i<8;i++)
printf("%d",ct[i]);
En_De(ct,1);
printf("\nPlain Text (After Decrypting):");
for(i=0;i<8;i++)
printf("%d",ct[i]);
}
Output:
3.
AIM: Implement RSA asymmetric (public key and private key) EncryptionImplement
Euclidean and Extended Euclidean algorithm for calculating the GCD.
Algorithm:
Key generation:
-1
Step5: Determinedsuchthatd=e modФ(n)
Encryption:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
char msg[100];
void ce();
void encrypt();
void decrypt();
void main() {
scanf("%d",&p);
flag=prime(p);
if(flag==0) {
printf("\nWRONG INPUT\n");
getch();
exit(1);
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q) {
printf("\nWRONG INPUT\n");
getch();
exit(1);
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for (i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
for (i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
getch();
int i;
j=sqrt(pr);
for (i=2;i<=j;i++) {
if(pr%i==0)
return 0;
return 1;
void ce() {
int k;
k=0;
for (i=2;i<t;i++) {
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q) {
e[k]=i;
flag=cd(e[k]);
if(flag>0) {
d[k]=flag;
k++;
if(k==99)
break;
while(1) {
k=k+t;
if(k%x==0)
return(k/x);
void encrypt() {
i=0;
len=strlen(msg);
while(i!=len) {
pt=m[i];
pt=pt-96;
k=1;
for (j=0;j<key;j++) {
k=k*pt;
k=k%n;
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;
en[i]=-1;
for (i=0;en[i]!=-1;i++)
printf("%c",en[i]);
void decrypt() {
for (j=0;j<key;j++) {
k=k*ct;
k=k%n;
pt=k+96;
m[i]=pt;
i++;
m[i]=-1;
for (i=0;m[i]!=-1;i++)
printf("%c",m[i]);
Output: