lundi 28 janvier 2013

CRC32 and MD5 hash c++


//CRC32
static DWORD crc32table[256];
static bool crc32Intalized;

DWORD crc32Hash(const void *data, DWORD size);


DWORD Crypt::crc32Hash(const void *data, DWORD size)
{
  if(crc32Intalized == false)
  {
    register DWORD crc;
    for(register DWORD i = 0; i < 256; i++)
    {
      crc = i;
      for(register DWORD j = 8; j > 0; j--)
      {
        if(crc & 0x1)crc = (crc >> 1) ^ 0xEDB88320L;
        else crc >>= 1;
      }
      crc32table[i] = crc;
    }

    crc32Intalized = true;
  }

  register DWORD cc = 0xFFFFFFFF;
  for(register DWORD i = 0; i < size; i++)cc = (cc >> 8) ^ crc32table[(((LPBYTE)data)[i] ^ cc) & 0xFF];
  return ~cc;
}

//MD5
#define MD5HASH_SIZE 16

  bool _md5Hash(LPBYTE output, void *inputData, DWORD dataSize);

bool Crypt::_md5Hash(LPBYTE output, void *inputData, DWORD dataSize)
{
  bool r = false;
  HCRYPTPROV hashProv;

  if(CWA(advapi32, CryptAcquireContextW)(&hashProv, NULL, NULL, PROV_RSA_FULL /*Èìåííî ýòî çíà÷åíèå, íå êàêîãî èíîãî.*/, CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != FALSE)
  {
    HCRYPTHASH hashHandle;
    if(CWA(advapi32, CryptCreateHash)(hashProv, CALG_MD5, 0, 0, &hashHandle) == TRUE)
    {
      DWORD hashLen = MD5HASH_SIZE;
      if(CWA(advapi32, CryptHashData)(hashHandle, (LPBYTE)inputData, dataSize, 0) == TRUE && CWA(advapi32, CryptGetHashParam)(hashHandle, HP_HASHVAL, output, &hashLen, 0) == TRUE && hashLen == MD5HASH_SIZE)r = true;
      CWA(advapi32, CryptDestroyHash)(hashHandle);
    }
    CWA(advapi32, CryptReleaseContext)(hashProv, 0);
  }

  return r;
}

Base64 for c++

  LPSTR _base64Encode(LPBYTE source, SIZE_T sourceSize, SIZE_T *destSize);
  LPBYTE _base64Decode(LPSTR source, SIZE_T sourceSize, SIZE_T *destSize);
LPSTR base64Encode(LPBYTE source, SIZE_T sourceSize, SIZE_T *destSize)
{
  static const char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  LPBYTE dest = (LPBYTE)Mem::alloc((sourceSize + 2) / 3 * 4 + 1);
  if(dest != NULL)
  {
    LPBYTE p = dest;
    BYTE cur[3];

    while(sourceSize > 0)
    {
      DWORD len = 0;
      for(DWORD i = 0; i < 3; i++)
      {
        if(sourceSize > 0)
        {
          sourceSize--;
          len++;
          cur[i] = source[i];
        }
        else cur[i] = 0;
      }

      source += 3;

      p[0] = cb64[cur[0] >> 2];
      p[1] = cb64[((cur[0] & 0x03) << 4) | ((cur[1] & 0xF0) >> 4)];
      p[2] = (BYTE)(len > 1 ? cb64[((cur[1] & 0x0F) << 2) | ((cur[2] & 0xC0) >> 6) ] : '=');
      p[3] = (BYTE)(len > 2 ? cb64[cur[2] & 0x3F] : '=');

      p += 4;
    }

    *p = 0;
    if(destSize)*destSize = (SIZE_T)(p - dest);
  }

  return (LPSTR)dest;
}
//////////////////////////
LPBYTE base64Decode(LPSTR source, SIZE_T sourceSize, SIZE_T *destSize)
{
  static const char cd64[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";

  LPBYTE dest = (LPBYTE)Mem::alloc(sourceSize + sizeof(BYTE));
  if(dest != NULL)
  {
    LPBYTE p = (LPBYTE)source;
    LPBYTE e = p + sourceSize;
    LPBYTE r = (LPBYTE)dest;

    BYTE in[4], out[3], v;
    int len, i;

    while(p < e)
    {
      for(len = 0, i = 0; i < 4 && p < e; i++)
      {
        v = 0;
        while(p < e && v == 0)
        {
          v = (BYTE)*(p++);
          v = (BYTE)((v < 43 || v > 122) ? 0 : cd64[v - 43]);
          if(v != 0)v = (BYTE)((v == '$') ? 0 : v - 61);
        }

        if(v != 0)
        {
          len++;
          in[i] = (BYTE)(v - 1);
        }
      }

      if(len)
      {
        out[0] = (BYTE)(in[0] << 2 | in[1] >> 4);
        out[1] = (BYTE)(in[1] << 4 | in[2] >> 2);
        out[2] = (BYTE)(((in[2] << 6) & 0xC0) | in[3]);
        for(i = 0; i < len - 1; i++){*(r++) = out[i]; if(i==0)i=0;/*instrict*/}
      }
    }

    *r = 0;
    if(destSize)*destSize = (SIZE_T)(r - dest);
  }
  return dest;
}

jeudi 10 janvier 2013

Call you function dynamically in c++

#include 

//Function for loading DLL
HMODULE Load_DLL(char* DLL){
 
 HANDLE Proc;
 HMODULE hDLL;
 hDLL = LoadLibraryA(DLL);
 if(hDLL == NULL){ 
  //Fail 
  FreeLibrary(hDLL);
  }
  else{
  //Sucess
  } 
  FreeLibrary(hDLL);
return hDLL ;
}
FARPROC Get_ProcA(char* DLL , char* FUNC){
 HMODULE hDLL = Load_DLL(DLL);
 FARPROC Myproc = GetProcAddress(hDLL,FUNC);
 if (Myproc == NULL)  FreeLibrary(hDLL);
 
 return Myproc;
}


//Define the function to load
typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);

int main()
{
  //Call you function
    msgbox Me = msgbox(Get_ProcA("User32", "MessageBoxA"));
   
    //TestIt
    Me(0,"TEST","Success",0);

 
return 0;
}

jeudi 3 janvier 2013

RC4 in C++

/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////// RC4 Encryption //////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////


LPBYTE RC4(LPBYTE lpBuf, LPBYTE lpKey, DWORD dwBufLen, DWORD dwKeyLen)
{
 int a, b = 0, s[256];
 BYTE swap;
 DWORD dwCount;
 for(a = 0; a < 256; a++)
 {
  s[a] = a;
 }
 for(a = 0; a < 256; a++)
 {
  b = (b + s[a] + lpKey[a % dwKeyLen]) % 256;
  swap = s[a];
  s[a] = s[b];
  s[b] = swap;
 }
 for(dwCount = 0; dwCount < dwBufLen; dwCount++)
 {
  a = (a + 1) % 256;
  b = (b + s[a]) % 256;
  swap = s[a];
  s[a] = s[b];
  s[b] = swap;
  lpBuf[dwCount] ^= s[(s[a] + s[b]) % 256];
 }
 return lpBuf;
}

mardi 25 décembre 2012

C++ MsgBoxA winApi

#include 

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev,  LPSTR lpCmd, int nShow)
{
    MessageBoxA(NULL, "Hello World!", "Ett0rhake", MB_OK);
}

Best way to read ressources data in C#

    public static byte[] Read(string typeRes, string nameRes)
    {
        IntPtr resH1 = FindResource(IntPtr.Zero, typeRes, nameRes);
        IntPtr resH2 = LoadResource(IntPtr.Zero, resH1);
        IntPtr resH3 = LockResource(resH2);
        uint resSize = SizeofResource(IntPtr.Zero, resH1);
        //copey resorce to byte array in our memory
        byte[] y = new byte[resSize];
        Marshal.Copy(resH3, y, 0, (int)resSize);
        //convert byte array to string
        //System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        //string settingtxt = encoding.GetString(y);

        return y;
    }

samedi 22 décembre 2012

Best way to write ressources data in C#

using System.Globalization;
using System.Runtime.InteropServices;
using System;
class ResManager
{
    #region WINAPI
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr BeginUpdateResource(string pFileName,
        [MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage,
        IntPtr lpData, uint cbData);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
    #endregion

    public enum ICResult
    {
        Success,
        FailBegin,
        FailUpdate,
        FailEnd
    }

    public static ICResult Write(string exeFilePath, byte[] Data, string NameRes, string TypeRes)
    {
        // Load executable
        IntPtr handleExe = BeginUpdateResource(exeFilePath, false);

        if (handleExe == null)
            return ICResult.FailBegin;

        // Get language identifier
        CultureInfo currentCulture = CultureInfo.CurrentCulture;
        int pid = ((ushort)currentCulture.LCID) & 0x3ff;
        int sid = ((ushort)currentCulture.LCID) >> 10;
        ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid));

        // Get pointer to data
        GCHandle iconHandle = GCHandle.Alloc(Data, GCHandleType.Pinned);

        // Replace Data
        if (UpdateResource(handleExe, NameRes, TypeRes, languageID, iconHandle.AddrOfPinnedObject(), (uint)Data.Length))
        {
            if (EndUpdateResource(handleExe, false))
                return ICResult.Success;
            else
                return ICResult.FailEnd;
        }
        else
            return ICResult.FailUpdate;
    }
}