int CEncrypt::encdec_rc5(unsigned char *data, unsigned int nLen, bool enc) { if ((0==data)||(!haveKey_rc5)) return -1; unsigned int offset = 0; if(nLen >= 8) { while (offset<=nLen-8) { RC5_32_INT d[2]; memcpy(d, data+offset, sizeof(d)); if (enc) RC5_32_encrypt(d, &key_rc5); else RC5_32_decrypt(d, &key_rc5); /* if (enc) RC5_32_encrypt((RC5_32_INT *)data+offset, key_rc5); else RC5_32_decrypt((RC5_32_INT *)data+offset, key_rc5); */ memcpy(data+offset, d, sizeof(d)); offset += sizeof(d); } } return nLen-offset; }
void RC5_32_ecb_encrypt(const unsigned char *in, unsigned char *out, RC5_32_KEY *ks, int encrypt) { unsigned long l,d[2]; c2l(in,l); d[0]=l; c2l(in,l); d[1]=l; if (encrypt) RC5_32_encrypt(d,ks); else RC5_32_decrypt(d,ks); l=d[0]; l2c(l,out); l=d[1]; l2c(l,out); l=d[0]=d[1]=0; }
SWORD Encrypt::encdec_rc5(BYTE *data,DWORD len,bool enc) { if(!data || !haveKey_rc5) { return -1; } DWORD offset = 0; while(offset <= len) { RC5_32_INT d[2]; memcpy(d,dataoffset,sizeof(d)); if(enc) { RC5_32_encrypt(d,&key_rc5); } else { RC5_32_decrypt(d,&key_rc5); } memcpy(dataoffset,d,sizeof(d)); offset += sizeof(d); } return len - offset; }
void RC5_32_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, RC5_32_KEY *ks, unsigned char *iv, int encrypt) { unsigned long tin0,tin1; unsigned long tout0,tout1,xor0,xor1; long l=length; unsigned long tin[2]; if (encrypt) { c2l(iv,tout0); c2l(iv,tout1); iv-=8; for (l-=8; l>=0; l-=8) { c2l(in,tin0); c2l(in,tin1); tin0^=tout0; tin1^=tout1; tin[0]=tin0; tin[1]=tin1; RC5_32_encrypt(tin,ks); tout0=tin[0]; l2c(tout0,out); tout1=tin[1]; l2c(tout1,out); } if (l != -8) { c2ln(in,tin0,tin1,l+8); tin0^=tout0; tin1^=tout1; tin[0]=tin0; tin[1]=tin1; RC5_32_encrypt(tin,ks); tout0=tin[0]; l2c(tout0,out); tout1=tin[1]; l2c(tout1,out); } l2c(tout0,iv); l2c(tout1,iv); } else { c2l(iv,xor0); c2l(iv,xor1); iv-=8; for (l-=8; l>=0; l-=8) { c2l(in,tin0); tin[0]=tin0; c2l(in,tin1); tin[1]=tin1; RC5_32_decrypt(tin,ks); tout0=tin[0]^xor0; tout1=tin[1]^xor1; l2c(tout0,out); l2c(tout1,out); xor0=tin0; xor1=tin1; } if (l != -8) { c2l(in,tin0); tin[0]=tin0; c2l(in,tin1); tin[1]=tin1; RC5_32_decrypt(tin,ks); tout0=tin[0]^xor0; tout1=tin[1]^xor1; l2cn(tout0,tout1,out,l+8); xor0=tin0; xor1=tin1; } l2c(xor0,iv); l2c(xor1,iv); } tin0=tin1=tout0=tout1=xor0=xor1=0; tin[0]=tin[1]=0; }