forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2.cpp
More file actions
90 lines (70 loc) · 2.01 KB
/
Copy pathlab2.cpp
File metadata and controls
90 lines (70 loc) · 2.01 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
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int modInverse(int n)
{
n %= 26;
for(int i = 0; i < 26; i++)
{
if((n * i) % 26 == 1)
return i;
}
return -1;
}
string encrypt(string text, int key[2][2])
{
string cipher = "";
for(int i = 0; i < text.length(); i += 2)
{
int p1 = text[i] - 'A';
int p2 = text[i+1] - 'A';
cipher += char((key[0][0]*p1 + key[0][1]*p2) % 26 + 'A');
cipher += char((key[1][0]*p1 + key[1][1]*p2) % 26 + 'A');
}
return cipher;
}
string decrypt(string cipher, int key[2][2])
{
int det = (key[0][0]*key[1][1] - key[0][1]*key[1][0]) % 26;
if(det < 0) det += 26;
int invdet = modInverse(det);
if(invdet == -1) return "Key is not invertible";
int invKey[2][2];
invKey[0][0] = ( key[1][1] * invdet ) % 26;
invKey[0][1] = (-key[0][1] * invdet ) % 26;
invKey[1][0] = (-key[1][0] * invdet ) % 26;
invKey[1][1] = ( key[0][0] * invdet ) % 26;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(invKey[i][j] < 0)
invKey[i][j] += 26;
string plain = "";
for(int i = 0; i < cipher.length(); i += 2)
{
int c1 = cipher[i] - 'A';
int c2 = cipher[i+1] - 'A';
plain += char((invKey[0][0]*c1 + invKey[0][1]*c2) % 26 + 'A');
plain += char((invKey[1][0]*c1 + invKey[1][1]*c2) % 26 + 'A');
}
return plain;
}
int main()
{
int key[2][2];
string message;
cout << "Enter 4 integers for the key matrix (2x2): ";
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
cin >> key[i][j];
cout << "Enter message (uppercase letters only): ";
cin >> message;
// Ensure even length
if(message.length() % 2 != 0)
message += 'X';
string cipher = encrypt(message, key);
cout << "Encrypted message: " << cipher << endl;
string decrypted = decrypt(cipher, key);
cout << "Decrypted message: " << decrypted << endl;
return 0;
}