Professional Documents
Culture Documents
XAVIER’S COLLEGE
Maitighar, Kathmandu
Cryptography
Lab 1
Submitted By:
5th semester
Submitted To:
Lecturer
Algorithm:
1. Input plaintext
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:";
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();
}
Maitighar, Kathmandu
Cryptography
Lab 2
Submitted By:
5th semester
Submitted To:
Lecturer
Algorithm:
3. For plaintext,
For chipertext,
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])
{
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];
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;
}
Output:
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
Source code
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void encipher();
void decipher();
void main()
int option;
while(1)
printf("\n 1. Encipher!!");
printf("\n 2. Decipher!!");
scanf("%d",&option);
fflush(stdin);
if(option==3)
exit(0);
else if(option==1)
encipher();
else if(option==2)
decipher();
else
void encipher()
unsigned int i,j; //unsigned integer can represent only non negative integers
char plain[127],key[15];
gets(key);
if(j>=strlen(key))
j=0;
printf("%c",65+(((toupper(plain[i])-65)+(toupper(key[j])-65))%26));
void decipher()
char plain[127],key[15];
int value;
gets(plain);
gets(key);
j=0;
value=(toupper(plain[i])-64)-(toupper(key[j])-64);
if(value<0)
value=value+26;
printf("%c",65+(value%26));
Output
[1] http://en.algoritmy.net/article/45623/Vigenere-cipher
[2] http://www.math.tamu.edu/~dallen/hollywood/breaking/v.htm
[3] Wikipedia.org
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
Algorithm:
Source Code:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
printf("**********Playfair Cipher************\n\n");
char key[100];
int flag;
int count=0;
flag=check(table,key[count]);
++count;
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
for(i=-1;i<l;++i)
{
if(p[i]=='j')
p[i]='i';
}
count=0;
for(i=-1;i<l;++i)
{
if(p[i]==p[i+1])
count=count+1;
}
int length=0;
if((l+count)%2!=0)
length=(l+count+1);
else
length=(l+count);
//char bogus;
if((l+count)%2!=0)
{
if(p1[length-1]=='x')
p1[length]='z';
else
p1[length]='x';
}
char cipher_text[100];
int r1,r2,c1,c2;
int k1;
for(k1=1;k1<=length;++k1)
{
for(i=0;i<5;++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
getch();
}
Output:
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
Algorithm:
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 c[3];
int p[3];
void get_mssg()
{
printf("\n Enter the Plaintext (in caps): ");
gets(mssg);
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;
}
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);
}
Output:
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
Source Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
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: