Skip to content

Instantly share code, notes, and snippets.

@MareArts
Last active January 4, 2017 02:08
Show Gist options
  • Save MareArts/615bcc38ff7fa367e7880a6a6d92b731 to your computer and use it in GitHub Desktop.
Save MareArts/615bcc38ff7fa367e7880a6a6d92b731 to your computer and use it in GitHub Desktop.
MFC decode
http://study.marearts.com/2017/01/mfc-encode-decode-example-souce-code.html
bool Decode()
{
BYTE *bBuff;
DWORD dwFileLen;
CString csFile;
FILE *pFile;
// 암호화된 파일 저장하기
fopen_s(&pFile, "AutoLogIn.CPR", "rb");
//pFile = fopen(csFile, "rb");
if (!pFile)
{
::AfxMessageBox(_T("error"));
return false;
}
dwFileLen = _filelength(_fileno(pFile));
bBuff = new BYTE[dwFileLen];
// 파일 읽어서 버퍼에 저장
fread(bBuff, 1, dwFileLen, pFile);
fclose(pFile);
HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
//CString csPass=_T("rlawjdgus");
char csPass[100] = "rlawjdgus"; //encription key
//m_Pass.GetWindowText(csPass);
// CSP(Crystographic Service Provider) 핸들 얻기
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
::AfxMessageBox(_T("ecode fail"));
return false;
}
}
// 해쉬 만들기
CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
// 해쉬 값 계산
CryptHashData(hHash, (BYTE*)csPass, sizeof(csPass), 0);
// 키 만들기\tab
CryptDeriveKey(hProv, CALG_RC4, hHash, 0x0080 * 0x10000, &hKey);
// 복호화
CryptDecrypt(hKey, 0, TRUE, 0, bBuff, &dwFileLen);
// 해쉬 없애기
CryptDestroyHash(hHash);
// CSP 핸들 풀어주기
CryptReleaseContext(hProv, 0);
CString str;
str.Format(_T("%s"), CString(bBuff));
::AfxMessageBox(str);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment