#define SWAP(x,y) {x^=y;y^=x;x^=y;}
/*
* AUTHOR : Karcrack
* DATE : 100113
* PURPOSE: RC4+ C++ cipher/decipher.
*/
template<unsigned int SIZE, class TYPE> struct myArr{
private:
TYPE *data;
public:
operator int(){
return (int)data;
}
myArr& operator=(TYPE* rhs){
this->data = rhs;
return *this;
};
TYPE& operator[](unsigned int idx){
return data[idx%SIZE];
};
};
template <unsigned int KEY_SIZE>class RC4P{
private:
unsigned int i,
j,
size;
myArr<256, char> S;
myArr<KEY_SIZE, char>K;
public:
RC4P(char S[], char K[], unsigned int size){
this->S = S;
this->K = K;
this->size = size;
};
void calculate(char O[]){
//KSA
if(S==0 || K==0)
return;
j = 0;
for(i = 0; i<256; i++)
S[i] = i;
for(i = 0; i<256; i++){
j = j + S[i] + K[i];
SWAP(S[i], S[j])
};
//PRGA
unsigned int x = 0,
a,
b,
c;
i = 0;
j = 0;
while(x<size){
i++;
a = S[i];
j+= a;
b = S[j];
S[i] = b;
S[j] = a;
c = S[i<<5 ^ j>>3] + S[j<<5 ^ i>>3];
O[x]^= (S[a+b] + S[c^0xAA]) ^ S[j+b];
x++;
i++;
};
};
};
Ejemplo de uso:
#pragma comment(linker, "/ENTRY:main")
#include <Windows.h>
#include "rc4p.cpp"
void main(){
char* S = (char*)LocalAlloc(GPTR, 256*sizeof(char));
char K[]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char crypt[]= "karcrack";
RC4P<sizeof(K)> r(S, K, sizeof(crypt));
r.calculate(crypt);
r.calculate(crypt);
LocalFree(S);
}