📄 rc4.c
字号:
#include "config.h"
#define RC4_KEYSTATE_NUM 32 //默认:256
typedef struct rc4_key
{
uint8 state[RC4_KEYSTATE_NUM];
uint8 x;
uint8 y;
} rc4_key;
void prepareKey(const uint8 *keygen, uint32 keyDataLen, rc4_key *key)
{
int i;
uint8 *keyData;
uint8 keyDataSeed[RC4_KEYSTATE_NUM];
uint8 swapByte;
uint8 index1;
uint8 index2;
uint8* state;
short counter;
keyData = (uint8 *)(keygen);
if ( keyDataLen > 512 )
keyDataLen = 512;
keyDataLen = (int)keyDataLen / 2;
for (i=0;i<keyDataLen;i++)
{
keyDataSeed[i] = (keyData[i*2] + keyData[i*2+1])%RC4_KEYSTATE_NUM;
}
state = &key->state[0];
for(counter = 0; counter < RC4_KEYSTATE_NUM; counter++)
state[counter] = (uint8)counter;
key->x = 0; key->y = 0;
index1 = 0; index2 = 0;
for(counter = 0; counter < RC4_KEYSTATE_NUM; counter++)
{
index2 = (keyDataSeed[index1] + state[counter] + index2) % RC4_KEYSTATE_NUM;
swapByte = state[counter];
state[counter] = state[index2];
state[index2] = swapByte;
index1 = (index1 + 1) % keyDataLen;
}
}
void RC4(const uint8 *keygen, uint32 keyDataLen, void *dataBuffer, uint16 bufferLen)
{
uint8 x;
uint8 y;
uint16 counter;
uint8 swapByte;
uint8* state;
uint8 xorIndex;
uint8 *buffer_ptr;
rc4_key key;
prepareKey(keygen, keyDataLen, &key);
buffer_ptr = (uint8 *)(dataBuffer);
x = key.x;
y = key.y;
state = &key.state[0];
for(counter = 0; counter < bufferLen; counter++)
{
x = (x + 1) % RC4_KEYSTATE_NUM;
y = (state[x] + y) % RC4_KEYSTATE_NUM;
swapByte = state[x];
state[x] = state[y];
state[y] = swapByte;
xorIndex = (state[x] + state[y]) % RC4_KEYSTATE_NUM;
buffer_ptr[counter] ^= state[xorIndex];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -