?? 文件保險箱dlg.cpp
字號:
#ifdef _DEBUG
AfxMessageBox(strPass);
#endif
if(Decrypt_File(strCipherFilePath,strPlainFilePath,strPass))
{
AfxMessageBox("解密文件成功");
}
else
{
AfxMessageBox("解密文件失敗");
}
}
/**********************************************************************
函數(shù)名稱:Encrypt_File
函數(shù)功能:加密文件
處理過程:
1.根據(jù)選擇的密碼算法以及口令,生成用于加密數(shù)據(jù)的會話密鑰
2.把密碼算法作為文件頭寫到密文。
3.循環(huán)讀取原文文件數(shù)據(jù)加密后保存到密文文件路徑中。
參數(shù)說明:
strPstrPlainFilePath:[IN] CString,待加密的原文文件路徑
strCipherFilePath:[IN] CString,加密后的密文文件保存路徑
nAlg_ID:[IN] int 密碼算法ID
strPass:[IN] CString 口令
返回值:成功返回TRUE,否則返回FALSE
************************************************************************/
BOOL CMyDlg::Encrypt_File(CString strPlainFilePath, CString strCipherFilePath, DWORD nAlg_ID, CString strPass)
{
FILE *hSource; //保存打開明文文件的句柄
FILE *hDestination; //保存打開密文文件的句柄
HCRYPTPROV hCryptProv; //CSP句柄
HCRYPTKEY hKey; //加密文件的會話密鑰句柄
HCRYPTHASH hHash; //根據(jù)口令派生會話密鑰的哈希對象
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
char *szSource= strPlainFilePath.GetBuffer(0);
char *szDestination= strCipherFilePath.GetBuffer(0);
char *szPassword = strPass.GetBuffer(0);
// 打開明文文件
if(!(hSource = fopen(szSource,"rb")))
{
return FALSE;
}
// 打開密文文件
if(!(hDestination = fopen(szDestination,"wb")))
{
return FALSE;
}
//打開 MS_ENHANCED_PROV CSP
if(!CryptAcquireContext(
&hCryptProv,
NULL,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
0))
{
fclose(hDestination);
fclose(hSource);
return FALSE;
}
//使用口令派生出會話密鑰來加密文件
//創(chuàng)建摘要句柄
if(!CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
fclose(hDestination);
fclose(hSource);
return FALSE;
}
// 對口令進行摘要運算
if(!CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0))
{
fclose(hDestination);
fclose(hSource);
return FALSE;
}
//從哈希對象中派生出會話密鑰
if(!CryptDeriveKey(
hCryptProv,
nAlg_ID,
hHash,
CRYPT_EXPORTABLE,
&hKey))
{
DWORD dwErr=GetLastError();
fclose(hDestination);
fclose(hSource);
CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
//銷毀哈希對象
CryptDestroyHash(hHash);
hHash = 0;
DWORD dwEncBlockSize = 0;
DWORD dwTmp;
//獲得會話密鑰參數(shù)
if(!CryptGetKeyParam(hKey,KP_BLOCKLEN,(BYTE *)&dwEncBlockSize,&dwTmp,0))
{
CryptDestroyKey(hKey);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
//現(xiàn)在加密文件的會話密鑰已經準備好了。
dwBlockLen = 1000 - 1000 % dwEncBlockSize;
if(dwEncBlockSize > 1)
dwBufferLen = dwBlockLen + dwEncBlockSize;
else
dwBufferLen = dwBlockLen;
if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
fclose(hDestination);
fclose(hSource);
CryptDestroyHash(hHash);
CryptDestroyKey(hKey);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
char enchead[128]={0};
sprintf(enchead,"ALGID:%d\n",nAlg_ID);
fwrite(enchead,1,128,hDestination);
//不斷循環(huán)加密原文件,把密文寫入的密文文件
do
{
//讀取原文dwBlockLen字節(jié)
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource))
{
fclose(hDestination);
fclose(hSource);
CryptDestroyHash(hHash);
CryptDestroyKey(hKey);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
//加密數(shù)據(jù)
if(!CryptEncrypt(
hKey,
0,
feof(hSource),
0,
pbBuffer,
&dwCount,
dwBufferLen))
{
fclose(hDestination);
fclose(hSource);
CryptDestroyHash(hHash);
CryptDestroyKey(hKey);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
//把密文寫入的密文文件
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination))
{
fclose(hDestination);
fclose(hSource);
CryptDestroyHash(hHash);
CryptDestroyKey(hKey);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
} while(!feof(hSource));
//加密完成,關閉文件句柄、釋放內存、銷毀會話密鑰等
if(hSource)
fclose(hSource);
if(hDestination)
fclose(hDestination);
if(pbBuffer)
free(pbBuffer);
if(hKey)
CryptDestroyKey(hKey);
if(hHash)
CryptDestroyHash(hHash);
if(hCryptProv)
CryptReleaseContext(hCryptProv, 0);
return TRUE;
}
/**********************************************************************
函數(shù)名稱:Decrypt_File
函數(shù)功能:對加密文件解密
處理過程:
1.根據(jù)算法和口令派生出解密數(shù)據(jù)的會話密鑰
2.讀取密文文件頭,獲取加密算法。
2.循環(huán)讀取原文文件數(shù)據(jù)解密,并保存在原文文件。
參數(shù)說明:
strCipherFilePath:[IN] CString,密文文件路徑
strPstrPlainFilePath:[IN] CString,解密后的原文文件保存路徑。
strPass:[IN] CString 口令
返回值:成功返回TRUE,否則返回FALSE
************************************************************************/
BOOL CMyDlg::Decrypt_File(CString strCipherFilePath, CString strPlainFilePath, CString strPass)
{
FILE *hSource; //保存打開密文文件的句柄
FILE *hDestination; //保存打開明文文件的句柄
HCRYPTPROV hCryptProv; //CSP句柄
HCRYPTKEY hKey; //解密文件的會話密鑰句柄
HCRYPTHASH hHash; //根據(jù)口令派生會話密鑰的哈希對象
DWORD nAlg_ID;
PBYTE pbKeyBlob = NULL;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
BOOL status = FALSE;
char *szSource=strCipherFilePath.GetBuffer(0);
char *szDestination=strPlainFilePath.GetBuffer(0);
char *szPassword=strPass.GetBuffer(0);
// 打開密文文件
if(!(hSource = fopen(szSource,"rb")))
{
return status;
}
//打開目標文件即解密后的明文文件
if(!(hDestination = fopen(szDestination,"wb")))
{
return status;
}
//從密文文件頭讀取加密算法
char enchead[128]={0};
fread(enchead,1,128,hSource);
sscanf(enchead,"ALGID:%d\n",&nAlg_ID);
// 獲得CSP句柄
if(!CryptAcquireContext(
&hCryptProv,
NULL,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
0))
{
return status;
}
//利用口令派生出的會話密鑰解密文件
// 創(chuàng)建哈希對象
if(!CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
return status;;
}
// 哈希口令
if(!CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0))
{
return status;;
}
// 從哈希對象中派生出會話密鑰
if(!CryptDeriveKey(
hCryptProv,
nAlg_ID,
hHash,
CRYPT_EXPORTABLE,
&hKey))
{
return status;
}
// 銷毀哈希對象
CryptDestroyHash(hHash);
hHash = 0;
//現(xiàn)在已經獲得了解密數(shù)據(jù)的會話密鑰。
DWORD dwEncBlockSize = 0;
DWORD dwTmp;
CryptGetKeyParam(hKey,KP_BLOCKLEN,(BYTE *)&dwEncBlockSize,&dwTmp,0);
dwBlockLen = 1000 - 1000 % dwEncBlockSize;
dwBufferLen = dwBlockLen;
if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
return status;
}
// 解密密文,并把明文寫到明文文件中。
do {
// 循環(huán)讀取密文
dwCount = fread(
pbBuffer,
1,
dwBlockLen,
hSource);
if(ferror(hSource))
{
return status;
}
// 數(shù)據(jù)解密
if(!CryptDecrypt(
hKey,
0,
feof(hSource),
0,
pbBuffer,
&dwCount))
{
return status;
}
// 寫明文數(shù)據(jù)到文件
fwrite(
pbBuffer,
1,
dwCount,
hDestination);
if(ferror(hDestination))
{
return status;
}
}
while(!feof(hSource));
status = TRUE;
// 關閉文件
if(hSource)
fclose(hSource);
if(hDestination)
fclose(hDestination);
// 解密完成,釋放內存、關閉文件句柄、銷毀會話密鑰、CSP句柄等。
if(pbKeyBlob)
free(pbKeyBlob);
if(pbBuffer)
free(pbBuffer);
if(hKey)
CryptDestroyKey(hKey);
if(hHash)
CryptDestroyHash(hHash);
if(hCryptProv)
CryptReleaseContext(hCryptProv, 0);
return status;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -