0% found this document useful (0 votes)
40 views31 pages

Practical File: Cryptography & Network Security Code: ETIT-455

This document contains code and output for 7 experiments on cryptography and network security topics. The experiments implement and test various classical encryption algorithms including: 1) Monoalphabetic cipher 2) Caesar cipher 3) Playfair cipher 4) Vigenere cipher 5) Rail fence cipher 6) Vernam cipher 7) An additional program is mentioned but not included.

Uploaded by

SHIV PANDEY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views31 pages

Practical File: Cryptography & Network Security Code: ETIT-455

This document contains code and output for 7 experiments on cryptography and network security topics. The experiments implement and test various classical encryption algorithms including: 1) Monoalphabetic cipher 2) Caesar cipher 3) Playfair cipher 4) Vigenere cipher 5) Rail fence cipher 6) Vernam cipher 7) An additional program is mentioned but not included.

Uploaded by

SHIV PANDEY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CRYPTOGRAPHY & NETWORK SECURITY

Code: ETIT-455

Practical File

Name: Karmenpreet Kaur


Class: IT-1
Enrollment No.: 04613203117
Experiment No. 1
1. Write a program for encryption and decryption for Monoalphabetic cipher.
Code:
#include <iostream>
#include<vector>
using namespace std;
int main()
{
string s;
cin>>s;
cout<<"\nPlain Text :\n"<<s<<endl;
string enc="",dec="";
int k,ki;
cin>>k;
cout<<"Key : "<<k<<endl<<endl;
vector<int> vect;
for(int i=0;i<s.length();i++)
{
int val=s[i]-'a';
vect.push_back(val);
}
for(int i=0;i<vect.size();i++)
{
vect[i]=(vect[i]+k)%26;
}
for(int i=0;i<vect.size();i++)
{
char c=vect[i]+'A';
enc=enc+c;
}
cout<<"Encrypted/Cipher Text: \n"<<enc;
cout<<"\n \nEncrypted using Monoalphabetic Cipher ";
cout<<"\n\nNow Decrypting :\n"<<enc<<endl;
for(int i=0;i<vect.size();i++)
{
if(vect[i]-k<0)
{
vect[i]=26+(vect[i]-k);
}
else
vect[i]=(vect[i]-k)%26;
}
for(int i=0;i<vect.size();i++)
{
char c=vect[i]+'a';
dec=dec+c;

}
cout<<"Plain Text comes out to be: \n"<<dec<<endl;

return 0;
}

Output:
Experiment No. 2
2. Write a program for Ceaser cipher.
Code:
#include <iostream>
#include<vector>
using namespace std;
int main()
{
string s;
cin>>s;
cout<<"\nPlain Text :\n"<<s<<endl;
string enc="",dec="";
int k,ki;
cin>>k;
cout<<"Key : "<<k<<endl<<endl;
vector<int> vect;
for(int i=0;i<s.length();i++)
{
int val=s[i]-'a';
vect.push_back(val);
}
for(int i=0;i<vect.size();i++)
{
vect[i]=(vect[i]+k)%26;
}
for(int i=0;i<vect.size();i++)
{
char c=vect[i]+'A';
enc=enc+c;
}
cout<<"Encrypted/Cipher Text: \n"<<enc;
cout<<"\n \nEncrypted using Caesar Cipher ";
cout<<"\n\nNow Decrypting :\n"<<enc<<endl;

for(int i=0;i<vect.size();i++)
{
if(vect[i]-k<0)
{
vect[i]=26+(vect[i]-k);
}
else
vect[i]=(vect[i]-k)%26;
}
for(int i=0;i<vect.size();i++)
{

char c=vect[i]+'a';
dec=dec+c;
}
cout<<"Plain Text comes out to be: \n"<<dec<<endl;
return 0;
}

Output:
Experiment No. 3
3. Write a program for Playfair matrix algorithm.
Code:
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main(){
int i,j,k,n;
string s,origin;
getline(cin,origin);
cout<<"\nPlain Text :\n"<<origin<<endl;
string key;
cin>>key;
cout<<"\nKey : "<<key<<endl<<endl;
for(i=0;i<origin.size();i++){
if(origin[i]!=' ')
s+= origin[i];
}
vector<vector<char> > a(5,vector<char>(5,' '));
n=5;
map<char,int> mp;
k=0;
int pi,pj;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
while(mp[key[k]]>0&&k<key.size()){
k++;
}
if(k<key.size()){
a[i][j]=key[k];
mp[key[k]]++;
pi=i;
pj=j;
}
if(k==key.size())
break;
}
if(k==key.size())
break;
}
k=0;
for(;i<n;i++){
for(;j<n;j++){
while(mp[char(k+'a')]>0&&k<26){
k++;
}
if(char(k+'a')=='j'){
j--;
k++;
continue;
}
if(k<26){
a[i][j]=char(k+'a');
mp[char(k+'a')]++;
}
}
j=0;
}

string ans;
if(s.size()%2==1)
s+="x";
for(i=0;i<s.size()-1;i++){
if(s[i]==s[i+1])
s[i+1]='x';
}

map<char,pair<int,int> > mp2;

for(i=0;i<n;i++){
for(j=0;j<n;j++){
mp2[a[i][j]] = make_pair(i,j);
}
}

for(i=0;i<s.size()-1;i+=2){
int y1 = mp2[s[i]].first;
int x1 = mp2[s[i]].second;
int y2 = mp2[s[i+1]].first;
int x2 = mp2[s[i+1]].second;
if(y1==y2){
ans+=a[y1][(x1+1)%5];
ans+=a[y1][(x2+1)%5];
}
else if(x1==x2){
ans+=a[(y1+1)%5][x1];
ans+=a[(y2+1)%5][x2];
}
else {
ans+=a[y1][x2];
ans+=a[y2][x1];
}
}
cout<<"Encrypted/Cipher Text: \n"<<ans;
cout<<"\n \nEncrypted using Playfair Matrix Algorithm";

return 0;
}

Output:
Experiment No. 4
4. Write a program for Vigenere Cipher (Polyalphabetic substitution).
Code:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
string s;
cin>>s;
cout<<"\nPlain Text :\n"<<s<<endl;
string enc="",dec="";
string key;
cin>>key;
cout<<"Key : "<<key<<endl<<endl;
vector<int> vect;
for(int i=0;i<s.length();i++)
{
int val=s[i]-'a';
vect.push_back(val);
}
vector<int> vect2;
for(int i=0;i<key.length();i++)
{
int val=key[i]-'a';
vect2.push_back(val);
}
int j=0;
for(int i=0;i<vect.size();i++)
{
if(j==vect2.size())
{
j=0;
}
vect[i]=(vect[i]+vect2[j++])%26;
}
for(int i=0;i<vect.size();i++)
{
char c=vect[i]+'A';
enc=enc+c;

}
cout<<"Encrypted/Cipher Text: \n"<<enc;
cout<<"\n \nEncrypted using Vigenere Cipher ";
cout<<"\n\nNow Decrypting :\n"<<enc<<endl;
j=0;
for(int i=0;i<vect.size();i++)
{
if(j==vect2.size())
{
j=0;
}
vect[i]=(vect[i]-vect2[j++])%26;
if(vect[i]<0)
{
vect[i]=26+vect[i];
}
}
for(int i=0;i<vect.size();i++)
{
char c=vect[i]+'a';
dec=dec+c;

}
cout<<"\nPlain Text comes out to be: \n"<<dec<<endl;

return 0;
}

Output:
Experiment No. 5
5. Write a program for Rail fence Cipher.
Code:
#include <iostream>
using namespace std;

string encrypt(string text, int key)


{
char rail[key][(text.length())];
for (int i=0; i < key; i++)
for (int j = 0; j < text.length(); j++)
rail[i][j] = '\n';

bool dir_down = false;


int row = 0, col = 0;
for (int i=0; i < text.length(); i++)
{
if (row == 0 || row == key-1)
dir_down = !dir_down;
rail[row][col++] = text[i];
dir_down?row++ : row--;
}
string result;
for (int i=0; i < key; i++)
for (int j=0; j < text.length(); j++)
if (rail[i][j]!='\n')
{
rail[i][j]=rail[i][j]-'a'+'A';
result.push_back(rail[i][j]);
}
return result;
}
string decrypt(string cipher, int key)
{
char rail[key][cipher.length()];
for (int i=0; i < key; i++)
for (int j=0; j < cipher.length(); j++)
rail[i][j] = '\n';
bool dir_down;
int row = 0, col = 0;
for (int i=0; i < cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
rail[row][col++] = '*';
dir_down?row++ : row--;
}
int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '*' && index<cipher.length())
rail[i][j] = cipher[index++];
string result;
row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
if (rail[row][col] != '*')
{
rail[row][col]=rail[row][col]-'A'+'a';
result.push_back(rail[row][col++]);
}
dir_down?row++: row--;
}
return result;
}

