윈도우에서 제공하는 간단한 방식의 암복호화 소스입니다.

#include <WinCrypt.h>
int Encrypt(LPCTSTR lpszIn, LPTSTR lpszOut, int nBufLen)
{
 int result = 0;

 HCRYPTPROV hProv;
    HCRYPTHASH hHash;
    HCRYPTKEY hKey; 
    CString  csPass = "PASSWORD";
 DWORD  dwLen = strlen(lpszIn);

    if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
    {
        if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
        {
            return -1;
        }
    }
 strcpy(lpszOut, lpszIn);
 
    CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
    CryptHashData(hHash, (BYTE*)(LPCTSTR)csPass, csPass.GetLength(), 0);
    CryptDeriveKey(hProv, CALG_RC4, hHash, 0x0080*0x10000, &hKey);
    CryptEncrypt(hKey, 0, TRUE, 0, (BYTE*)lpszOut, &dwLen, nBufLen);
    //CryptDecrypt(hKey, 0, TRUE, 0, (BYTE*)lpszOut, &dwLen);
    CryptDestroyHash(hHash);
    CryptReleaseContext(hProv, 0);
 return result;
}

int Decrypt(LPCTSTR lpszIn, LPTSTR lpszOut)
{
 int result = 0;
 
 HCRYPTPROV hProv;
    HCRYPTHASH hHash;
    HCRYPTKEY hKey; 
    CString  csPass = "PASSWORD";
 DWORD  dwLen = strlen(lpszIn);
 
    if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
    {
        if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
        {
            return -1;
        }
    }
 strcpy(lpszOut, lpszIn);
 
    CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
    CryptHashData(hHash, (BYTE*)(LPCTSTR)csPass, csPass.GetLength(), 0);
    CryptDeriveKey(hProv, CALG_RC4, hHash, 0x0080*0x10000, &hKey);
    CryptDecrypt(hKey, 0, TRUE, 0, (BYTE*)lpszOut, &dwLen);
    CryptDestroyHash(hHash);
    CryptReleaseContext(hProv, 0);
 
 return result;
}

Posted by 띠깜
,