vc + 네트워크 보안 프로 그래 밍 범례 (17) - open ssl 파일 암호 화 및 복호화 실현
우 리 는 VC + + 로 파일 암호 화 를 실현 할 것 입 니 다. 코드 구현 과 설명 을 보십시오.
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <wincrypt.h>
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define KEYLENGTH 0x00800000
void HandleError(char *s);
HCRYPTPROV GetCryptProv();
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8
BOOL EncryptFile(
PCHAR szSource,
PCHAR szDestination,
PCHAR szPassword);
HCRYPTKEY GenKeyByPassword(HCRYPTPROV hCryptProv,PCHAR szPassword);
HCRYPTKEY GenKeyByRandom(HCRYPTPROV hCryptProv,FILE* hDestination);
//-------------------------------------------------------------------
// Begin main.
void main(void)
{
PCHAR szSource;
PCHAR szDestination;
CHAR szPassword[100] = "";
char response;
if(!(szSource=(char *)malloc(100)))
HandleError("Memory allocation failed.");
if(!(szDestination=(char *)malloc(100)))
HandleError("Memory allocation failed.");
printf(" .
");
printf(" : ");
fgets(szSource, 100, stdin);
if(szSource[strlen(szSource)-1] == '
')
szSource[strlen(szSource)-1] = '\0';
printf(" : ");
fgets(szDestination, 100, stdin);
if(szDestination[strlen(szDestination)-1] == '
')
szDestination[strlen(szDestination)-1] = '\0';
printf(" ? ( y/n ) ");
response = getchar();
if(response == 'y')
{
printf(" :");
getchar();
gets(szPassword);
}
else
{
printf(" .
");
}
//-------------------------------------------------------------------
// EncryptFile .
if(EncryptFile(szSource, szDestination, szPassword))
{
printf(" %s !
",
szSource);
printf(" %s .
",szDestination);
}
else
{
HandleError(" !");
}
//-------------------------------------------------------------------
// .
if(szSource)
free(szSource);
if(szDestination)
free(szDestination);
} // end main
//-------------------------------------------------------------------
// : szSource , szDestination
// :
// szSource:
// szDestination:
// szPassword:
static BOOL EncryptFile(
PCHAR szSource,
PCHAR szDestination,
PCHAR szPassword)
{
//-------------------------------------------------------------------
// .
FILE *hSource;
FILE *hDestination;
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//-------------------------------------------------------------------
// .
if(hSource = fopen(szSource,"rb"))
{
printf(" %s .
", szSource);
}
else
{
HandleError(" !");
}
//-------------------------------------------------------------------
// .
if(hDestination = fopen(szDestination,"wb"))
{
printf(" %s .
", szDestination);
}
else
{
HandleError(" !");
}
//
hCryptProv = GetCryptProv();
//-------------------------------------------------------------------
// .
if(!szPassword || strcmp(szPassword,"")==0 )
{
//--------------------------------------------------------------
// , , .
hKey = GenKeyByRandom( hCryptProv, hDestination);
}
else
{
//--------------------------------------------------------------
// ,
hKey=GenKeyByPassword( hCryptProv, szPassword);
}
//--------------------------------------------------------------------
// ENCRYPT_BLOCK_SIZE ,
// ENCRYPT_BLOCK_SIZE 。
// 。
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
//--------------------------------------------------------------------
// . ,
if(ENCRYPT_BLOCK_SIZE > 1)
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
else
dwBufferLen = dwBlockLen;
//-------------------------------------------------------------------
// .
if(pbBuffer = (BYTE *)malloc(dwBufferLen))
{
printf(" .
");
}
else
{
HandleError(" .
");
}
//-------------------------------------------------------------------
//
do
{
//-------------------------------------------------------------------
// dwBlockLen .
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource))
{
HandleError(" !
");
}
//-------------------------------------------------------------------
// .
if(!CryptEncrypt(
hKey, //
0, // ,
feof(hSource), // , TRUE. FALSE.
// 。
0, //
pbBuffer, // ,
&dwCount, // ,
dwBufferLen)) //pbBuffer 。
{
HandleError("Error during CryptEncrypt.
");
}
//-------------------------------------------------------------------
//
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination))
{
HandleError(" .");
}
} while(!feof(hSource));
//-------------------------------------------------------------------
//
if(hSource)
{
if(fclose(hSource))
HandleError(" !");
}
if(hDestination)
{
if(fclose(hDestination))
HandleError(" !");
}
//-------------------------------------------------------------------
// .
if(pbBuffer)
free(pbBuffer);
//-------------------------------------------------------------------
//
if(hKey)
{
if(!(CryptDestroyKey(hKey)))
HandleError("Error during CryptDestroyKey");
}
//-------------------------------------------------------------------
// CSP
if(hCryptProv)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
HandleError("Error during CryptReleaseContext");
}
return(TRUE);
} // end Encryptfile
//
HCRYPTPROV GetCryptProv()
{
HCRYPTPROV hCryptProv; //
//
if(CryptAcquireContext(
&hCryptProv, //
NULL, // ,
MS_ENHANCED_PROV, //
PROV_RSA_FULL, // ,
0)) //
{
printf(" !
");
}
else
{
//
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
HandleError(" !");
}
}
return hCryptProv;
}
// HandleError: , ,
void HandleError(char *s)
{
printf(" !
");
printf("%s
",s);
printf(" : %x.
",GetLastError());
printf(" !
");
exit(1);
}
// GenKeyByRandom:
// :hCryptProv CSP
// hDestination ,
HCRYPTKEY GenKeyByRandom(HCRYPTPROV hCryptProv,FILE* hDestination)
{
HCRYPTKEY hKey;
HCRYPTKEY hXchgKey;
PBYTE pbKeyBlob;
DWORD dwKeyBlobLen;
if(CryptGenKey(
hCryptProv,
ENCRYPT_ALGORITHM,
KEYLENGTH | CRYPT_EXPORTABLE,
&hKey))
{
printf(" .
");
}
else
{
HandleError("Error during CryptGenKey.
");
}
//--------------------------------------------------------------
//
if(CryptGenKey(
hCryptProv,
AT_KEYEXCHANGE,
0,
&hXchgKey))
{
printf(" .
");
}
else
{
HandleError(" .
");
}
//--------------------------------------------------------------
// , .
if(CryptExportKey(
hKey,
hXchgKey,
SIMPLEBLOB,
0,
NULL,
&dwKeyBlobLen))
{
printf(" %d .
",dwKeyBlobLen);
}
else
{
HandleError(" !
");
}
if(pbKeyBlob =(BYTE *)malloc(dwKeyBlobLen))
{
printf(" .
");
}
else
{
HandleError(" .
");
}
//--------------------------------------------------------------
// .
if(CryptExportKey(
hKey,
hXchgKey,
SIMPLEBLOB,
0,
pbKeyBlob,
&dwKeyBlobLen))
{
printf(" .
");
}
else
{
HandleError("Error during CryptExportKey!
");
}
//--------------------------------------------------------------
// .
if(hXchgKey)
{
if(!(CryptDestroyKey(hXchgKey)))
HandleError("Error during CryptDestroyKey");
hXchgKey = 0;
}
//--------------------------------------------------------------
// .
fwrite(&dwKeyBlobLen, sizeof(DWORD), 1, hDestination);
if(ferror(hDestination))
{
HandleError(" .");
}
else
{
printf(" .
");
}
//--------------------------------------------------------------
// .
fwrite(pbKeyBlob, 1, dwKeyBlobLen, hDestination);
if(ferror(hDestination))
{
HandleError(" ");
}
else
{
printf(" .
");
}
// .
free(pbKeyBlob);
//
return hKey;
}
// GenKeyByRandom:
// :hCryptProv CSP
// szPassword
HCRYPTKEY GenKeyByPassword(HCRYPTPROV hCryptProv,PCHAR szPassword)
{
HCRYPTKEY hKey;
HCRYPTHASH hHash;
//-------------------------------------------------------------------
// .
if(CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
printf(" .
");
}
else
{
HandleError("Error during CryptCreateHash!
");
}
//-------------------------------------------------------------------
// .
if(CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0))
{
printf(" .
");
}
else
{
HandleError("Error during CryptHashData.
");
}
//-------------------------------------------------------------------
// .
if(CryptDeriveKey(
hCryptProv,
ENCRYPT_ALGORITHM,
hHash,
KEYLENGTH,
&hKey))
{
printf(" .
");
}
else
{
HandleError("Error during CryptDeriveKey!
");
}
//-------------------------------------------------------------------
// .
if(hHash)
{
if(!(CryptDestroyHash(hHash)))
HandleError("Error during CryptDestroyHash");
hHash = 0;
}
//
return hKey;
}
우 리 는 VC + + 로 파일 복호화 를 실현 할 것 입 니 다. 코드 구현 과 설명 을 보십시오.
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <wincrypt.h>
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define KEYLENGTH 0x00800000
void HandleError(char *s);
HCRYPTPROV GetCryptProv();
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8
BOOL DecryptFile(
PCHAR szSource,
PCHAR szDestination,
CHAR *szPassword);
HCRYPTKEY GenKeyByPassword(HCRYPTPROV hCryptProv,PCHAR szPassword);
HCRYPTKEY GenKeyFromFile(HCRYPTPROV hCryptProv,FILE* hSource);
void main(void)
{
//--------------------------------------------------------------------
// .
PCHAR szSource;
PCHAR szDestination;
CHAR szPassword[100] = "";
char response;
if(!(szSource=(char *)malloc(100)))
HandleError("Memory allocation failed.");
if(!(szDestination=(char *)malloc(100)))
HandleError("Memory allocation failed.");
printf(" .
");
printf(" : ");
fgets(szSource, 100, stdin);
if(szSource[strlen(szSource)-1] == '
')
szSource[strlen(szSource)-1] = '\0';
printf(" : ");
fgets(szDestination, 100, stdin);
if(szDestination[strlen(szDestination)-1] == '
')
szDestination[strlen(szDestination)-1] = '\0';
printf(" ? ( y/n ) ");
response = getchar();
if(response == 'y')
{
getchar();
printf(" :");
gets(szPassword);
}
else
{
printf(" .
");
}
//
if(!DecryptFile(szSource, szDestination, szPassword))
{
printf("
Error decrypting file.
");
}
else
{
printf("
%s .
", szSource);
printf(" %s .
",szDestination);
}
//--------------------------------------------------------------------
// .
if(szSource)
free(szSource);
if(szDestination)
free(szDestination);
} // End main
//-------------------------------------------------------------------
// : szSource , szDestination
// :
// szSource:
// szDestination:
// szPassword:
static BOOL DecryptFile(
PCHAR szSource,
PCHAR szDestination,
PCHAR szPassword)
{
//--------------------------------------------------------------------
// .
FILE *hSource;
FILE *hDestination;
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
BOOL status = FALSE;
//--------------------------------------------------------------------
// .
if(!(hSource = fopen(szSource,"rb")))
{
HandleError(" !");
}
//--------------------------------------------------------------------
// , .
if(!(hDestination = fopen(szDestination,"wb")))
{
HandleError(" !");
}
//
hCryptProv = GetCryptProv();
//
if(!szPassword|| strcmp(szPassword,"")==0 )
{
//--------------------------------------------------------------------
//
hKey = GenKeyFromFile( hCryptProv,hSource);
}
else
{
//--------------------------------------------------------------------
// .
hKey=GenKeyByPassword( hCryptProv, szPassword);
}
// , ENCRYPT_BLOCK_SIZE
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
dwBufferLen = dwBlockLen;
//--------------------------------------------------------------------
// .
if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
HandleError(" !
");
}
//--------------------------------------------------------------------
// ,
do {
//--------------------------------------------------------------------
// dwBlockLen .
dwCount = fread(
pbBuffer,
1,
dwBlockLen,
hSource);
if(ferror(hSource))
{
HandleError(" !");
}
//--------------------------------------------------------------------
//
if(!CryptDecrypt(
hKey,
0,
feof(hSource),
0,
pbBuffer,
&dwCount))
{
HandleError("Error during CryptDecrypt!");
}
//--------------------------------------------------------------------
// .
fwrite(
pbBuffer,
1,
dwCount,
hDestination);
if(ferror(hDestination))
{
HandleError("Error writing plaintext!");
}
} while(!feof(hSource));
status = TRUE;
//--------------------------------------------------------------------
//
if(hSource)
{
if(fclose(hSource))
HandleError(" ");
}
if(hDestination)
{
if(fclose(hDestination))
HandleError(" ");
}
//--------------------------------------------------------------------
//
if(pbBuffer)
free(pbBuffer);
//--------------------------------------------------------------------
//
if(hKey)
{
if(!(CryptDestroyKey(hKey)))
HandleError("Error during CryptDestroyKey");
}
//--------------------------------------------------------------------
// CSP
if(hCryptProv)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
HandleError("Error during CryptReleaseContext");
}
return status;
} // end Decryptfile
//
HCRYPTPROV GetCryptProv()
{
HCRYPTPROV hCryptProv; //
//
if(CryptAcquireContext(
&hCryptProv, //
NULL, // ,
MS_ENHANCED_PROV, //
PROV_RSA_FULL, // ,
0)) //
{
printf(" !
");
}
else
{
//
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
HandleError(" !");
}
}
return hCryptProv;
}
// HandleError: , ,
void HandleError(char *s)
{
printf(" !
");
printf("%s
",s);
printf(" : %x.
",GetLastError());
printf(" !
");
exit(1);
}
// GenKeyFromFile:
// :hCryptProv CSP
// hSource
HCRYPTKEY GenKeyFromFile(HCRYPTPROV hCryptProv,FILE* hSource)
{
HCRYPTKEY hKey;
PBYTE pbKeyBlob;
DWORD dwKeyBlobLen;
// , .
fread(&dwKeyBlobLen, sizeof(DWORD), 1, hSource);
if(ferror(hSource) || feof(hSource))
{
HandleError(" !");
}
if(!(pbKeyBlob = (BYTE *)malloc(dwKeyBlobLen)))
{
HandleError(" .");
}
//--------------------------------------------------------------------
//
fread(pbKeyBlob, 1, dwKeyBlobLen, hSource);
if(ferror(hSource) || feof(hSource))
{
HandleError(" !
");
}
//--------------------------------------------------------------------
// CSP.
if(!CryptImportKey(
hCryptProv,
pbKeyBlob,
dwKeyBlobLen,
0,
0,
&hKey))
{
HandleError("Error during CryptImportKey!");
}
if(pbKeyBlob)
free(pbKeyBlob);
//
return hKey;
}
// GenKeyByPassword:
// :hCryptProv CSP
// szPassword
HCRYPTKEY GenKeyByPassword(HCRYPTPROV hCryptProv,PCHAR szPassword)
{
HCRYPTKEY hKey;
HCRYPTHASH hHash;
//-------------------------------------------------------------------
// .
if(CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
printf(" .
");
}
else
{
HandleError("Error during CryptCreateHash!
");
}
//-------------------------------------------------------------------
// .
if(CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0))
{
printf(" .
");
}
else
{
HandleError("Error during CryptHashData.
");
}
//-------------------------------------------------------------------
// .
if(CryptDeriveKey(
hCryptProv,
ENCRYPT_ALGORITHM,
hHash,
KEYLENGTH,
&hKey))
{
printf(" .
");
}
else
{
HandleError("Error during CryptDeriveKey!
");
}
//-------------------------------------------------------------------
// .
if(hHash)
{
if(!(CryptDestroyHash(hHash)))
HandleError("Error during CryptDestroyHash");
hHash = 0;
}
//
return hKey;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux Shell 프로 그래 밍 - 텍스트 처리 grep, sed사용자 가 지정 한 '모드' 에 따라 대상 텍스트 를 일치 하 게 검사 하고 일치 하 는 줄 을 인쇄 합 니 다. ##포함 되 지 않 음, 역방향 일치 \ ##키워드 앞 뒤 가 맞지 않 고 키워드 만 일치 합 니 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.