-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolybiusSquareCipher.cpp
More file actions
72 lines (67 loc) · 1.4 KB
/
PolybiusSquareCipher.cpp
File metadata and controls
72 lines (67 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<string.h>
using namespace std;
char table[5][5];
string encrypt(string pt)
{
string encpt="";
int l = pt.length();
char encpt1[l+1];
strcpy(encpt1,pt.c_str());
for(int i=0;i<l;i++)
for(int j=0;j<5;j++)
for(int k=0;k<5;k++)
{
if(encpt1[i]==table[j][k])
{
char ch = j+48+1;
string row(1,ch);
encpt=encpt+row;
ch = k+48+1;
string col(1,ch);
encpt=encpt+col;
}
}
return encpt;
}
string decrypt(string encpt)
{
string decpt="";
int l = encpt.length();
char decpt1[l+1];
strcpy(decpt1,encpt.c_str());
for(int i=0;i<l;i=i+2)
{
int row, col;
row=decpt1[i];
row-=48;
col=decpt1[i+1];
col-=48;
string s(1,table[row-1][col-1]);
decpt=decpt+s;
}
return decpt;
}
int main()
{
int num = 65, i=0;
while(num<=90)
{
if(num!=74)
{
int j = i/5;
char ch = num;
table[j][i%5] = ch;
i++;
}
num++;
}
string pt,encpt,decpt;
cout<<"Enter plaintext: ";
cin>>pt;
encpt = encrypt(pt);
cout<<"Encrypted text: "<<encpt;
decpt = decrypt(encpt);
cout<<"\nDecrypted text: "<<decpt;
return 0;
}