You are on page 1of 31

ST.

XAVIER’S COLLEGE

(Affiliated to Tribhuwan University)

Maitighar, Kathmandu

Cryptography
Lab 1

Submitted By:

Kshitiz Adhikari (011BScCSIT019)

5th semester

Submitted To:

Er. Anil Shah

Lecturer

Department of Computer Science and Information Technology

Downloaded from CSIT Tutor


Caesar Cipher

Algorithm:

1. Input plaintext

2. Process each letter x at once

3. For a shift by n characters

a. Encryption, E(x) = (x + n) mod 26.

b. Decryption, D(x) = (x - n) mod 26.

4. Display the encrypted and decrypted message as required

5. End the program.

Source code:

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
char a[100],c[100];
int i,z,b,n;
cout<<"Enter string:";
cin.getline(a,256);
b=strlen(a);
cout<<"Enter the encryption key:";

Downloaded from CSIT Tutor


cin>>n;

for(i=0;i<b;i++)
{
if(a[i]==' ')
c[i]=a[i];
else
{
c[i]=toupper(a[i]);
c[i]=(c[i]+n)%90;
}
}
cout<<"Encryption:"<<c;

for(i=0;i<b;i++)
{
if(c[i]==' ')
a[i]=c[i];
else
{
a[i]=tolower(c[i]);
a[i]=(a[i]-n)%122;
}
}
cout<<endl<<"Decryption:"<<a;

getch();
}

Downloaded from CSIT Tutor


Output:

Downloaded from CSIT Tutor


ST. XAVIER’S COLLEGE

(Affiliated to Tribhuwan University)

Maitighar, Kathmandu

Cryptography
Lab 2

Submitted By:

Kshitiz Adhikari (011BScCSIT019)

5th semester

Submitted To:

Er. Anil Shah

Lecturer

Department of Computer Science and Information Technology

Downloaded from CSIT Tutor


Monoalphabetic Substitution Cipher

Algorithm:

1. Set unique monoalphabetic key

2. Input plaintext or ciphertext

3. For plaintext,

Set the key for each character in text

For chipertext,

Set the cipher text for each character in text

4. End the program

Source code:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
char p[100], c[100],k[100];
int i,j,index;
int check_unique(char k[],int i)
{

for(j=0;j<i;j++)
{
if(k[j]==k[i])
{

Downloaded from CSIT Tutor


return (1);
}
else
{
return(0);
}
}
}

void upcipher()
{
int u;
cout<<"\n Plaintext: ";
cin>>p;
cout<<"\n Enter key: "<<endl;
for(i=0; i<26; i++)
{
loop:
cout<<" "<<char(i+65)<<"--->";
cin>>k[i];
u=check_unique(k,i);
if(u==1)
{
cout<<"\n Enter unique key";
goto loop;
}
}
for(i=0;i<strlen(p);i++)
{
index=p[i]-65;
c[i]=k[index];

Downloaded from CSIT Tutor


}
cout<<"\n Ciphertext: "<<c;
}

void lowcipher()
{
int u;
cout<<"\n Enter plaintext: ";
cin>>p;
cout<<"\n Enter key: "<<endl;
for(i=0; i<26; i++)
{
loop:
cout<<" "<<char(i+97)<<"--->";
cin>>k[i];
u=check_unique(k,i);
if(u==1)
{
cout<<"\n Enter unique key";
goto loop;
}
}
for(i=0;i<strlen(p);i++)
{
index=p[i]-97;
c[i]=k[index];
}
cout<<"\n Ciphertext: "<<c;
}

Downloaded from CSIT Tutor


void main()
{
int ch;
cout<<"\n 1. Uppercase letters \n 2. Lowercase letters";
cout<<"\n Enter your choice: ";
cin>>ch;
if(ch==1)
upcipher();
else if (ch==2)
lowcipher();
else
cout<<"\n Invalid choice"; getch();
}

Output:

Downloaded from CSIT Tutor


ST. XAVIER'S COLLEGE
(Affiliated to Tribhuvan University)
Maitighar, Kathmandu

Cryptography
Lab work 3

SUBMITTED BY
Kshitiz Adhikari
Roll No: 011BScCSIT019
BSc.CSIT, 5th semester

SUBMITTED TO
Er. Anil Shah
Lecturer
Department of Computer Science and Information Technology
St.Xavier’s College
Maitighar, Kathmandu

