-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHillCypher.cpp
More file actions
80 lines (78 loc) · 2.61 KB
/
HillCypher.cpp
File metadata and controls
80 lines (78 loc) · 2.61 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
73
74
75
76
77
78
79
80
#include<iostream>
using namespace std;
#include<vector>
class HillCipher{
vector<vector<float>> Matrix;
string ConvertMatrixToString(vector<float> Matrix){
string s = "";
for(auto it: Matrix){
s.push_back(char(it+float('a')));
}
return s;
}
vector<float> ConvertStringToMatrix(string s){
vector<float> Matrix;
for(auto it: s){
Matrix.push_back(it-'a');
}
return Matrix;
}
vector<float> MultiplyMatrix(vector<float> S, vector<vector<float>> M){
vector<float> Result(S.size(),0);
for(float i=0;i<S.size();i++){
for(float j=0;j<M.size();j++){
Result[i]+=M[j][i]*S[j];
}
Result[i] = int(Result[i])%26;
}
return Result;
}
void GenerateMatrix(float n){
Matrix = {{1,6,4,8}, {2,1,21,19},{7,11,13,17},{3,5,23,29}};
}
void getInverse(vector<vector<float>>& Matrix){
float det = (Matrix[1][1]*Matrix[0][0]) - (Matrix[0][1]*Matrix[1][0]);
float temp;
temp = Matrix[0][0];
Matrix[0][0] = Matrix[1][1]/det;
Matrix[1][1] = temp/det;
Matrix[0][1] =(Matrix[0][1]*(-1))/det;
Matrix[1][0] *=(Matrix[1][0]*(-1))/det;
}
vector<vector<float>> findKey(vector<vector<float>> message, vector<vector<float>> cipher){
getInverse(message);
vector<vector<float>> mult(2, vector<float>(2,0));
for(float i = 0; i < 2; ++i){
for(float j = 0; j < 2; ++j){
for(float k = 0; k < 2; ++k){
mult[i][j] += (int(message[i][k] * cipher[k][j])%26+26)%26;
}
}
}
return mult;
}
public:
string Encrypt(string message){
float n = message.size();
GenerateMatrix(n);
return ConvertMatrixToString(MultiplyMatrix(ConvertStringToMatrix(message), Matrix));
}
void Analyse(){
vector<vector<float>> message = {{0,1},{19,3}};
vector<vector<float>> cipher = {{2, 1}, {0,0}};
vector<vector<float>> key = findKey(message, cipher);
for(auto it: key){
for(auto b: it) cout<<b<<" ";
cout<<endl;
}
}
};
int main(){
string s;
cin>>s;
HillCipher HC;
string c = HC.Encrypt(s);
cout<<"Encrypted String: "<<c<<endl;
// HC.Analyse();
return 0;
}