윈도우에서 제공하는 간단한 방식의 암복호화 소스
#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;
}
'Programming > Security' 카테고리의 다른 글
버퍼 오버플로우 (0) | 2011.12.04 |
---|---|
웹메소드 중 Get방식과 Post방식 (0) | 2011.12.04 |
FTP의 Active Mode 와 Passive Mode (0) | 2011.12.04 |
DES (0) | 2011.11.29 |
오픈소스 API (0) | 2011.11.26 |
error C2731: 'WinMain' : 함수를 오버로드할 수 없습니다
'Programming > MFC' 카테고리의 다른 글
MFC 헤더 및 라이브러리파일 설명 (0) | 2011.11.26 |
---|