윈도우에서 제공하는 간단한 방식의 암복호화 소스입니다.
#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;
}
'프로그래밍 팁 > etc' 카테고리의 다른 글
프로세스 이름(이미지 이름)으로 핸들값 얻기 (6) | 2010.03.29 |
---|---|
IP주소값 가져오기 (0) | 2010.03.29 |
일정 길이(픽셀단위) 이상의 문자열에 ...으로 나타내기 (0) | 2010.03.23 |
IP 차단하기(방화벽) (0) | 2010.01.31 |
압축 소스(ZIP, ZLIB) (4) | 2010.01.28 |