/////////////////////////////////////////////////////////////////////////////////// // CCrypt class V1.2 : RC4, Base64 // Antoine Guilmard - 2006 // // Use RC4 : // - Call RC4Crypt // // Use Base64 : // - Call B64ByteEncode with the third parameter allocated with size returned // by B64ByteAfterEncode // - Call B64ByteDecode with the third parameter allocated with size returned // by B64ByteAfterDecode // /////////////////////////////////////////////////////////////////////////////////// #include "StdAfx.h" #include "Crypt.h" CCrypt::CCrypt(void) { } CCrypt::~CCrypt(void) { } /////////////////////////////////////////////////////////////////////////////////// // RC4 crypt // [in] ByteToCrypt : the bytes to crypt // [in] Key : the key for crypting // [out] CryptedByte : the result, must have the same size as ByteToCrypt /////////////////////////////////////////////////////////////////////////////////// BOOL CCrypt::RC4Crypt(BYTE* ByteToCrypt,DWORD nByteToCrypt,LPSTR Key,BYTE* CryptedByte) { size_t nLen; UCHAR *ucKey; UCHAR i,j,s; UCHAR State[256]; // Setup State for (int k=0;k<256;k++) State[k]=k; // Mix State nLen=strlen(Key); if (nLen==0) return FALSE; ucKey=reinterpret_cast(Key); j=0; for (size_t k=0; k< 256; k++) { j+= State[k] + ucKey[k%nLen]; s = State[k]; State[k] = State[j]; State[j] = s; } i=0;j=0; // Apply RC4 for(size_t k=0;k2) { for(i=0,j=0;i> 2 ]; EncodedByte[j+1]=Alphabet[ ((ByteToEncode[i]&0x03) << 4) | (ByteToEncode[i+1] >> 4 & 0x0f)]; EncodedByte[j+2]=Alphabet[ ((ByteToEncode[i+1]&0x0f) << 2) | (ByteToEncode[i+2] >> 6 & 0x03)]; EncodedByte[j+3]=Alphabet[ ByteToEncode[i+2] & 0x3f ]; j+=4; } } else i=0; switch(nByte-i) { case 1: EncodedByte[j]=Alphabet[ ByteToEncode[i] >> 2 ]; EncodedByte[j+1]=Alphabet[ (ByteToEncode[i]&0x03) << 4]; EncodedByte[j+2]='='; EncodedByte[j+3]='='; break; case 2: EncodedByte[j]=Alphabet[ ByteToEncode[i] >> 2 ]; EncodedByte[j+1]=Alphabet[ ((ByteToEncode[i]&0x03) << 4) | (ByteToEncode[i+1] >> 4 & 0x0f)]; EncodedByte[j+2]=Alphabet[(ByteToEncode[i+1]&0x0f) << 2]; EncodedByte[j+3]='='; break; } return; } /////////////////////////////////////////////////////////////////////////////////// // Base 64 decode // [in] ByteToDecode : the bytes to decode // [in] nByte : number of bytes to decode // [out] ByteDecoded : the result of decode, get the size with B64ByteAfterDecode /////////////////////////////////////////////////////////////////////////////////// void CCrypt::B64Decode(BYTE* ByteToDecode,DWORD nByte,BYTE* DecodedByte) { DWORD i,j; char InvAlphabet[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 61, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0 }; if (nByte==0) return; for(i=0,j=0;i>4); if (ByteToDecode[i+2]=='=') break; DecodedByte[j+1]=(InvAlphabet[ByteToDecode[i+1]] << 4)|(InvAlphabet[ByteToDecode[i+2]] >> 2); if (ByteToDecode[i+3]=='=') break; DecodedByte[j+2]=(InvAlphabet[ByteToDecode[i+2]]<<6)|(InvAlphabet[ByteToDecode[i+3]]); j+=3; } return; }