0xtv

RC4 Class

Dec 1st, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #define SWAP(x,y) {x^=y;y^=x;x^=y;}
  2. /*
  3. *  AUTHOR : Karcrack
  4. *  DATE   : 100113
  5. *  PURPOSE: RC4+ C++ cipher/decipher.
  6. */
  7. template<unsigned int SIZE, class TYPE> struct myArr{
  8. private:
  9.     TYPE *data;
  10. public:
  11.     operator int(){
  12.         return (int)data;
  13.     }
  14.  
  15.     myArr& operator=(TYPE* rhs){
  16.         this->data = rhs;
  17.         return *this;
  18.     };
  19.  
  20.     TYPE& operator[](unsigned int idx){
  21.         return data[idx%SIZE];
  22.     };
  23. };
  24.  
  25. template <unsigned int KEY_SIZE>class RC4P{
  26. private:
  27.     unsigned int    i,
  28.                     j,
  29.                     size;
  30.  
  31.     myArr<256, char>     S;
  32.     myArr<KEY_SIZE, char>K;
  33. public:
  34.     RC4P(char S[], char K[], unsigned int size){
  35.         this->S     = S;
  36.         this->K     = K;
  37.         this->size  = size;
  38.     };
  39.     void calculate(char O[]){
  40.         //KSA
  41.         if(S==0 || K==0)
  42.             return;
  43.  
  44.         j = 0;
  45.         for(i = 0; i<256; i++)
  46.             S[i] = i;
  47.  
  48.         for(i = 0; i<256; i++){
  49.             j = j + S[i] + K[i];
  50.             SWAP(S[i], S[j])
  51.         };
  52.         //PRGA
  53.         unsigned int x = 0,
  54.                      a,
  55.                      b,
  56.                      c;
  57.         i = 0;
  58.         j = 0;
  59.         while(x<size){
  60.             i++;
  61.             a = S[i];
  62.             j+= a;
  63.             b = S[j];
  64.             S[i] = b;
  65.             S[j] = a;
  66.             c = S[i<<5 ^ j>>3] + S[j<<5 ^ i>>3];
  67.             O[x]^= (S[a+b] + S[c^0xAA]) ^ S[j+b];
  68.             x++;
  69.             i++;
  70.         };
  71.     };
  72. };
  73.  
  74.  
  75. Exemple of use :
  76.  
  77. #pragma comment(linker, "/ENTRY:main")
  78. #include <Windows.h>
  79. #include "rc4p.cpp"
  80.  
  81. void main(){
  82.     char* S = (char*)LocalAlloc(GPTR, 256*sizeof(char));
  83.     char K[]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  84.  
  85.     char crypt[]= "karcrack";
  86.  
  87.     RC4P<sizeof(K)> r(S, K, sizeof(crypt));
  88.  
  89.     r.calculate(crypt);
  90.     r.calculate(crypt);
  91.  
  92.     LocalFree(S);
  93. }
  94.  
  95. http://hackhound.org/forums/user/2003-karcrack/
Advertisement
Add Comment
Please, Sign In to add comment