在c++项目中使用rc5对数据传输进行加密的方法
具体方法如下:
#include "stdafx.h"
#include#include #include #define UINT32 unsigned int
#define INT32 int
#define UINT16 unsigned short
#define INT16 short
#define UINT8 unsigned char
#define INT8 char
unsigned int subKey[25];
void generateSubKey()
{
UINT8 i;
for(i=0;i<25;i++)
subKey[i] = i;
}
void encrypt(UINT32 *PText,UINT32 *CText)
{//对数据传输进行加密
UINT32 PData[1];
UINT32 CData[1];
UINT32 tempData1,tempData2,temp;
UINT8 i;
PData[0] = *PText;
PText++;
PData[1] = *PText;
CData[0] = *CText;
CText++;
CData[1] = *CText;
tempData1 = PData[0] + subKey[0];
tempData2 = PData[1] + subKey[1];
for(i=1;i<=12;i++)
{
temp = tempData1;
tempData1 ^= tempData2;
_rotl(tempData1,tempData2);
tempData1 += subKey[2*i];
tempData2 ^= temp;
_rotl(tempData2,temp);
tempData2 += subKey[2*i + 1];
}
CData[0] = tempData1;
CData[1] = tempData2;
}
int main()
{
UINT32 PlainText[1];
UINT32 CipherText[1];
generateSubKey();
cout<<"Hello!!\n\n";
PlainText[0] = 0x5a5a5a5a;
PlainText[1] = 0x12345678;
cout<<"加密前的数据为:"<encrypt(PlainText,CipherText);
cout<<"加密后的数据为:"<return 0;
}