rc4

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值