Вы находитесь на странице: 1из 14

CRYPTOGRAPHY AND NETWORK

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;

const int a = 17;

string encryptMessage(string msg)


{
string cipher = "";
for (int i = 0; i < msg.length(); i++)
{
if(msg[i]!=' ')
cipher = cipher +
(char) ((((a * (msg[i]-'A') ) ) % 26)
+'A');
else
cipher += msg[i];
}
return cipher;
}

string decryptCipher(string cipher)


{
string msg = "";
int a_inv = 0;
int flag = 0;

for (int i = 0; i < 26; i++)


{
flag = (a * i) % 26;

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";

string cipherText = encryptMessage(msg);


cout << "Encrypted Message is : " << cipherText<<endl;

cout << "Decrypted Message is: " << decryptCipher(cipherText);

return 0;
}

2. Vigenere Cipher –

#include <iostream>
#include <string>
using namespace std;

3
string generateKey(string str, string key)
{
int x = str.size();

for (int i = 0; ; i++)


{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}

string cipherText(string str, string key)


{
string cipher_text;

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


{
int x = (str[i] + key[i]) %26;

x += 'A';

cipher_text.push_back(x);
}
return cipher_text;
}

string originalText(string cipher_text, string key)


{
string orig_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";

string key = generateKey(str, keyword);


string cipher_text = cipherText(str, key);

cout << "Ciphertext : "


<< cipher_text << "\n";

cout << "Original/Decrypted Text : "


<< originalText(cipher_text, key);
return 0;
}

3. Affine Cipher –

#include <iostream>
#include <string>
using namespace std;

const int a = 17;


const int b = 20;

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;
}

string decryptCipher(string cipher)


{
string msg = "";
int a_inv = 0;
int flag = 0;

for (int i = 0; i < 26; i++)


{
flag = (a * i) % 26;

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";

string cipherText = encryptMessage(msg);


cout << "Encrypted Message is : " << cipherText<<endl;

cout << "Decrypted Message is: " << decryptCipher(cipherText);

return 0;
}

4. Hill Cipher –

#include <iostream>
#include <string>
using namespace std;

void getKeyMatrix(string key, int keyMatrix[][3])


{
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
keyMatrix[i][j] = (key[k]) % 65;

7
k++;
}
}
}

void encrypt(int cipherMatrix[][1],


int keyMatrix[][3],
int messageVector[][1])
{
int x, i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 1; j++)
{
cipherMatrix[i][j] = 0;

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


{
cipherMatrix[i][j] +=
keyMatrix[i][x] * messageVector[x][j];
}

cipherMatrix[i][j] = cipherMatrix[i][j] % 26;


}
}
}

void HillCipher(string message, string key)


{
int keyMatrix[3][3];
getKeyMatrix(key, keyMatrix);

int messageVector[3][1];

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

8
messageVector[i][0] = (message[i]) % 65;

int cipherMatrix[3][1];

encrypt(cipherMatrix, keyMatrix, messageVector);

string CipherText;

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


CipherText += cipherMatrix[i][0] + 65;

cout << " Ciphertext:" << CipherText;


}

int main()
{
string message = "AYUSH";

string key = "GYBNQKURP";

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 find(char s,int *x, int *y)


{
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
if(s==key[i][j])
{
*x=i;
*y=j;
}
}
}

}
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);

if(s_x==f_x) type =1;


else if(s_y==f_y) type =2;
else type=3;

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

Вам также может понравиться