-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRC4_cipher.cpp
executable file
·109 lines (89 loc) · 2.39 KB
/
RC4_cipher.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <vector>
#include <algorithm>
class RC4 {
private:
std::vector<int> S;
int i, j;
void ksa(const std::vector<int>& key)
{
S.resize(256);
for (int i = 0; i < 256; ++i)
S[i] = i;
j = 0;
for (int i = 0; i < 256; ++i)
{
j = (j + S[i] + key[i % key.size()]) % 256;
std::swap(S[i], S[j]);
}
// for( int i : S)
// std:: cout<< i << " ";
}
int prga() {
i = (i + 1) % 256;
j = (j + S[i]) % 256;
std::swap(S[i], S[j]);
// std::cout<<S[(S[i] + S[j]) % 256]<<" ";
return S[(S[i] + S[j]) % 256];
}
public:
RC4(const std::vector<int>& key)
{
ksa(key);
i = j = 0;
}
std::vector<int> encrypt(const std::vector<int>& data)
{
std::vector<int> result;
for (int byte : data)
result.push_back(byte ^ prga());
return result;
}
// Decryption is the same as encryption
std::vector<int> decrypt(const std::vector<int>& data) {
return encrypt(data);
}
};
void Input_Plaintext(std::vector<int> &plaintext)
{
std::string mess;
std::getline(std::cin, mess);
for( int i = 0; i < mess.length(); i ++)
{
plaintext.push_back((int)mess[i]);
}
}
void Input_key(std::vector<int> &key)
{
std::string seed;
std::getline(std::cin, seed);
for(int i = 0; i< seed.length(); i++)
{
if(seed[i]<48 || seed[i]> 57)
key.push_back((int)seed[i]);
else key.push_back((int)seed[i] - 48);
}
}
int main() {
std::vector<int> key ;
std::vector<int> plaintext;
std::cout<< "Enter the key: ";
Input_key(key);
std::cout<<"Enter the message: ";
Input_Plaintext(plaintext);
RC4 rc4_encrypt(key);
// Encrypt
std::vector<int> encrypted_data = rc4_encrypt.encrypt(plaintext);
std::cout<< "Encrypted: ";
for (int byte : encrypted_data)
std::cout << (char)byte<< " ";
std::cout << std::endl;
// Decrypt
RC4 rc4_decrypt(key);
std::vector<int> decrypted_data = rc4_decrypt.decrypt(encrypted_data);
std::cout << "Decrypted: ";
for (int byte : decrypted_data)
std::cout << char(byte);
std::cout << std::endl;
return 0;
}