전에 RC4 스트림 암호화 알고리즘을 사용한 예제를 보고 카피한 간단한 프로그램 입니다.
vc++ 6.0 으로 만들었구요..
win32 용 콘솔 프로그램 입니다.
// DyCryt.cpp : Defines the entry point for the console application.
//
#define _WIN32_WINNT 0x0400
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#define KEYLENGTH 0x00800000
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8
void HandleError(char *szStr);
bool EncryptFile(char *szSource, char *szDestination, char *szPassword);
bool DecryptFile(char *szSource, char *szDestination, char *szPassword);
int main(int argc, char* argv[])
{
//printf("Hello World!\n");
char szOriginal[256];
char szCrypted[256];
char szRescued[256];
char szPassword[256];
int nOption = 0;
int nWorked = 0;
while(!nWorked)
{
printf("Choice EnCrypt or DeCrypt or Exit : 0 or 1 or 2\n");
scanf("%d",&nOption);
if(nOption == 0)
{
printf("Enter then Source file name: ");
scanf( "%s", &szOriginal);
printf("Enter the Target file name: ");
scanf("%s",&szCrypted);
printf("Enter the password:");
scanf("%s", &szPassword);
if(EncryptFile(szOriginal, szCrypted, szPassword))
{
printf("Success Encrypt file\n");
nWorked = 0;
}
}
else if(nOption == 1)
{
printf("Enter the Source file name: ");
scanf("%s",&szCrypted);
printf("Enter the Target file name: ");
scanf("%s",&szRescued);
printf("Enter the password:");
scanf("%s", &szPassword);
if(DecryptFile(szCrypted, szRescued, szPassword))
{
printf("Success Decrypt file\n");
nWorked = 0;
}
}
else if(nOption == 2)
{
nWorked = 1;
}
else
{
printf("Fail!\n");
nWorked = 1;
}
}
return 0;
}
bool EncryptFile(char *szSource, char *szDestination, char *szPassword)
{
FILE *hSource;
FILE *hDestination;
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
if(hSource = fopen(szSource, "rb"))
{
printf("The source plaintext file, %s, is open.\n", szSource);
}
else
{
HandleError("Error opening source plaintext file!");
}
if(hDestination = fopen(szDestination, "wb"))
{
printf("The destination file %s is open.\n", szDestination);
}
else
{
HandleError("Error opening destination ciphertext file!");
}
// Getting the handle of default CSP
if(CryptAcquireContext(
&hCryptProv,
NULL,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
0))
{
printf("A cryptographic provider has been acquired.\n");
}
else
{
HandleError("Error during CryptAcquireContext()");
}
// Create hash object
if(CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
printf("A hash object has been created.\n");
}
else
{
HandleError("Error durint CryptCreateHash()\n");
}
// Hashing password
if(CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0))
{
printf("The password has been added to the hash.\n");
}
else
{
HandleError("Error durint CryptHashData()\n");
}
if(CryptDeriveKey(
hCryptProv,
ENCRYPT_ALGORITHM,
hHash,
KEYLENGTH,
&hKey))
{
printf("An encryption key is derived from the password hash.\n");
}
else
{
HandleError("Error durint CryptDeriveKey()\n");
}
CryptDestroyHash(hHash);
hHash = NULL;
// Now, the session key is ready.
// Decide size of block
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("Memory has been allocated for the buffer.\n");
}
else
{
HandleError("Out of memory.\n");
}
do
{
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource))
{
HandleError("Error reading plaintext!\n");
}
if(!CryptEncrypt(
hKey,
0,
feof(hSource),
0,
pbBuffer,
&dwCount,
dwBufferLen))
{
HandleError("Error during CryptEncrypt.\n");
}
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination))
{
HandleError("Error writing chipertext.\n");
}
}while(!feof(hSource));
fclose(hSource);
fclose(hDestination);
free(pbBuffer);
CryptDestroyKey(hKey);
CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
return TRUE;
}
bool DecryptFile(
char *szSource,
char *szDestination,
char *szPassword)
{
FILE *hSource;
FILE *hDestination;
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
if(!(hSource = fopen(szSource, "rb")))
{
HandleError("Error opening ciphertext file!");
}
if(!(hDestination = fopen(szDestination, "wb")))
{
HandleError("Error opening plaintext file!");
}
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
HandleError("Error during CryptAcuireContext()");
}
if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
{
HandleError("Error during CryptCreateHash()");
}
if(!CryptHashData(hHash, (BYTE *)szPassword, strlen(szPassword), 0))
{
HandleError("Error during CryptHashData()");
}
if(!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
{
HandleError("Error during CryptDeriveKey()");
}
CryptDestroyHash(hHash);
hHash = NULL;
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
dwBufferLen = dwBlockLen;
if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
HandleError("Out of memory.\n");
}
do
{
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource))
{
HandleError("Error reading ciphertext file.\n");
}
if(!CryptDecrypt(hKey, 0, feof(hSource), 0, pbBuffer, &dwCount))
{
HandleError("Error during CryptDecrypt()\n");
}
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination))
{
HandleError("Error writing plaintext file.\n");
}
} while(!feof(hSource));
fclose(hSource);
fclose(hDestination);
free(pbBuffer);
CryptDestroyKey(hKey);
CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
return TRUE;
}
void HandleError(char *pStr)
{
fprintf(stderr, "An error occured in running the program.\n");
fprintf(stderr, pStr);
fprintf(stderr, "Error number %X\n", GetLastError());
fprintf(stderr, "Program terminating.\n");
//exit(1);
return;
}
'MIS(경영정보시스템)' 카테고리의 다른 글
암호화 (0) | 2007.09.22 |
---|---|
차세대 국가표준 암호화 알고리즘 (0) | 2007.09.22 |
라인델 알고리즘(Rijndeal Algorithm) (0) | 2007.09.22 |
보 안 (Security) (0) | 2007.09.22 |
인터넷의 기본 개념 (0) | 2007.09.20 |