forked from lsw29475/iAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiRc4.cpp
More file actions
152 lines (124 loc) · 2.53 KB
/
Copy pathiRc4.cpp
File metadata and controls
152 lines (124 loc) · 2.53 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
#include "iRc4.h"
#include "macro.h"
CRC4::CRC4(void)
{
pRC4Key = NULL;
RC4KeySize = 0;
pRC4StreamKey = NULL;
RC4StreamKeySize = 0;
}
CRC4::~CRC4(void)
{
if (pRC4Key)
{
delete[] pRC4Key;
pRC4Key = NULL;
RC4KeySize = 0;
}
if (pRC4StreamKey)
{
delete[] pRC4StreamKey;
pRC4StreamKey = NULL;
RC4StreamKeySize = 0;
}
}
void CRC4::InitStateAndTemp()
{
for (int i = 0; i < 256; i++)
{
State[i] = i;
Temp[i] = pRC4Key[i % RC4KeySize];
}
}
void CRC4::RangeState()
{
int j = 0;
unsigned char tmp;
for (int i = 0; i < 256; ++i)
{
j = (j + State[i] + Temp[i]) % 256;
tmp = State[i];
State[i] = State[j];
State[j] = tmp;
}
}
void CRC4::KeyStream(int BufferLength)
{
int i = 0, j = 0, t;
unsigned char tmp;
if (pRC4StreamKey)
{
delete[] pRC4StreamKey;
pRC4StreamKey = NULL;
RC4StreamKeySize = 0;
}
pRC4StreamKey = new unsigned char[BufferLength];
memset(pRC4StreamKey, 0x00, BufferLength);
RC4StreamKeySize = BufferLength;
for (int n = 0; n < RC4StreamKeySize; n++)
{
i = (i + 1) % 256;
j = (j + State[i]) % 256;
tmp = State[i];
State[i] = State[j];
State[j] = tmp;
t = (State[i] + State[j]) % 256;
pRC4StreamKey[n] = State[t];
}
}
bool CRC4::SetKey(unsigned char* pKey, int KeySize)
{
if (KeySize < 1 || KeySize > 256)
{
return false;
}
if (pRC4Key)
{
delete[] pRC4Key;
pRC4Key = NULL;
KeySize = 0;
}
pRC4Key = new unsigned char[KeySize];
memset(pRC4Key, 0x00, KeySize);
memcpy(pRC4Key, pKey, KeySize);
RC4KeySize = KeySize;
return true;
}
bool CRC4::InitKey(unsigned char* pKey, int KeySize)
{
if (!SetKey(pKey, KeySize))
{
return false;
}
return true;
}
bool CRC4::EncryptData(unsigned char* pBufferIn, int BufferInSize, unsigned char* pBufferOut, int BufferOutSize)
{
if ((BufferOutSize != BufferInSize) && (BufferOutSize < BufferInSize))
{
return false;
}
InitStateAndTemp();
RangeState();
KeyStream(BufferInSize);
for (int i = 0; i < BufferInSize; i++)
{
pBufferOut[i] = pBufferIn[i] ^ pRC4StreamKey[i];
}
return true;
}
bool CRC4::DecryptData(unsigned char* pBufferIn, int BufferInSize, unsigned char* pBufferOut, int BufferOutSize)
{
if ((BufferOutSize != BufferInSize) && (BufferOutSize < BufferInSize))
{
return false;
}
InitStateAndTemp();
RangeState();
KeyStream(BufferInSize);
for (int i = 0; i < BufferInSize; i++)
{
pBufferOut[i] = pBufferIn[i] ^ pRC4StreamKey[i];
}
return true;
}