-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecComponent.cpp
More file actions
163 lines (151 loc) · 4.59 KB
/
secComponent.cpp
File metadata and controls
163 lines (151 loc) · 4.59 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
(c) Matthew Slocum 2015
secComponent.cpp
secComponent is responsible for the encryption/decryption of messages.
This file contains code from the public domain found at https://web.archive.org/web/20050110121238/http://www.xs4all.nl/~cg/ciphersaber/comp/c++.txt
// ciphersabre-1 by graydon hoare
// placed in public domain, jun 2000
This file contain sudo code from https://github.com/PSU-CS-300-Fall2015/ciphersaber2
this sudo code is licenced under the MIT Licence
*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include "secComponent.h"
using namespace std;
secComponent::secComponent() {
}
//"Produce an RC4 keystream of length" n "with" r "rounds of key scheduling given key" k "of length" l
//returns keystream
void secComponent::rc4(int n, int r, char* k, int l, char* keystream) {
//init array
unsigned char S [256] = { };
for(int i=0; i<256; i++) {
S[i]=i;
}
//do key scheduling
int j=0;
for(int ri=0; ri<r; ri++) {
for(int i=0; i<256; i++) {
j = (j + S[i] + k[i%l]) % 256;
swap(S[i], S[j]);
}
}
//produce the keystream
j=0;
int ip;
for(int i=0; i<n; i++) {
ip = (i+1) % 256;
j = (j+S[ip]) % 256;
swap(S[ip], S[j]);
keystream[i] = S[(S[ip]+S[j]) % 256];
}
}
//"Ciphersaber-2 encrypt message" m "with key" k "and" r "rounds of key scheduling" "outputting to" ciphertext
//return length of ciphertext
int secComponent::encrypt(string m, string k, int r, char * ciphertext) {
//get length of message
int n = m.length();
//generate iv
unsigned char iv [10];
ifstream rng("/dev/urandom");
if (!rng) { cerr << "canot open /dev/urandom" << endl; exit(1); }
for (int i=0; i<10; ++i) {
iv[i]=rng.get();
}
//create a holder(kp) for the key + iv
char kp[10+k.length()];
//load the key into kp
for(unsigned int i=0; i<k.length(); i++) {
kp[i] = k[i];
}
//load the iv into kp
for(unsigned int i=k.length(); i<k.length()+10; i++) {
kp[i] = iv[i-k.length()];
}
//get a keystream
char keystream [1024];
rc4(n, r, kp, k.length()+10, keystream);
//load the iv into the payload
for(int i=0; i<10; i++) {
ciphertext[i] = iv[i];
}
//encrypt the message with the keystream and save to output
for(int i=0; i<n; i++) {
ciphertext[i+10] = m[i] ^ keystream[i];
}
//return size of ciphertext
return n+10;
}
//"Ciphersaber-2 decrypt ciphertext" m "with key" k "and" r "rounds of key scheduling" "ouputting to" plaintext
//returns length of plaintext
int secComponent::decrypt(char* m, int m_len, string k, int r, char* plaintext) {
//get length of m (unneccisary)
int n = m_len;
//load iv
char iv [10];
for(int i=0; i<10; i++) {
iv[i] = m[i];
}
//get the message without the iv
char msg_no_iv [m_len-10];
for(int k=0; k<m_len-10; k++) {
msg_no_iv[k]=m[k+10];
}
//prepend k to iv (store in kp)
char kp[k.length()+10];
//put the key in
for(unsigned int i=0; i<k.length(); i++) {
kp[i] = k[i];
}
//put the iv in
for(unsigned int i=k.length(); i<k.length()+10; i++) {
kp[i] = iv[i-k.length()];
}
//get a keystream
char keystream [256];
rc4(n, r, kp, k.length()+10, keystream);
//decrypt the message with the keystream and save to output
for(int i=0; i<n-10; i++) {
plaintext[i] = msg_no_iv[i] ^ keystream[i];
}
//return the length of the plaintext
return n-10;
}
int secComponent::decrypt_long(char* m, int m_len, string k, int r, char* plaintext) {
//get length of m (unneccisary)
int n = m_len;
//load iv
char iv [10];
for(int i=0; i<10; i++) {
iv[i] = m[i];
}
//get the message without the iv
char msg_no_iv [m_len-10];
for(int k=0; k<m_len-10; k++) {
msg_no_iv[k]=m[k+10];
}
//prepend k to iv (store in kp)
char kp[k.length()+10];
//put the key in
for(unsigned int i=0; i<k.length(); i++) {
kp[i] = k[i];
}
//put the iv in
for(unsigned int i=k.length(); i<k.length()+10; i++) {
kp[i] = iv[i-k.length()];
}
//get a keystream
char keystream [20474];
rc4(n, r, kp, k.length()+10, keystream);
//decrypt the message with the keystream and save to output
for(int i=0; i<n-10; i++) {
plaintext[i] = msg_no_iv[i] ^ keystream[i];
}
//return the length of the plaintext
return n-10;
}