-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc.cpp
More file actions
69 lines (64 loc) · 1.67 KB
/
Copy pathrc.cpp
File metadata and controls
69 lines (64 loc) · 1.67 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
//Name: Nicholas Noto
//CruzID: nnoto
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
void ksa(vector<unsigned char> &s, string key);
void prga(vector<unsigned char> &s, vector<unsigned char> &out, int keyLength);
int main() {
vector<unsigned char> state(256);
vector<unsigned char> stream(1024);
string plaintext = "", key = "";
int KeyLength = 16;
cout << "Enter Key: ";
getline(cin, key);
cout << "Enter text: ";
getline(cin, plaintext);
cout << "Text to encrypt: " << plaintext << endl;
ksa(state,key);
prga(state,stream, KeyLength);
cout << "Cipher: ";
for (int i=0; i < plaintext.length(); i++){
//XOR plaintext with keystream
plaintext[i] ^= stream[i];
printf("%02X ", (unsigned char)plaintext[i]);
}
cout << "\nText Decrypted: ";
for (int i=0; i < plaintext.length(); i++){
plaintext[i] ^= stream[i];
cout << plaintext[i];
}
cout << endl;
return 0;
}
// Key Scheduling Algorithm
void ksa(vector<unsigned char> &s, string key) {
int j = 0, temp = 0;
//initialize s array
for(int i = 0; i < 256; i++)
s[i] = i;
for(int i = 0; i < 256; i++) {
j = (j + s[i] + key[i % key.length()]) % 256;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
// Pseudo-Random Generator Algorithm
void prga(vector<unsigned char> &s,vector<unsigned char> &output, int keyLength) {
int j = 0, k = 0, temp = 0;
cout << "Keystream: ";
for(int i = 0; i < keyLength; i++) {
k = (k + 1) % 256;
j = (j + s[k]) % 256;
temp = s[k];
s[k] = s[j];
s[j] = temp;
output[i] = s[(s[k] + s[j]) % 256];
printf("%02X ", output[i]);
}
cout << endl;
}