int main()
{
string s;
cin>>s;
cout<<"\nPlain Text :\n"<<s<<endl;
int k;
cin>>k;
cout<<"Key : "<<k<<endl<<endl;
string enc=encrypt(s, k);
cout<<"Encrypted/Cipher Text: \n"<<enc;
cout<<"\n \nEncrypted using Rail Fence Cipher ";
cout<<"\n\nNow Decrypting :\n"<<enc<<endl;
string dec=decrypt(enc, k);
cout<<"Plain Text comes out to be: \n"<<dec<<endl;

return 0;
}
Output:
Experiment No. 6
6. Write a program to implement Vernam Cipher.
Code:
#include<iostream>
#include<vector>

using namespace std;

int main()
{
cout<<"\nVernam Cipher is a Special Case of Vignere Cipher where the length of plain text is equal to
the length of key."<<endl;
string s;
cin>>s;
cout<<"\nPlain Text :\n"<<s<<endl;
string enc="",dec="";
string key;
cin>>key;

cout<<"Key : "<<key<<endl<<endl;
vector<int> vect;
for(int i=0;i<s.length();i++)
{
int val=s[i]-'a';
vect.push_back(val);
}
vector<int> vect2;
for(int i=0;i<key.length();i++)
{
int val=key[i]-'a';
vect2.push_back(val);
}

for(int i=0;i<vect.size();i++)
{

vect[i]=(vect[i]+vect2[i])%26;
}
for(int i=0;i<vect.size();i++)
{
char c=vect[i]+'A';
enc=enc+c;

}
cout<<"Encrypted/Cipher Text: \n"<<enc;
cout<<"\n \nEncrypted using Vernam Cipher ";
cout<<"\n\nNow Decrypting :\n"<<enc<<endl;

for(int i=0;i<vect.size();i++)
{

vect[i]=(vect[i]-vect2[i])%26;
if(vect[i]<0)
{
vect[i]=26+vect[i];
}
}
for(int i=0;i<vect.size();i++)
{
char c=vect[i]+'a';
dec=dec+c;

}
cout<<"\nPlain Text comes out to be: \n"<<dec<<endl;

return 0;
}

Output:
Experiment No. 7
7. Write a program to implement S-DES algorithm for data encryption.
Code:
#include <iostream>
#include <string>
#include <ctime>
#include <stdio.h>
using namespace std;

string SDESEncryption(string, string, int);


string SDESDecryption(string, string, int);
string findKey(string, int);
string functionF(string, string);
string XOR(string, string);
string S1Box(string);
string S2Box(string);
string randomString(int);
int main()
{
string plaintext = randomString(12);
cout<< "\nGenerating plaintext and key pair."<<endl;
string key = randomString(9);
string ciphertext;
string decryption;
int numrounds = 4;
int rounds;
cout<<"Plaintext: "<< plaintext<<endl<<"Key: "<<key<<endl;
cout << "\nEncrypting using S-DES Algorithm:\n";
for(int i = 0; i < numrounds; i++)
{
plaintext = SDESEncryption(key, plaintext, i+1);
cout << "\tC (after round " << i + 1 << "): " << plaintext << endl;
rounds = i + 1;
}
ciphertext.append(plaintext, 6, 6);
ciphertext.append(plaintext, 0, 6);
cout<<"\nCiphertext after "<<rounds<<" rounds: " << ciphertext << endl << endl;

cout<< "Now decrypting Ciphertext: " << ciphertext << endl << endl;
decryption.append(ciphertext, 6, 6);
decryption.append(ciphertext, 0, 6);
for(int j = numrounds; j > 0; j--)
{
decryption = SDESDecryption(key, decryption, j);
if(j != 1)
cout << "C (after round " << numrounds-j+1 << "): "<< decryption << endl;
else if(j == 1)
cout << "\nPlaintext comes out to be: "<< decryption << endl << endl;
}
return 0;
}
string SDESEncryption(string key, string plaintext, int round)
{
string Li,Ri,Ln,Rn,K,f;
K = findKey(key, round);
Li.append(plaintext, 0, 6);
Ri.append(plaintext, 6, 6);
Ln = Ri;
f.append(functionF(Ri, K));
Rn.append(f);
Rn = XOR(Li, f);
return (Ln + Rn);
}
string SDESDecryption(string key, string ciphertext, int round)
{
string Li,Ri,Ln,Rn,K,f;
K = findKey(key, round);
Li.append(ciphertext, 0, 6);
Ri.append(ciphertext, 6, 6);
Rn = Li;
f.append(functionF(Rn, K));
Ln.append(f);
Ln = XOR(Ri, f);
return (Ln + Rn);
}
string findKey(string key, int round)
{
string K;
if(round == 1)
K.append(key, 0, 8);
else if(round == 2)
K.append(key, 1, 8);
else if(round == 3)
{
K.append(key, 2, 7);
K.append(key, 0, 1);
}
else if(round == 4)
{
K.append(key, 3, 6);
K.append(key, 0, 2);
}
return K;
}
string functionF(string R, string K)
{
char tmp;
string s1;
string s2;
R.append(R, 4, 2);
tmp = R[3];
R[5] = R[2];
R[4] = tmp;
R[3] = R[2];
R[2] = tmp;
R = XOR(R, K);
s1.append(R, 0, 4);
s2.append(R, 4, 4);
return S1Box(s1) + S2Box(s2);
}
string XOR(string x, string y)
{
for(int i = 0; i < x.length(); i++)
{
if(x[i] == y[i])
x[i] = '0';
else if(x[i] != y[i])
x[i] = '1';
}
return x;
}
string S1Box(string s1)
{
string row1[8] = {"101", "010", "001", "110", "011", "100", "111", "000"};
string row2[8] = {"001", "100", "110", "010", "000", "111", "101", "011"};
int column = 0;
if(s1[0] == '0')
{
if(s1[1] == '1')
column += 4;
if(s1[2] == '1')
column += 2;
if(s1[3] == '1')
column += 1;
return row1[column];
}
else if(s1[0] == '1')
{
if(s1[1] == '1')
column += 4;
if(s1[2] == '1')
column += 2;
if(s1[3] == '1')
column += 1;
return row2[column];
}
else
return "ERROR";
}

string S2Box(string s2)


{
string row1[8] = {"100", "000", "110", "101", "111", "001", "011", "010"};
string row2[8] = {"101", "011", "000", "111", "110", "010", "001", "100"};
int column = 0;
if(s2[0] == '0')
{
if(s2[1] == '1')
column += 4;
if(s2[2] == '1')
column += 2;
if(s2[3] == '1')
column += 1;
return row1[column];
}
else if(s2[0] == '1')
{
if(s2[1] == '1')
column += 4;
if(s2[2] == '1')
column += 2;
if(s2[3] == '1')
column += 1;
return row2[column];
}
else
return "ERROR";
}

string randomString(int length)


{ srand(time(0));
int randomNumber;
string randomBit;
string randomString;
for(int k = 0; k < length; k++)
{
randomNumber = rand() % 2;
if(randomNumber == 0)
randomBit = "0";
else if(randomNumber == 1)
randomBit = "1";
randomString+=randomBit;
}
return randomString;
}

Output:
Experiment No. 8
8. Write a program to implement RSA algorithm.
Code:
#include<iostream>
#include<vector>
#include<math.h>

using namespace std;

int gcd(int a, int h)


{
int temp;
while(1)
{
temp = a%h;
if(temp==0)
return h;
a = h;
h = temp;
}
}
int main()
{
double p = 3,q = 7,count,e=2,d,k=2,msg=12;
double n=p*q;
double totient = (p-1)*(q-1);

while(e<totient){
count = gcd(e,totient);
if(count==1)
break;
else
e++;
}

cout<<"\nMessage: "<<msg;
d = (1 + (k*totient))/e;
double c = pow(msg,e);
c=fmod(c,n);

cout<<"\n"<<"Encrypted data : "<<c;


cout<<"\n \nEncrypted using RSA Algorithm ";
cout<<"\n\nNow Decrypting : "<<c<<endl;
double m = pow(c,d);
m=fmod(m,n);

cout<<"\n"<<"Original Message comes out to be: "<<m;

return 0;
}

Output:
Experiment No. 9
9. Write a program to implement Diffie Hellman algorithm.
Code:
#include<iostream>
#include<math.h>

using namespace std;

long long int power(long long int a, long long int b,long long int P)
{
if (b == 1)
return a;
else
return (((long long int)pow(a, b)) % P);
}

