Implement Ceaser, Playfair and Rail Fence Ciphers.: Tools / Apparatus: O.S.: Microsoft Windows (Any) / Linux / DOS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

1.Implement ceaser, Playfair and rail fence ciphers.

Aim:Write a program for Ceaser cipher ,Playfair & Railfence encryption and decryption
using files

Tools / Apparatus: O.S.: Microsoft Windows (any) / Linux / DOS

Packages: Turbo/Borland/GNU - C/C++/Java

Procedure:

Algorithm Encryption:

1. Open a file which contains the plain text in read mode

2. Create a new file to which the cipher to be written.

3. Read one by one character of file-1 and call encrypt function write the cipher character in
file2.

4. Close the files.

Algorithm Decryption:

1. Open a file which contains the cipher text in read mode

2. Read one by one character of file and call decrypt function

3. Close the file.

Note: Use an integer digit from 1-26 key. The same key is used for Encryption and
Decryption.

Encrypt function:

Read the key

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= character – key;

code=code+26;

original_character = to_char(code);

Code:
#include<stdio.h>
void decrypt(){
char message[100], ch;
int i, key;

printf("Enter a message to decrypt: ");


// gets(message);
scanf("%s",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 < 'a'){


ch = ch + 'z' - 'a' + 1;
}

message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;

if(ch < 'A'){


ch = ch + 'Z' - 'A' + 1;
}

message[i] = ch;
}
}

printf("Decrypted message: %s", message);

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

AIM: Implementation of Play Fair Cipher Encryption

Tools / Apparatus: O.S.: Microsoft Windows (any) / Linux / DOS

Packages: Turbo/Borland/GNU - C/C++

Procedure:

Step1: Generating Key matrix

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

the last character followed by the first, in the matrix.

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

Example:Using key as “monarchy” we have

- Encryption of AR as RM

- Encryption of MU as CM

- Encryption of BP as IM

Designing the Solution:

For this solution we have to implement the following functions given below.

1) Input function for key & Plain Text.

2) Matrix generation.

3) Encryption function for generating Cipher Text.

4) Print function for printing Cipher Text Output.

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;

for (i=0; i<n; i++) {

int j;

for (j=0; j<i; j++)

if (keystr[i] == keystr[j])

break;

if (j == i)

keystr[ind++] = keystr[i];

return 0;

void playfair(char ch1, char ch2, char key[MX][MX])

int i, j, w, x, y, z;

FILE * out;

if ((out = fopen("cipher.txt", "a+")) == NULL)

printf("File Currupted.");
}

for (i = 0; i < MX; i++)

for (j = 0; j < MX; j++)

if (ch1 == key[i][j])

w = i;

x = j;

else if (ch2 == key[i][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]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else if (x == z)

w = (w + 1) % 5;

y = (y + 1) % 5;

printf("%c%c", key[w][x], key[y][z]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else

printf("%c%c", key[w][z], key[y][x]);

fprintf(out, "%c%c", key[w][z], key[y][x]);

fclose(out);

void playfaird(char ch1, char ch2, char key[MX][MX])

int i, j, w, x, y, z;
FILE * out;

if ((out = fopen("cipher.txt", "a+")) == NULL)

printf("File Currupted.");

for (i = 0; i < MX; i++)

for (j = 0; j < MX; j++)

if (ch1 == key[i][j])

w = i;

x = j;

else if (ch2 == key[i][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]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else if (x == z)

w = (w - 1) % 5;

y = (y - 1) % 5;

printf("%c%c", key[w][x], key[y][z]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else

printf("%c%c", key[w][z], key[y][x]);

fprintf(out, "%c%c", key[w][z], key[y][x]);

fclose(out);

}
int main()

int i, j, k = 0, m = 0, n;

char key[MX][MX], keyminus[25], str[25] = {0};

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;

//convert the characters to uppertext

for (i = 0; i < n; i++)

if (keystr[i] == 'j') keystr[i] = 'i';

else if (keystr[i] == 'J') keystr[i] = 'I';

keystr[i] = toupper(keystr[i]);

printf("\nEnter the plain text/ chiper text:");

gets(str);
//convert all the characters of plaintext to uppertext

for (i = 0; i < strlen(str); i++)

if (str[i] == 'j') str[i] = 'i';

else if (str[i] == 'J') str[i] = 'I';

str[i] = toupper(str[i]);

// store all characters except key

j = 0;

for (i = 0; i < 26; i++)

for (k = 0; k < n; k++)

if (keystr[k] == alpa[i]) break;

else if (alpa[i] == 'J') break;

if (k == n)

keyminus[j] = alpa[i];

j++;

}
}

//construct key keymatrix

k = 0;

for (i = 0; i < MX; i++)

for (j = 0; j < MX; j++)

if (k < n)

key[i][j] = keystr[k];

k++;

else

key[i][j] = keyminus[m];

m++;

printf("%c ", key[i][j]);

printf("\n");

}
printf("1 encription\n2 decription");

int z;

scanf("%d",&z);

if(z==1)

// construct diagram and convert to cipher text

printf("\n\nEntered text :%s\nCipher Text :", str);

for (i = 0; i < strlen(str); i++)

if (str[i] == 'J') str[i] = 'I';

if (str[i + 1] == '\0') playfair(str[i], 'X', key);

else

if (str[i + 1] == 'J') str[i + 1] = 'I';

if (str[i] == str[i + 1]) playfair(str[i], 'X', key);

else

playfair(str[i], str[i + 1], key);

i++;

}
}

else if(z==2)

printf("\n\nEntered text :%s\nPlain Text :", str);

for (i = 0; i < strlen(str); i++)

if (str[i] == 'J') str[i] = 'I';

if (str[i + 1] == '\0') playfaird(str[i], 'X', key);

else

if (str[i + 1] == 'J') str[i + 1] = 'I';

if (str[i] == str[i + 1]) playfaird(str[i], 'X', key);

else

playfaird(str[i], str[i + 1], key);

i++;

}
}

return 0;

Output:
1.3
AIM:- Implementation of encryption and decryption using railfence cipher

Tools / Apparatus: O.S.: Microsoft Windows (any) / Linux / DOS

Packages: Turbo/Borland/GNU - C/C++

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:

1. Read plain text

2. Consider CT as a temporary string to which cipher is copied

3. Copy all the even indexed letters of the plain text to CT

4. Copy all the odd indexed letters of the plain text to CT

5. CT contains the cipher

Algorithm Decryption:

1. Read cipher text, CT

2. Consider PT as a temporary string to which plain text is copied

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++

8. Repeat steps 5,6,7 till the end of the char is reached in CT

9. PT contains the plain text derived based on cipher

Code:-

#include<stdio.h>

#include<string.h>

void encryptMsg(char msg[], int key){

int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0;

char railMatrix[key][msgLen];

for(i = 0; i < key; ++i)

for(j = 0; j < msgLen; ++j)

railMatrix[i][j] = '\n';

for(i = 0; i < msgLen; ++i){

railMatrix[row][col++] = msg[i];

if(row == 0 || row == key-1)


k= k * (-1);

row = row + k;

printf("\nEncrypted Message: ");

for(i = 0; i < key; ++i)

for(j = 0; j < msgLen; ++j)

if(railMatrix[i][j] != '\n')

printf("%c", railMatrix[i][j]);

void decryptMsg(char enMsg[], int key){

int msgLen = strlen(enMsg), i, j, k = -1, row = 0, col = 0, m = 0;

char railMatrix[key][msgLen];

for(i = 0; i < key; ++i)

for(j = 0; j < msgLen; ++j)

railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){

railMatrix[row][col++] = '*';

if(row == 0 || row == key-1)

k= k * (-1);

row = row + k;

for(i = 0; i < key; ++i)

for(j = 0; j < msgLen; ++j)

if(railMatrix[i][j] == '*')

railMatrix[i][j] = enMsg[m++];

row = col = 0;

k = -1;

printf("\nDecrypted Message: ");

for(i = 0; i < msgLen; ++i){

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;

printf("enter the message: ");

gets(msg);

printf("press 1 for ENCRYPTION\npress 2 for DECRYPTION \n");

scanf("%d",&k);

if(k==1)

printf("enter key: ");

scanf("%d",&key);

printf("Original Message: %s", msg);

encryptMsg(msg, key);
}

else if(k==2)

printf("enter key: ");

scanf("%d",&key);

printf("Original Message: %s", msg);

decryptMsg(msg, key);

else

printf(" enter a valid number");

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:

Step1: Selecttwoprime numbers,p,q.

Step2: Calculaten= p*q

Step3: Calculate Ф(n) =(p-1)(q-1)

Step4: SelectesuchthateisrelativelyprimetoФ(n), gcd(e, Ф(n))=1 andlessthanФ(n);

-1
Step5: Determinedsuchthatd=e modФ(n)

Encryption:

Step1: Read Plain Text, M

Step 2: Find C= M*M mod n

Step 3: Repeat Step 2 for e times

Step 4: C contains cipher


Decryption:

Step1: Read Cipher Text, C

Step 2: Find M= C*C mod n

Step 3: Repeat Step 2 for d times

Step 4: M contains plain text

Code:
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<string.h>

long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;

char msg[100];

int prime(long int);

void ce();

long int cd(long int);

void encrypt();

void decrypt();

void main() {

printf("\nENTER FIRST PRIME NUMBER\n");

scanf("%d",&p);

flag=prime(p);

if(flag==0) {
printf("\nWRONG INPUT\n");

getch();

exit(1);

printf("\nENTER ANOTHER PRIME NUMBER\n");

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();

printf("\nPOSSIBLE VALUES OF e AND d ARE\n");

for (i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);

encrypt();

decrypt();

getch();

int prime(long int pr) {

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;

long int cd(long int x) {

long int k=1;

while(1) {

k=k+t;

if(k%x==0)

return(k/x);

void encrypt() {

long int pt,ct,key=e[0],k,len;

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;

printf("\nTHE ENCRYPTED MESSAGE IS\n");

for (i=0;en[i]!=-1;i++)

printf("%c",en[i]);

void decrypt() {

long int pt,ct,key=d[0],k;


i=0;
while(en[i]!=-1) {
ct=temp[i];
k=1;

for (j=0;j<key;j++) {

k=k*ct;

k=k%n;

pt=k+96;

m[i]=pt;

i++;

m[i]=-1;

printf("\nTHE DECRYPTED MESSAGE IS\n");

for (i=0;m[i]!=-1;i++)

printf("%c",m[i]);

Output:

You might also like