📄 rc5.cpp
字号:
/* RC5.c -- Reference implementation of RC5-32/12/16 in C. */
/* Copyright (C) 1995 RSA Data Security, Inc. */
#include "rc5.h"
#include <string.h>
CRC5::CRC5()
{
}
void CRC5::ENCRYPT(LWORD *pt, LWORD *ct)
/* Do Encrypt 2 WORD input pt/output ct*/
{
LWORD i, A=pt[0]+S[0], B=pt[1]+S[1];
for (i=1; i<=rn; i++)
{
A = ROTL(A^B,B)+S[2*i];
B = ROTL(B^A,A)+S[2*i+1];
}
ct[0] = A;
ct[1] = B;
}
void CRC5::DECRYPT(LWORD *ct, LWORD *pt)
/*Do Decrypt 2 WORD input ct/output pt */
{
LWORD i, B=ct[1], A=ct[0];
for (i=rn; i>0; i--)
{
B = ROTR(B-S[2*i+1],A)^A;
A = ROTR(A-S[2*i],B)^B;
}
pt[1] = B-S[1];
pt[0] = A-S[0];
}
void CRC5::SETUP(unsigned char *K) /* secret input key K[0...b-1] */
{
LWORD P = 0xb7e15163;
LWORD Q = 0x9e3779b9; /* magic constants */
LWORD i, j, k, u=ws/8, A, B, L[cn];
/* Initialize L, then S, then mix key into S */
for (i=bn-1,L[cn-1]=0; i!=-1; i--)
L[i/u] = (L[i/u]<<8)+K[i];
for (S[0]=P,i=1; i<ts; i++)
S[i] = S[i-1]+Q;
for (A=B=i=j=k=0; k<3*ts; k++,i=(i+1)%ts,j=(j+1)%cn) /* 3*t > 3*c */
{
A = S[i] = ROTL(S[i]+(A+B),3);
B = L[j] = ROTL(L[j]+(A+B),(A+B));
}
}
/*change the miwen stored in char to longch is type of unsigned char ,lg is type of unsigned long*/
unsigned char CRC5::CharToLong(unsigned char *ch,unsigned long *lg)
{
int i;
lg[0]=lg[1]=0L;
for(i = 0;i<4;i++)
{
lg[0]=lg[0] | ch[i];
if (i != 3 )
lg[0]=lg[0]<<8;
}
for(i = 4;i<8;i++)
{
lg[1]=lg[1] | ch[i];
if (i != 7 )
lg[1]=lg[1]<<8;
}
return 0;
}
/*change the miwen stored in long to charlg store unsigned long,ch store unsigned char */
unsigned char CRC5::LongToChar(unsigned long *lg,unsigned char *ch)
{
unsigned long ltc[]={
0xff000000,0x00ff0000,
0x0000ff00,0x000000ff
};
int i;
for(i = 0;i<4;i++)
{
ch[i]=(unsigned char)((lg[0] & ltc[i])>>(24-8*i));
}
for(i = 4;i<8;i++)
{
ch[i]=(unsigned char)((lg[1] & ltc[i-4])>>(24-8*(i-4)));
}
return 0;
}
void CRC5::Setup(unsigned char *K)
{
unsigned char key[16];
int len;
len = strlen((char *)K);
if(len>16)
len = 16;
memset(key, 0, 16);
memcpy(key, K, len);
SETUP(key);
}
unsigned long CRC5::Encrypt(unsigned char * ibuf,unsigned char * obuf,unsigned long len)
{
unsigned long lent,i,ilg[2],olg[2];
/*lent:lenth of the char can encrypted
ilg:every 4 word of will encrypted char
olg:every 4 word of encrypted char
*/
unsigned char * iptr=ibuf, *optr=obuf;
lent=len&0xFFFFFFF8; /*let lent is times of 8 */
for(i=0;i<lent/8;i++) /*every 8 bytes one time*/
{
ilg[0]=0L;
ilg[1]=0L;
olg[0]=0L;
olg[1]=0L;
CharToLong(iptr,ilg);
ENCRYPT(ilg,olg);
LongToChar(olg,optr);
iptr+=8;
optr+=8;
}
if(lent<len) /*copy the rest char*/
{
for(i=lent;i<len;i++)
{
*optr=*iptr;
optr++;
iptr++;
}
}
return lent;
}
unsigned long CRC5::Decrypt(unsigned char * ibuf,unsigned char * obuf,unsigned long len)
{
unsigned long i,lent,ilg[2],olg[2];
/* lent:lenth of char will be decrypted
ilg:every 4 word of will be decrypted
olg:every 4 word of decrypted char
*/
unsigned char * iptr=ibuf,* optr=obuf;
/*let lenth of char will be decrypted is times 8 */
lent=len&0xFFFFFFF8;
for (i=0;i<lent/8;i++)
{
ilg[0]=0L;
ilg[1]=0L;
olg[0]=0L;
olg[1]=0L;
CharToLong(iptr,ilg);
DECRYPT(ilg,olg);
LongToChar(olg,optr);
iptr+=8;
optr+=8;
}
if(lent<len)
{
for(i=lent;i<len;i++)
{
*optr=*iptr;
iptr++;
optr++;
}
}
return lent;
}
CString CRC5::Decrypt(CString str)
{
unsigned char key[16]={0x7, 0x0, 0x9, 0x1, 0x5, 0x70, 0x91, 0x5,
0x51, 0x90, 0x7, 0x5, 0x1, 0x9, 0x0, 0x7};
Setup(key);
int len=str.GetLength();
char * bin;
bin = PCHAR(malloc(len+1));
memset(bin, 0, len+1);
HexToBin(PCHAR(str.GetBuffer(len)), bin, len);
len=len/2;
UPCHAR obuf;
obuf = byte*(malloc(len+1));
memset(obuf, 0, len+1);
Decrypt(UPCHAR(bin), obuf, len);
return _T(obuf);
}
CString CRC5::Encrypt(CString str)
{
unsigned char key[16]={0x7, 0x0, 0x9, 0x1, 0x5, 0x70, 0x91, 0x5,
0x51, 0x90, 0x7, 0x5, 0x1, 0x9, 0x0, 0x7};
Setup(key);
int len=str.GetLength();
if(len<8)
len=len+8; //str.GetBuffer(len)
UPCHAR obuf;
obuf = UPCHAR(malloc(len+1));
memset(obuf, 0, len+1);
Encrypt(UPCHAR(str.GetBuffer(len)), obuf, len);
char * hex;
hex = PCHAR(malloc(2*len+1));
memset(hex, 0, 2*len+1);
BinToHex(PCHAR(obuf), hex, len);
return _T(hex);
}
void CRC5::BinToHex(LPSTR buf, LPSTR hex, int bufSize)
{
int i;
unsigned char c, j, k;
LPSTR ptrBuf=buf;
LPSTR ptrHex=hex;
for(i=0; i<bufSize; i++)
{
c=*ptrBuf;
j=c/16;
k=c-j*16;
*ptrHex=j>9 ? 'A'+j-10 : '0'+j;
ptrHex++;
*ptrHex=k>9 ? 'A'+k-10 : '0'+k;
ptrHex++;
ptrBuf++;
}
}
void CRC5::HexToBin(LPSTR hex, LPSTR buf, int bufSize)
{
int i;
unsigned char c, j, k;
LPSTR ptrBuf=buf;
LPSTR ptrHex=hex;
for(i=0; i<bufSize; i++)
{
j=*ptrHex;
j=((j>='A')&&(j<='F')) ? j-'A'+10 : j-'0';
ptrHex++;
k=*ptrHex;
k=((k>='A')&&(k<='F')) ? k-'A'+10 : k-'0';
c=j*16+k;
*ptrBuf=c;
ptrHex++;
ptrBuf++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -