RC4+ clase

Iniciado por Karcrack, Junio 16, 2013, 06:32:18 PM

Tema anterior - Siguiente tema

0 Miembros y 2 Visitantes están viendo este tema.

Junio 16, 2013, 06:32:18 PM Ultima modificación: Febrero 08, 2014, 05:38:13 PM por Expermicid
Código: cpp
#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:
Código: cpp
#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);
}
I code for $$$.

(PGP ID 0xCC050E77)
ASM, C, C++, VB6... skilled [malware] developer