-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rc4.cpp
66 lines (57 loc) · 1.84 KB
/
Rc4.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
/*
* Copyright 2006 Mike McCormack
*
* based on arc4.cpp - written and placed in the public domain by Wei Dai
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* http://cryptopp.sourceforge.net/docs/ref521/arc4_8cpp-source.html */
#include "Rc4.h"
void CRc4::Init(CContext* a4i, const void* key, uint32_t keyLen) {
uint32_t keyIndex = 0, stateIndex = 0;
uint32_t i, a;
a4i->x = a4i->y = 0;
for (i = 0; i < 256; i++) {
a4i->state[i] = i;
}
for (i = 0; i < 256; i++) {
a = a4i->state[i];
stateIndex += ((const uint8_t*)key)[keyIndex] + a;
stateIndex &= 0xff;
a4i->state[i] = a4i->state[stateIndex];
a4i->state[stateIndex] = a;
if (++keyIndex >= keyLen) {
keyIndex = 0;
}
}
}
void CRc4::Crypt(CContext* a4i, void* inoutString, uint32_t length) {
uint8_t* const s = a4i->state;
uint32_t x = a4i->x;
uint32_t y = a4i->y;
uint32_t a, b;
while (length--) {
x = (x + 1) & 0xff;
a = s[x];
y = (y + a) & 0xff;
b = s[y];
s[x] = b;
s[y] = a;
*(uint8_t*)inoutString ^= s[(a + b) & 0xff];
inoutString = (uint8_t*)inoutString + 1;
}
a4i->x = x;
a4i->y = y;
}