首页
社区
课程
招聘
[旧帖] [求助]大家帮我看看什么地方出错了(RC5) 0.00雪花
发表于: 2010-5-24 14:29 2925

[旧帖] [求助]大家帮我看看什么地方出错了(RC5) 0.00雪花

2010-5-24 14:29
2925
加密后,解密出来的内容不是原文,找了好半天也不知道什么地出了。请高手指正,谢谢大侠了

void CRC5Base::InitialKeyAndRounds(unsigned char *key, int iKeyLength, int iRounds)
{
	m_rc5Key.iKeyLong = iKeyLength;	
	this->iRounds = iRounds;
	int T = 2 * iRounds + 2;
	int i , j, c, t;
	c = iKeyLength / 4;

	RC5_WORD *S = new RC5_WORD[T];
	S[0] = P32;
	for (i = 1; i < T; i++)
	{
		S[i] = S[i-1] + Q32;
	}
	m_rc5Key.KeyExpanded = S;

	RC5_WORD *L = new RC5_WORD[c];
	for (i = 0;i< c; i++)
	{
		L[i] = 0;
	}

	for (i = 0; i < iKeyLength; i++)
	{
		t = (key[i] & 0xFF) << (8 * (i % 4));
		L[i/4] = L[i/4] + t;
	}


	i = 0;
	j = 0;
	RC5_WORD X = 0;
	RC5_WORD Y = 0;

	int k = (T > c) ? 3 * T : 3 * c;
	
	
	while ( k --)
	{		
		m_rc5Key.KeyExpanded[i] = ROTL(m_rc5Key.KeyExpanded[i] + X + Y, 3);
		X = m_rc5Key.KeyExpanded[i];
		i = (i + 1) % T;
		
		L[j] = ROTL(L[j] + X + Y, X + Y);
		Y = L[j];
		j = (j + 1) % c;
	}
	m_rc5Key.KeyExpanded[0] = m_rc5Key.KeyExpanded[0];
	m_rc5Key.KeyExpanded[1] = m_rc5Key.KeyExpanded[1];
	m_rc5Key.KeyExpanded[2] = m_rc5Key.KeyExpanded[2];
	delete L;
}

RC5_WORD CRC5Base::ROTL(RC5_WORD s, RC5_WORD x)
{
	RC5_WORD Temp = 0;
	Temp = (s << x ) | (s >> (32 - x ));	
	
	return Temp;
}	

RC5_WORD CRC5Base::ROTR(RC5_WORD s, RC5_WORD x)
{
	RC5_WORD Temp = 0;
	Temp = (s >> x ) | (s << (32 - x));

	return Temp;
}


void CRC5Base::Encrypt(unsigned char *data)
{
	RC5_WORD *S = m_rc5Key.KeyExpanded;
	RC5_WORD A, B;
	A  =  data[0] & 0xFF;
	A += (data[1] & 0xFF) << 8;
	A += (data[2] & 0xFF) << 16;
	A += (data[3] & 0xFF) << 24;

	B  = data[4] & 0xFF;
	B += (data[5] & 0xFF) << 8;
	B += (data[6] & 0xFF) << 16;
	B += (data[7] & 0xFF) << 24;

	A = A + S[0];
	B = B + S[1];

	for (int i = 1; i <= iRounds; i++)
	{

		A = ROTL(A ^ B, B) + S[2 * i];
		B = ROTL(B ^ A, A) + S[2 * i + 1];
	}

	data[0] = (A >> 0) & 0xFF;
	data[1] = (A >> 8) & 0xFF;
	data[2] = (A >> 16) & 0xFF;
	data[3] = (A >> 24) & 0xFF;

	data[4] = (B >> 0) & 0xFF;
	data[5] = (B >> 8) & 0xFF;
	data[6] = (B >> 16) & 0xFF;
	data[7] = (B >> 24) & 0xFF;

}

void CRC5Base::Decrypt(unsigned char *data)
{
	RC5_WORD *S = m_rc5Key.KeyExpanded;
	RC5_WORD A, B;
	A  =  data[0] & 0xFF;
	A += (data[1] & 0xFF) << 8;
	A += (data[2] & 0xFF) << 16;
	A += (data[3] & 0xFF) << 24;

	B  =  data[4] & 0xFF;
	B += (data[5] & 0xFF) << 8;
	B += (data[6] & 0xFF) << 16;
	B += (data[7] & 0xFF) << 24;

	for (int i = iRounds; i > 0; i--)
	{
		B = (ROTR(B - S[2 * iRounds + 1], A)) ^ A;
		A = (ROTR(A - S[2 * iRounds], B)) ^ B;
	}

	B = B - S[1];
	A = A - S[0];
	
	data[0] = (A >> 0)  & 0xFF;
	data[1] = (A >> 8)  & 0xFF;
	data[2] = (A >> 16) & 0xFF;
	data[3] = (A >> 24) & 0xFF;

	data[4] = (B >> 0)  & 0xFF;
	data[5] = (B >> 8)  & 0xFF;
	data[6] = (B >> 16) & 0xFF;
	data[7] = (B >> 24) & 0xFF;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (4)
雪    币: 993
活跃值: (442)
能力值: ( LV12,RANK:403 )
在线值:
发帖
回帖
粉丝
2
参照RFC2040
http://tools.ietf.org/html/rfc2040
2010-5-24 14:54
0
雪    币: 11
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
做下来慢慢看在说!
2010-5-25 11:57
0
雪    币: 162
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
大家帮帮忙了,真不知道什么地方出错了
2010-5-25 20:51
0
雪    币: 114
活跃值: (70)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
5
已经回答过了,你也登录过了。就是不结贴,叹......

哪位大侠给我个邀请码啊?
谢谢

NJZhuJinhua

(NJZhuJinhua)

等 级:

#2楼 得分:0回复于:2010-05-25 22:18:34void CRC5Base::Decrypt(unsigned char *data)
中的
C/C++ code
    for (int i = iRounds; i > 0; i--)
    {
        B = (ROTR(B - S[2 * iRounds + 1], A)) ^ A;
        A = (ROTR(A - S[2 * iRounds], B)) ^ B;
    }

将iRounds换成i即可

这里iRounds为相对来说定值。不用多说了
2010-5-27 12:21
0
游客
登录 | 注册 方可回帖
返回
//