简单的RC4代码加密实现
代码语言:javascript复制struct rc4_state
{
int x, y, m[256];
};
void rc4_setup(struct rc4_state *s, unsigned char *key, int length)
{
int i, j, k, *m, a;
s->x = 0;
s->y = 0;
m = s->m;
for(i = 0; i < 256; i )
{
m[i] = i;
}
j = k = 0;
for(i = 0; i < 256; i )
{
a = m[i];
j = (unsigned char) (j a key[k]);
m[i] = m[j]; m[j] = a;
if( k >= length) k = 0;
}
}
//******************************************************************************
void rc4_crypt( struct rc4_state *s, unsigned char *data, int length)
{
int i, x, y, *m, a, b;
x = s->x;
y = s->y;
m = s->m;
for( i = 0; i < length; i )
{
x = (unsigned char) (x 1); a = m[x];
y = (unsigned char) (y a);
m[x] = b = m[y];
m[y] = a;
data[i] ^= m[(unsigned char) (a b)];
}
s->x = x;
s->y = y;
}
BOOL EntryptionBuf(char* Buf, DWORD dwlen)
{
BYTE rc4_key[8] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
rc4_state rc4_test;
memset(&rc4_test, 0, sizeof(rc4_test));
try
{
rc4_setup(&rc4_test, rc4_key, 8);
rc4_crypt(&rc4_test, (unsigned char*)Buf, dwlen);
}
catch (...)
{
return FALSE;
}
return TRUE;
}
int main()
{
EntryptionBuf(youBuf,youBufSize);
}