Beruflich Dokumente
Kultur Dokumente
SECURITY AAT
AYUSH JETHWANI
(1BM16CS020)
Contents
SL. Topics
No.
1. Multiplicative Cipher
2. Vingenere cipher
3. Affine Cipher
4. Hill Cipher
5. Playfair Cipher
1
1. Multiplicatve Cipher –
#include <iostream>
#include <string>
using namespace std;
if (flag == 1)
2
{
a_inv = i;
}
}
for (int i = 0; i < cipher.length(); i++)
{
if(cipher[i]!=' ')
msg = msg +
(char) (((a_inv * ((cipher[i]+'A')) % 26)) +
'A');
else
msg += cipher[i];
}
return msg;
}
int main(void)
{
string msg = "AYUSH";
return 0;
}
2. Vigenere Cipher –
#include <iostream>
#include <string>
using namespace std;
3
string generateKey(string str, string key)
{
int x = str.size();
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
4
for (int i = 0 ; i < cipher_text.size(); i++)
{
int x = (cipher_text[i] - key[i] + 26) %26;
x += 'A';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
string str = "AYUSHJETHWANI";
string keyword = "CRICKET";
3. Affine Cipher –
#include <iostream>
#include <string>
using namespace std;
5
string encryptMessage(string msg)
{
string cipher = "";
for (int i = 0; i < msg.length(); i++)
{
if(msg[i]!=' ')
cipher = cipher +
(char) ((((a * (msg[i]-'A') ) + b) % 26) +
'A');
else
cipher += msg[i];
}
return cipher;
}
if (flag == 1)
{
a_inv = i;
}
}
for (int i = 0; i < cipher.length(); i++)
{
if(cipher[i]!=' ')
msg = msg +
6
(char) (((a_inv * ((cipher[i]+'A' - b)) % 26)) +
'A');
else
msg += cipher[i];
}
return msg;
}
int main(void)
{
string msg = "AYUSH";
return 0;
}
4. Hill Cipher –
#include <iostream>
#include <string>
using namespace std;
7
k++;
}
}
}
int messageVector[3][1];
8
messageVector[i][0] = (message[i]) % 65;
int cipherMatrix[3][1];
string CipherText;
int main()
{
string message = "AYUSH";
HillCipher(message, key);
return 0;
}
5. Playfair Cipher –
#include<iostream>
#include<string>
using namespace std;
char
key[][5]={{'L','G','D','B','A'},{'Q','M','H','E','C'},{'U','R','N','I','F'},{'X','V','S','O','K'},{'Z','
Y','W','T','P'}};
9
string text;
string temp="";
}
void encrypt()
{
char bogus='x';
for (int i = 1; i < text.length(); ++i)
{
if(text[i]==text[i-1])
10
{
temp.append(text.substr(0,i));
temp.push_back('x');
temp.append(text.substr(i,text.length()));
text=temp;
temp.clear();
}
}
if(text.length()%2 != 0) text.append("x");
int f_x,s_x,f_y,s_y,type;
for (int i = 0; i < text.length(); i+=2)
{
text[i]-=32;
text[i+1]-=32;
find(text[i]=='J'? 'I':text[i],&f_x,&f_y);
find(text[i+1]=='J'? 'I':text[i+1],&s_x,&s_y);
switch(type)
{
case 1: temp.push_back(key[f_x][(f_y+1)%5]);
11
temp.push_back(key[s_x][(s_y+1)%5]);
break;
case 2: temp.push_back(key[(f_x+1)%5][f_y]);
temp.push_back(key[(s_x+1)%5][s_y]);
break;
case 3: temp.push_back(key[f_x][s_y]);
temp.push_back(key[s_x][f_y]);
break;
}
}
cout<<endl<<"The encrypted text: "<<temp<<endl;
}
void decrypt()
{
int f_x,s_x,f_y,s_y,type;
for (int i = 0; i < text.length(); i+=2)
{
find(text[i],&f_x,&f_y);
find(text[i+1],&s_x,&s_y);
if(s_x==f_x) type =1;
else if(s_y==f_y) type =2;
else type=3;
12
switch(type)
{
case 1: temp.push_back(key[f_x][(f_y-1)%5]);
temp.push_back(key[s_x][(s_y-1)%5]);
break;
case 2: temp.push_back(key[(f_x-1)%5][f_y]);
temp.push_back(key[(s_x-1)%5][s_y]);
break;
case 3: temp.push_back(key[f_x][s_y]);
temp.push_back(key[s_x][f_y]);
break;
}
}
cout<<endl<<"The decrypted text: "<<temp<<endl;
int main()
{
cout<<"\nEnter the text:";
getline(cin,text);
int opt;
cout<<"Enter option :\n1.Encrypt using playfair cipher\n2.Decrypt playfair
cipher text\nOption:";
cin>>opt;
13
cout<<"\nNote: I and J are considered same when encrypting\n";
switch(opt)
{
case 1:encrypt();break;
case 2:decrypt();break;
}
return 0;
}
14