RC4加密算法C语言/C++实现

运行环境 VC++ 6.0 

#include <iostream>
#include <string.h>
const int max=256;
const int BOX_LEN=256;
//把字节码转为十六进制码,一个字节两个十六进制,内部为字符串分配空间
char* ByteToHex(const unsigned char* Byte, const int Len)
{
	if(!Byte) 
		return NULL;
	char* tmp = new char[Len * 2 + 1]; 
	int tmp2;
	for (int i=0;i<Len;i++)
	{
		tmp2 = (int)(Byte[i])/16;
		tmp[i*2] = (char)(tmp2+((tmp2>9)?'A'-10:'0'));
		tmp2 = (int)(Byte[i])%16;
		tmp[i*2+1] = (char)(tmp2+((tmp2>9)?'A'-10:'0'));
	}
	tmp[Len * 2] = '\0';
	return tmp;
}
//把十六进制字符串,转为字节码,每两个十六进制字符作为一个字节
unsigned char* HexToByte(const char* szHex)
{
	int length = strlen(szHex);
	unsigned char* pbBuf = new unsigned char[length/2]; //数据缓冲区
	int tmp1, tmp2;
	for (int i=0;i<length/2;i++)
	{
		tmp1 = (int)szHex[i*2] - (((int)szHex[i*2]>='A')?'A'-10:'0');
		if(tmp1>=16) 
			return NULL;
		tmp2 = (int)szHex[i*2+1] - (((int)szHex[i*2+1]>='A')?'A'-10:'0');
		if(tmp2>=16)
			return NULL;
		pbBuf[i] = (tmp1*16+tmp2);
	}
	return pbBuf;
}
//交换
static void swap_byte(unsigned char* a, unsigned char* b)
{
	unsigned char swapByte; 
	swapByte = *a;
	*a = *b;
	*b = swapByte;
}
int GetKey(const unsigned char* pass, int pass_len, unsigned char* out)
{
	int i;
	for(i = 0; i < BOX_LEN; i++)
	{
		out[i] = i;
	}
	int j = 0;
	for(i = 0; i < BOX_LEN; i++)
	{
		j = (pass[i % pass_len] + out[i] + j) % BOX_LEN;
		swap_byte(&out[i], &out[j]); 
	}
	return -1;
}
int RC4(const unsigned char* data, int data_len, const unsigned char* key, int key_len, unsigned char* out, int* out_len)
{
	unsigned char* mBox = new unsigned char[BOX_LEN];
	int i=0;
	int x=0;
	int y=0;
	for(int k = 0; k < data_len; k++)
	{
		x = (x + 1) % BOX_LEN;
		y = (mBox[x] + y) % BOX_LEN;
		swap_byte(&mBox[x], &mBox[y]);
		out[k] = data[k] ^ mBox[(mBox[x] + mBox[y]) % BOX_LEN];
	}
	*out_len = data_len;
	delete[] mBox;
	return -1;
}
// 加密,返回加密结果
char* Encrypt(const char* szSource, const char* szPassWord)
{
	unsigned char* ret = new unsigned char[strlen(szSource)];
	int ret_len = 0;
	if(RC4((unsigned char*)szSource, 
		strlen(szSource), 
		(unsigned char*)szPassWord,
		strlen(szPassWord),
		ret,
		&ret_len) == NULL) 
		return NULL;
	char* ret2 = ByteToHex(ret, ret_len);
	delete[] ret;
	return ret2;
}
// 解密,返回解密结果
char* Decrypt(const char* szSource, const char* szPassWord) 
{
	unsigned char* src = HexToByte(szSource);
	unsigned char* ret = new unsigned char[strlen(szSource) / 2 + 1];
	int ret_len = 0;
	memset(ret, strlen(szSource) / 2 + 1,0);
	if(RC4(src, strlen(szSource) / 2, (unsigned char*)szPassWord, strlen(szPassWord), ret, &ret_len) == NULL) 
		return NULL;
	ret[ret_len] = '\0';
	return (char*)ret;
}
int main(int argc,char *argv[]) 
{
	int size = 0;
	//用户输入明文
	char Message[max];
	printf("请输入需要加密的信息:");
	scanf("%s", Message);
	//密钥
	char Key[max];
	printf("\n请输入密钥:");
	scanf("%s", Key);
	//加密、解密结果
	char *result1 = NULL;
	char *result2 = NULL;
	//加密
	result1 = Encrypt(Message, Key);
	printf("\n加密结果:%s\n", result1);
	//解密
	result2 = Decrypt(result1, Key);
	printf("\n解密结果:%s\n\n", result2);
	return 0; 
}

 

 

 

  • 4
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值