Professional Documents
Culture Documents
Code: ETIT-455
Practical File
}
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';
}
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;
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>
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;
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";
}
Output:
Experiment No. 8
8. Write a program to implement RSA algorithm.
Code:
#include<iostream>
#include<vector>
#include<math.h>
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);
return 0;
}
Output:
Experiment No. 9
9. Write a program to implement Diffie Hellman algorithm.
Code:
#include<iostream>
#include<math.h>
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;
cout<<"\n";
}
}
void decryption() {
int i, j, k;
inverse();
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;
return (m);
}
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];
return 0;
}
Output: