#include <windows.h>
#include <stdio.h>
static UCHAR g_DeCryptKey[48] = {0xDB, 0x22, 0x98, 0x90, 0x5B, 0xCB, 0x3A, 0x91, 0x92, 0xCA, 0xC4, 0x33, 0x0E, 0xDB, 0xBB, 0x55,
0x78, 0x02, 0xD8, 0x24, 0x91, 0x5C, 0x25, 0xBB, 0xC4, 0x53, 0xEE, 0xCA, 0x41, 0x60, 0xFB, 0xB8,
0x61, 0xEF, 0x52, 0x1C, 0xAD, 0xEA, 0xC3, 0x4F, 0xB7, 0xD4, 0x74, 0x69, 0xD6, 0x1B, 0x98, 0xA6};
VOID Rc4Init(UCHAR s[256], PUCHAR key, ULONG Len)
{
ULONG i = 0, j = 0;
UCHAR k[256] = { 0 };
UCHAR tmp = 0;
for (i = 0; i < 256; i++)
{
s[i] = (UCHAR)i;
k[i] = key[i%Len];
}
for (i = 0; i<256; i++)
{
j = (j + s[i] + k[i]) % 256;
tmp = s[i];
s[i] = s[j];//交换s[i]和s[j]
s[j] = tmp;
}
}
VOID Rc4Crypt(UCHAR s[256], PUCHAR pData, ULONG Len)
{
ULONG i = 0, j = 0, t = 0;
ULONG k = 0;
UCHAR tmp;
for (k = 0, i= 0; k<Len; k++)
{
i = (i + 1) % 256;
j = (j + s[i]) % 256;
tmp = s[i];
s[i] = s[j];//交换s[x]和s[y]
s[j] = tmp;
t = (s[i] + s[j]) % 256;
pData[k] ^= s[t];
}
}
//加密RC4参数
PUCHAR CryptRc4Paramter(char* PParamter)
{
UCHAR sTemp[256];
UINT nLineLen=0;
PUCHAR pRc4Data=NULL;
do
{
nLineLen=strlen(PParamter);
if (nLineLen<=0)break;
//分配内存
pRc4Data=new UCHAR[nLineLen+sizeof(DWORD)];
ZeroMemory((void*)pRc4Data,nLineLen+sizeof(DWORD));
CopyMemory(pRc4Data,PParamter,nLineLen);
//加密数据
ZeroMemory(sTemp,sizeof(sTemp));
Rc4Init(sTemp,g_DeCryptKey,sizeof(g_DeCryptKey));
Rc4Crypt(sTemp,pRc4Data,nLineLen);
解密测试(注意加密后可能某位为00,strlen会截取掉)
//Rc4Init(sTemp,g_DeCryptKey,sizeof(g_DeCryptKey));
//Rc4Crypt(sTemp,pRc4Data,nLineLen);
//MessageBoxA(NULL,(LPCSTR)pRc4Data,("解密数据"),MB_OK);
} while (FALSE);
return pRc4Data;
}
int main()
{
CryptRc4Paramter("1234567heloo呵呵");
getchar();
getchar();
return 0;
}
rc4
最新推荐文章于 2024-06-19 22:44:08 发布