int main()
{
long long int P, G, x, a, y, b, ka, kb;

P = 23;
cout<<"\nThe value of P is : "<<P<<endl;

G = 9;
cout<<"The value of G is : "<<G<<endl;
a = 4;
cout<<"\nThe private key for 1st person : "<<a<<endl;
x = power(G, a, P);
b = 3;
cout<<"The private key for 2nd person : "<<b<<endl;
y = power(G, b, P);

ka = power(y, a, P);
kb = power(x, b, P);
cout<<"\nSecret key for 1st person comes out to be :"<<ka<<endl;
cout<<"Secret key for 2nd person comes out to be :"<<kb<<endl;
cout<<"\nKey shared using Diffie Hellman algorithm";
return 0;
}
Output:
Experiment No. 10
10. Write a program to implement Hill Cipher algorithm.
Code:
#include<iostream>
#include<math.h>
using namespace std;

float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];


void getKeyMessage() {
int i, j;
char msg[3];
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
c[i][j] = a[i][j];
}
cin>>msg;

cout<<"\nPlain Text :\n"<<msg<<endl<<endl;

cout<<"Key Matrix: ";


for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cout<<a[i][j]<<" ";
if(j==2)
cout<<endl;
}

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


mes[i][0] = msg[i] - 97;
}
void encryption() {
int i, j, k;

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


for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];

cout<<"\nEncrypted/Cipher Text: ";


for(i = 0; i < 3; i++)
cout<<(char)(fmod(encrypt[i][0], 26) + 'A');
}
void inverse() {
int i, j, k;
float p, q;

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


for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}

for(k = 0; k < 3; k++) {


for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];

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


if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];

cout<<"\n\nInverse Matrix is:\n";


for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout<<b[i][j]<<" ";

cout<<"\n";
}
}
void decryption() {
int i, j, k;

inverse();

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


for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];

cout<<"\nPlain Text comes out to be: \n";


for(i = 0; i < 3; i++)
cout<<(char)(fmod(decrypt[i][0], 26) + 'a');

cout<<"\n";
}

int main() {
getKeyMessage();
encryption();
cout<<"\n \nEncrypted using Hill Cipher ";
cout<<"\n\nNow Decrypting :";
decryption();
}

Output:
Experiment No. 11
11. Write a program to implement the hash code using MD5.
Code:
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {


public static String getMd5(String input)
{
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static void main(String args[]) throws NoSuchAlgorithmException


{
String s = "Karanpreet";
System.out.println("Plain Text: "+s+"\n");
System.out.println("HashCode Generated by MD5 is: " + getMd5(s));
}
}
Output:
Experiment No. 12
12. Write a program to encrypt and decrypt with Knapsack Problem.
Code:
#include<iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int gcd( int m, int n)


{
while( m!= n) // execute loop until m == n
{
if( m > n)
m = m - n; // large - small , store the results in large variable
else
n = n - m;
}

return (m);
}

void sort(int arr[])


{
int i, j, temp ;
for ( i = 0 ; i <= 7 ; i++ )
{
for ( j = i + 1 ; j <= 8 ; j++ )
{
if ( arr[i] > arr[j] )
{
temp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = temp ;
}
}
}
}

int sum(int a[])


{
int i, sum=0;
for (i=0; i<8; i++)
{
sum = sum + a[i];
}
return(sum);
}

int main()
{
int w[8],i,j,qt,m,n;
int b[8],a[8];
int c=0;
for(i=0;i<9;i++)
srand(time(NULL));

for(i=0;i<8;i++)
w[i]=rand()%100;

qt=sum(w);
m=rand()%1000;
n=rand()%1000;
while(m<qt && gcd(m,n)!=1 && m<n)
{
m=rand()%1000;
n=rand()%1000;
}

sort(w);
cout<<"\nThe private key is: ";
for(j=0;j<8;j++)
cout<<"\t"<<w[j];
cout<<"\nThe value of m chosen is: "<<m<<endl;
cout<<"\nThe value of n chosen is: "<<n<<endl;
for(i=0;i<8;i++)
{
b[i]=(n*w[i])%m;
}
cout<<"\nThe public key comes out to be is: ";
for(j=0;j<8;j++)
cout<<"\t"<<b[j];
for(i=0;i<8;i++)
cin>>a[i];
cout<<"\n\n8-bit string entered is: ";
for(j=0;j<8;j++)
cout<<" "<<a[j];

cout<<"\nThe Encrypted Ciphertext comes out to be: ";


for(i=0;i<8;i++)
c+=(a[i]*b[i]);
cout<<c;

return 0;
}

Output:

You might also like