Downloaded from CSIT Tutor


Vigenere cipher
Algorithm

1. Input the plaintext and the keyword.


2. Repeat the keyword so as to make it as the length of the plaintext.
3. Encryption: Perform modular addition of the repeating keyword and the plaintext.
Ci = Pi + Ki (mod m)
Where Ci is the cipher text, Pi is the plaintext, Ki is the repeating keyword and ‘m’ is
the length of the alphabet.
4. Decryption: Perform modular subtraction of key phrase from the cipher text.
Pi = Ci - Ki (mod m)
5. Display the encrypted and decrypted message as well.
6. End the program.

Source code

#include<stdio.h>

#include<ctype.h>

#include<conio.h>

#include<string.h>

#include<process.h>

void vigenere(char *, char *);

void encipher();

void decipher();

void main()

int option;

while(1)

Downloaded from CSIT Tutor


{

printf("\n 1. Encipher!!");

printf("\n 2. Decipher!!");

printf("\n 3. Exit \n");

printf("\n Enter your option: ");

scanf("%d",&option);

fflush(stdin);

if(option==3)

exit(0);

else if(option==1)

encipher();

else if(option==2)

decipher();

else

printf("\n Invalid selection!!Try again!! ");

void encipher()

unsigned int i,j; //unsigned integer can represent only non negative integers

char plain[127],key[15];

printf("\n Enter the plaintext (maximum 128 characters): ");

Downloaded from CSIT Tutor


gets(plain);

printf("\n Enter the key (maximum 16 characters): ");

gets(key);

for(i=0,j=0; i<strlen(plain); i++,j++)

if(j>=strlen(key))

j=0;

printf("%c",65+(((toupper(plain[i])-65)+(toupper(key[j])-65))%26));

//plaintext+key%26 gives encrypted characters

void decipher()

unsigned int i,j;

char plain[127],key[15];

int value;

printf("\n Enter the ciphertext: ");

gets(plain);

printf("\n Enter the key: ");

gets(key);

for(i=0,j=0; i<strlen(plain); i++,j++)

Downloaded from CSIT Tutor


if(j>=strlen(key))

j=0;

value=(toupper(plain[i])-64)-(toupper(key[j])-64);

if(value<0)

value=value+26;

printf("%c",65+(value%26));

Output

Downloaded from CSIT Tutor


References

[1] http://en.algoritmy.net/article/45623/Vigenere-cipher

[2] http://www.math.tamu.edu/~dallen/hollywood/breaking/v.htm

[3] Wikipedia.org

Downloaded from CSIT Tutor


ST. XAVIER'S COLLEGE

(Affiliated to Tribhuvan University)


Maitighar, Kathmandu

Cryptography

SUBMITTED BY
Kshitiz Adhikari
Roll No: 011BSCIT019
BSc.CSIT, 5th semester

SUBMITTED TO
Er. Anil Shah
Lecturer
Department of Computer Science and Information Technology
St.Xavier’s College
Maitighar, Kathmandu

Downloaded from CSIT Tutor


PLAYFAIR CIPHER

Algorithm:

1. Enter the size of the key


2. Enter the key
3. Enter the size of the plaintext without spaces
4. Enter the plaintext
5. Create table using the key with i/j as a same character
6. Encrypt the plaintext using the table
 If the letters are in different rows and columns, replace the pair with the letters on
the same row respectively but at the other pair of corners of the rectangle defined
by the original pair. The order is important – the first encrypted letter of the pair is
the one that lies on the same row as the first plaintext letter.
 If the letters appear on the same row of the table, replace them with the letters to
their immediate right respectively (wrapping around to the left side of the row if a
letter in the original pair was on the right side of the row).
 If the letters appear on the same column of the table, replace them with the letters
immediately below respectively (wrapping around to the top side of the column if
a letter in the original pair was on the bottom side of the column).
7. Decrypt the cipher text
8. End the program

Source Code:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

int check(char table[5][5],char k)


{
int i,j;
for(i=0;i<5;++i)
for(j=0;j<5;++j)
{
if(table[i][j]==k)
return 0;
}
return 1;
}

Downloaded from CSIT Tutor


void main()
{
int i,j,key_len;
char table[5][5];
for(i=0;i<5;++i)
for(j=0;j<5;++j)
table[i][j]='0';

printf("**********Playfair Cipher************\n\n");

printf("Enter the length of the Key: ");


scanf("%d",&key_len);

char key[100];

printf("Enter the Key: ");


for(i=-1;i<key_len;++i)
{
scanf("%c",&key[i]);
if(key[i]=='j')
key[i]='i';
}

int flag;
int count=0;

// inserting the key into the table


for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
flag=0;
while(flag!=1)
{
if(count>key_len)
goto l1;

flag=check(table,key[count]);
++count;

Downloaded from CSIT Tutor


}// end of while
table[i][j]=key[(count-1)];
}// end of inner for
}// end of outer for

l1:printf("\n");

int val=97;
//inserting other alphabets
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(table[i][j]>=97 && table[i][j]<=123)
{}
else
{
flag=0;
while(flag!=1)
{
if('j'==(char)val)
++val;
flag=check(table,(char)val);
++val;
}// end of while
table[i][j]=(char)(val-1);
}//end of else
}// end of inner for
}// end of outer for

printf("The table is as follows:\n");


for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
printf("%c ",table[i][j]);
}
printf("\n");
}

Downloaded from CSIT Tutor


int l=0;
printf("\nEnter the length of plain text.(without spaces) ");
scanf("%d",&l);

printf("\nEnter the Plain text. ");


char p[100];
for(i=-1;i<l;++i)
{
scanf("%c",&p[i]);
}

for(i=-1;i<l;++i)
{
if(p[i]=='j')
p[i]='i';
}

printf("\nThe replaced text(j with i)");


for(i=-1;i<l;++i)
printf("%c ",p[i]);

count=0;
for(i=-1;i<l;++i)
{
if(p[i]==p[i+1])
count=count+1;
}

printf("\nThe cipher has to enter %d bogus char.It is either 'x' or 'z'\n",count);

int length=0;
if((l+count)%2!=0)
length=(l+count+1);
else
length=(l+count);

printf("\nValue of length is %d.\n",length);


char p1[100];

Downloaded from CSIT Tutor


//inserting bogus characters.
//char temp1;
int count1=0;
for(i=-1;i<l;++i)
{
p1[count1]=p[i];
if(p[i]==p[i+1])
{
count1=count1+1;
if(p[i]=='x')
p1[count1]='z';
else
p1[count1]='x';
}
count1=count1+1;
}

//checking for length

//char bogus;
if((l+count)%2!=0)
{
if(p1[length-1]=='x')
p1[length]='z';
else
p1[length]='x';
}

printf("The final text is:");


for(i=0;i<=length;++i)
printf("%c ",p1[i]);

char cipher_text[100];
int r1,r2,c1,c2;
int k1;

for(k1=1;k1<=length;++k1)
{
for(i=0;i<5;++i)

Downloaded from CSIT Tutor


{
for(j=0;j<5;++j)
{
if(table[i][j]==p1[k1])
{
r1=i;
c1=j;
}
else
if(table[i][j]==p1[k1+1])
{
r2=i;
c2=j;
}
}//end of for with j
}//end of for with i

if(r1==r2)
{
cipher_text[k1]=table[r1][(c1+1)%5];
cipher_text[k1+1]=table[r1][(c2+1)%5];
}

else
if(c1==c2)
{
cipher_text[k1]=table[(r1+1)%5][c1];
cipher_text[k1+1]=table[(r2+1)%5][c1];
}
else
{
cipher_text[k1]=table[r1][c2];
cipher_text[k1+1]=table[r2][c1];
}

k1=k1+1;
}//end of for with k1

printf("\n\nThe Cipher text is:\n ");


for(i=1;i<=length;++i)

Downloaded from CSIT Tutor


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

getch();
}

Output:

Downloaded from CSIT Tutor


ST. XAVIER'S COLLEGE

(Affiliated to Tribhuvan University)


Maitighar, Kathmandu

Cryptography

SUBMITTED BY
Kshitiz Adhikari
Roll No: 011BSCIT019
BSc.CSIT, 5th semester

SUBMITTED TO
Er. Anil Shah
Lecturer
Department of Computer Science and Information Technology
St.Xavier’s College
Maitighar, Kathmandu

Downloaded from CSIT Tutor


HILL CIPHER

Algorithm:

1. Enter the plaintext.


2. Enter the key matrix (a 3x3 matrix). The key matrix should be invertible and the elements
must not be exactly divisible by 3 and 26.
3. Divide the plain text into groups of three characters and turn them into a 3x1 matrix.
4. Multiply each of these single columned matrices with the key matrix and take the modulo
of the resultant by 26.
5. The resulting matrices consist of the encrypted text.
6. For decrypting, the encrypted text is again divided into single columned matrices and
multiplied to the inverse of the key matrix.
7. The resulting matrices hold the decrypted text.
8. Print both the encrypted and decrypted text.
9. End the program.

Source Code:

#include<stdio.h>
#include<conio.h>
#include<string.h>

char mssg[100];
int k[][3] = { {17,17,5},
{21,18,21},
{2,2,19}
};

int ki[][3] = { {4,9,15},


{15,17,6},
{24,0,17}
};

int c[3];
int p[3];

void get_mssg()
{
printf("\n Enter the Plaintext (in caps): ");
gets(mssg);

Downloaded from CSIT Tutor


}

void multiply()
{
for(int i=0;i<3;i++)
{
p[i] = 0;
for(int j=0;j<3;j++)
p[i] += c[j]*k[i][j];
p[i] %= 26;
}
}

void multiply2()
{
for(int i=0;i<3;i++)
{
c[i] = 0;
for(int j=0;j<3;j++)
c[i] += p[j]*ki[i][j];
c[i] %= 26;
}
}

void en_mssg()
{
int i = 0;
while(i < strlen(mssg))
{
c[0] = mssg[i]-65;
if(mssg[i+1] == '\0')
c[1] = c[2] = 0;
else
{
c[1] = mssg[i+1]-65;
if(mssg[i+2] == '\0')
c[2] = 0;
else
c[2] = mssg[i+2]-65;
}

Downloaded from CSIT Tutor


multiply();

int l =0;
while(i<strlen(mssg) && l<3)
mssg[i++] = p[l++]+65;

}
printf("\n The Ciphertext is : ");
puts(mssg);
}

void de_mssg()
{
int i = 0;
while(i < strlen(mssg))
{
p[0] = mssg[i]-65;
if(mssg[i+1] == '\0')
p[1] = p[2] = 0;
else
{
p[1] = mssg[i+1]-65;
if(mssg[i+2] == '\0')
p[2] = 0;
else
p[2] = mssg[i+2]-65;
}

multiply2();
int l =0;
while(i<strlen(mssg) && l<3)
mssg[i++] = c[l++]+65;
}
printf("\n The Decrypted Plaintext is : ");
puts(mssg);
}

Downloaded from CSIT Tutor


int main()
{
get_mssg();
en_mssg();
de_mssg();
getch();
return 0;
}

Output:

Downloaded from CSIT Tutor


ST. XAVIER'S COLLEGE

(Affiliated to Tribhuvan University)


Maitighar, Kathmandu

Cryptography

SUBMITTED BY
Kshitiz Adhikari
Roll No: 011BSCIT019
BSc.CSIT, 5th semester

SUBMITTED TO
Er. Anil Shah
Lecturer
Department of Computer Science and Information Technology
St.Xavier’s College
Maitighar, Kathmandu

Downloaded from CSIT Tutor


One time pad
Algorithm:

1. Enter the plaintext.


2. Generate a random key.
3. For encryption, XOR every bit of the key with the plaintext.
4. For decryption, again XOR every bit of the key with the ciphertext.
5. End the program.

Source Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>

//Construct a random key


void build_key (char *key, char *ch)
{
int i;
i = strlen(ch);
key[i]='\0';
i = 0;
while (key[i]!='\0')
{
//Random padding ch long key
key[i]=rand()%26;
i++;
}
}

void encrypt (char *key, char *ch)


{
int i = 0;
while (ch[i]!='\0')
{
ch[i] = (ch[i]^key[i]); //every XORed
i++;
}

Downloaded from CSIT Tutor


}

//Decryption and encryption of the code is identical


void decrypt (char *key, char *ch)
{
int i=0;
while(ch[i]!='\0')
{
ch[i] = (ch[i]^key[i]);
i++;
}
}

void main()
{
char ch[100] ; //store information
char key[100] ; //store the key
printf("Please input a message \n");
gets(ch);
//Get a random key
build_key(key,ch);
//Encryption
encrypt(key,ch);
printf("encrypted information: %s \n\n",ch);
//Decryption
decrypt(key,ch);
printf("decrypted information: %s \n",ch);
getch();

Output:

Downloaded from CSIT Tutor

You might also like