/* Rotations */
#define ROTL(x,y) (((x)<>(w-(y&(w-1)))))
#define ROTR(x,y) (((x)>>(y&(w-1))) | ((x)<
class rc6_base{
public:
rc6_base(){};
virtual ~rc6_base(){};
public:
unsigned int S[R24 - 1]; /* Key schedule */
public:
void rc6_key_setup(unsigned char *K, int b);
void rc6_block_encrypt(unsigned int *pt, unsigned int *ct);
void rc6_block_decrypt(unsigned int *ct, unsigned int *pt);
std::string rc6_encrypt(const char *in, unsigned char *out);
std::string rc6_decrypt(const char *in, unsigned char *out);
};
#endif
//rc6.cpp
/* rc6 (TM)
* Unoptimized sample implementation of Ron Rivest's submission to the
* AES bakeoff.
*
* Salvo Salasio, 19 June 1998
*
* Intellectual property notes: The name of the algorithm (RC6) is
* trademarked; any property rights to the algorithm or the trademark
* should be discussed with discussed with the authors of the defining
* paper "The RC6(TM) Block Cipher": Ronald L. Rivest (MIT),
* M.J.B. Robshaw (RSA Labs), R. Sidney (RSA Labs), and Y.L. Yin (RSA Labs),
* distributed 18 June 1998 and available from the lead author's web site.
*
* This sample implementation is placed in the public domain by the author,
* Salvo Salasio. The ROTL and ROTR definitions were cribbed from RSA Labs'
* RC5 reference implementation.
*/
#include "rc6.h"
inline unsigned char Byte(unsigned int ui)
{
return (unsigned char)(ui & 0xff);
}
inline void BytesToUint(unsigned char const* p, unsigned int& b)
{
unsigned int y;
b = 0;
y = *p++;
y <<= 24;
b |= y;
y = *p++;
y <<= 16;
b |= y;
y = *p++;
y <<= 8;
b |= y;
y = *p++;
b |= y;
}
inline void UintToBytes(unsigned int const b, unsigned char* p)
{
unsigned int y;
y = b;
*--p = Byte(y);
y = b >> 8;
*--p = Byte(y);
y = b >> 16;
*--p = Byte(y);
y = b >> 24;
*--p = Byte(y);
}
void rc6_base::rc6_key_setup(unsigned char *K, int b)
{
int i, j, s, v;
unsigned int L[(32 + bytes - 1) / bytes]; /* Big enough for max b */
unsigned int A, B;
L[c - 1] = 0;
for (i = b - 1; i >= 0; i--)
L[i / bytes] = (L[i / bytes] << 8) + K[i];
S[0] = P32;
for (i = 1; i <= 2 * r + 3; i++)
S[i] = S[i - 1] + Q32;
A = B = i = j = 0;
v = R24;
if (c > v) v = c;
v *= 3;
for (s = 1; s <= v; s++)
{
A = S[i] = ROTL(S[i] + A + B, 3);
B = L[j] = ROTL(L[j] + A + B, A + B);
i = (i + 1) % R24;
j = (j + 1) % c;
}
}
void rc6_base::rc6_block_encrypt(unsigned int *pt, unsigned int *ct)
{
unsigned int A, B, C, D, t, u, x;
int i, j;
A = pt[0];
B = pt[1];
C = pt[2];
D = pt[3];
B += S[0];
D += S[1];
for (i = 2; i <= 2 * r; i += 2)
{
t = ROTL(B * (2 * B + 1), lgw);
u = ROTL(D * (2 * D + 1), lgw);
A = ROTL(A ^ t, u) + S[i];
C = ROTL(C ^ u, t) + S[i + 1];
x = A;
A = B;
B = C;
C = D;
D = x;
}
A += S[2 * r + 2];
C += S[2 * r + 3];
ct[0] = A;
ct[1] = B;
ct[2] = C;
ct[3] = D;
}
void rc6_base::rc6_block_decrypt(unsigned int *ct, unsigned int *pt)
{
unsigned int A, B, C, D, t, u, x;
int i, j;
A = ct[0];
B = ct[1];
C = ct[2];
D = ct[3];
C -= S[2 * r + 3];
A -= S[2 * r + 2];
for (i = 2 * r; i >= 2; i -= 2)
{
x = D;
D = C;
C = B;
B = A;
A = x;
u = ROTL(D * (2 * D + 1), lgw);
t = ROTL(B * (2 * B + 1), lgw);
C = ROTR(C - S[i + 1], t) ^ u;
A = ROTR(A - S[i], u) ^ t;
}
D -= S[1];
B -= S[0];
pt[0] = A;
pt[1] = B;
pt[2] = C;
pt[3] = D;
}
std::string rc6_base::rc6_encrypt(const char *in, unsigned char *out)
{
unsigned int ct[4],pt[4];
unsigned char ch[4];
unsigned char *pzDest;
unsigned char *pzTemp;
int i_size=strlen(in);
int j=0;
if(i_size == 0)
{
return "";
}
if(i_size%16 != 0)
i_size=i_size+(16-i_size%16);
pzDest =(unsigned char*)malloc(sizeof(unsigned char*)*i_size+1);
memset(pzDest,0,i_size+1);
strcpy((char*)pzDest,in);
pzTemp = pzDest;
for(int i=0;i{
memset(ct,0,16);
memset(pt,0,16);
for(int n=0;n<4;n++,pzTemp+=4)
{
BytesToUint(pzTemp,pt[n]);
}
this->rc6_block_encrypt(pt,ct);
for(int n=0;n<4;n++)
{
UintToBytes(ct[n],ch+4);
for(int m=0;m<4;m++)
{
out[j]=ch[m];j++;
}
}
}
out[j]=0x00;
free(pzDest);
pzDest=NULL;
pzTemp=NULL;
return (std::string)(char*)out;
}
std::string rc6_base::rc6_decrypt(const char *in, unsigned char *out)
{
unsigned int ct[4],pt[4];
unsigned char ch[4];
unsigned char *pzDest;
unsigned char *pzTemp;
int i_size=strlen(in);
int j=0;
if(i_size == 0)
{
return "";
}
if(i_size%16 != 0)
i_size=i_size+(16-i_size%16);
pzDest =(unsigned char*)malloc(sizeof(unsigned char*)*i_size+1);
memset(pzDest,0,i_size+1);
strcpy((char*)pzDest,in);
pzTemp = pzDest;
for(int i=0;i{
memset(ct,0,16);
memset(pt,0,16);
for(int n=0;n<4;n++,pzTemp+=4)
{
BytesToUint(pzTemp,ct[n]);
}
this->rc6_block_decrypt(ct,pt);
for(int n=0;n<4;n++)
{
UintToBytes(pt[n],ch+4);
for(int m=0;m<4;m++)
{
out[j]=ch[m];j++;
}
}
}
out[j]=0x00;
free(pzDest);
pzDest=NULL;
pzTemp=NULL;
return (std::string)(char*)out;
}
/*
int main(int argc, char *argv[])
{
unsigned char out[512]={0};
unsigned char in[512]={0};
char k[]="12345678";
rc6_base rc6;
rc6.rc6_key_setup((unsigned char*)k,8);
string strtest="Statspack是一款功能强大的,免费的,oracle自带的性能分析工具需要用具有sysdba权限的用户登陆进行安装。";
clock_t ct1,ct2;
ct1 = clock();
for(int i=0;i<1000000;i++)
{
//cout
rc6.rc6_decrypt((char*)out,in);
}
ct2 = clock();
cout
return 0;
}
*/
发现加密解密字符串时会有0x00问题,需要想隔办法才行