Click here to Skip to main content
15,920,217 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: database probelm Pin
Jon Sagara18-Dec-03 7:41
Jon Sagara18-Dec-03 7:41 
GeneralRe: database probelm Pin
l a u r e n18-Dec-03 14:38
l a u r e n18-Dec-03 14:38 
GeneralRe: database probelm Pin
karteek18-Dec-03 20:31
karteek18-Dec-03 20:31 
GeneralFile handles Pin
Fabio Panzavolta18-Dec-03 6:50
Fabio Panzavolta18-Dec-03 6:50 
GeneralRe: File handles Pin
Alexander M.,18-Dec-03 8:02
Alexander M.,18-Dec-03 8:02 
GeneralCode Dependencies Pin
jmkhael18-Dec-03 6:46
jmkhael18-Dec-03 6:46 
GeneralRe: Code Dependencies Pin
John R. Shaw18-Dec-03 17:25
John R. Shaw18-Dec-03 17:25 
GeneralEncrypt and Decrypt data Pin
Anonymous18-Dec-03 6:46
Anonymous18-Dec-03 6:46 
Below are the code for encrypt and decrypt a text file by supplying a source file, destination file and password.

The problem is if I encrypt the file in Win2000 and decrypt it in WinXP the result is junk and vise versa. Win2000 to Win2000 or WinXP to WinXP work ok. Can anyone tell me what the problem is? Thanks alot.


Encrypt.c
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wincrypt.h>

#ifdef USE_BLOCK_CIPHER
// defines for RC2 block cipher
#define ENCRYPT_ALGORITHM CALG_RC2
#define ENCRYPT_BLOCK_SIZE 8
#else
// defines for RC4 stream cipher
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 1
#endif

static BOOL CAPIEncryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword);


/ **************************************************
***************************/
static BOOL CAPIEncryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword)
{
FILE *hSource = NULL;
FILE *hDestination = NULL;
INT eof = 0;

HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTKEY hXchgKey = 0;
HCRYPTHASH hHash = 0;

PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;

PBYTE pbBuffer = NULL;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;

BOOL status = FALSE;

// Open source file.
if((hSource = fopen(szSource,"rb")) == NULL) {
printf("Error opening Plaintext file!\n");
goto done;
}

// Open destination file.
if((hDestination = fopen(szDestination,"wb")) == NULL) {
printf("Error opening Ciphertext file!\n");
goto done;
}

// Get handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
printf("Error %x during CryptAcquireContext!\n", GetLastError());
goto done;
}

if(szPassword == NULL) {
// Encrypt the file with a random session key.

// Create a random session key.
if(!CryptGenKey(hProv, ENCRYPT_ALGORITHM, CRYPT_EXPORTABLE, &hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
goto done;
}

// Get handle to key exchange public key.
if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey)) {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
goto done;
}

// Determine size of the key blob and allocate memory.
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, NULL, &dwKeyBlobLen)) {
printf("Error %x computing blob length!\n", GetLastError());
goto done;
}
if((pbKeyBlob = malloc(dwKeyBlobLen)) == NULL) {
printf("Out of memory!\n");
goto done;
}

// Export session key into a simple key blob.
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, pbKeyBlob, &dwKeyBlobLen)) {
printf("Error %x during CryptExportKey!\n", GetLastError());
goto done;
}

// Release key exchange key handle.
CryptDestroyKey(hXchgKey);
hXchgKey = 0;

// Write size of key blob to destination file.
fwrite(&dwKeyBlobLen, sizeof(DWORD), 1, hDestination);
if(ferror(hDestination)) {
printf("Error writing header!\n");
goto done;
}

// Write key blob to destination file.
fwrite(pbKeyBlob, 1, dwKeyBlobLen, hDestination);
if(ferror(hDestination)) {
printf("Error writing header!\n");
goto done;
}

} else {
// Encrypt the file with a session key derived from a password.

// Create a hash object.
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
printf("Error %x during CryptCreateHash!\n", GetLastError());
goto done;
}

// Hash in the password data.
if(!CryptHashData(hHash, szPassword, strlen(szPassword), 0)) {
printf("Error %x during CryptHashData!\n", GetLastError());
goto done;
}

// Derive a session key from the hash object.
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey)) {
printf("Error %x during CryptDeriveKey!\n", GetLastError());
goto done;
}

// Destroy the hash object.
CryptDestroyHash(hHash);
hHash = 0;
}

// Determine number of bytes to encrypt at a time. This must be a multiple
// of ENCRYPT_BLOCK_SIZE.
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;

// Determine the block size. If a block cipher is used this must have
// room for an extra block.
if(ENCRYPT_BLOCK_SIZE > 1) {
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
} else {
dwBufferLen = dwBlockLen;
}

// Allocate memory.
if((pbBuffer = malloc(dwBufferLen)) == NULL) {
printf("Out of memory!\n");
goto done;
}

// Encrypt source file and write to Source file.
do {
// Read up to 'dwBlockLen' bytes from source file.
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource)) {
printf("Error reading Plaintext!\n");
goto done;
}
eof = feof(hSource);

// Encrypt data
if(!CryptEncrypt(hKey, 0, eof, 0, pbBuffer, &dwCount, dwBufferLen)) {
printf("bytes required:%d\n",dwCount);
printf("Error %x during CryptEncrypt!\n", GetLastError());
goto done;
}

// Write data to destination file.
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination)) {
printf("Error writing Ciphertext!\n");
goto done;
}
} while(!feof(hSource));

status = TRUE;

printf("OK\n");

done:

// Close files.
if(hSource) fclose(hSource);
if(hDestination) fclose(hDestination);

// Free memory.
if(pbKeyBlob) free(pbKeyBlob);
if(pbBuffer) free(pbBuffer);

// Destroy session key.
if(hKey) CryptDestroyKey(hKey);

// Release key exchange key handle.
if(hXchgKey) CryptDestroyKey(hXchgKey);

// Destroy hash object.
if(hHash) CryptDestroyHash(hHash);

// Release provider handle.
if(hProv) CryptReleaseContext(hProv, 0);

return(status);
}

/ **************************************************
***************************/
void init(void)
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
CHAR szUserName[100];
DWORD dwUserNameLen = 100;

// Attempt to acquire a handle to the default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) {
// Some sort of error occured.

// Create default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
printf("Error creating key container!\n");
exit(1);
}

// Get name of default key container.
if(!CryptGetProvParam(hProv, PP_CONTAINER, szUserName, &dwUserNameLen, 0)) {
// Error getting key container name.
szUserName[0] = 0;
}

printf("Create key container '%s'\n",szUserName);
}

// Attempt to get handle to signature key.
if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey)) {
if(GetLastError() == NTE_NO_KEY) {
// Create signature key pair.
printf("Create signature key pair\n");

if(!CryptGenKey(hProv,AT_SIGNATURE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}

// Attempt to get handle to exchange key.
if(!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey)) {
if(GetLastError()==NTE_NO_KEY) {
// Create key exchange key pair.
printf("Create key exchange key pair\n");

if(!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}

CryptReleaseContext(hProv,0);

// printf("OK\n");
}


/ **************************************************
***************************/
void _cdecl main(int argc, char *argv[])
{
PCHAR szSource = NULL;
PCHAR szDestination = NULL;
PCHAR szPassword = NULL;

init();

// Validate argument count.
if(argc != 3 && argc != 4) {
printf("USAGE: encrypt <dest file=""> [ <password> ]\n");
exit(1);
}

// Parse arguments.
szSource = argv[1];
szDestination = argv[2];
if(argc == 4) {
szPassword = argv[3];
}

if(!CAPIEncryptFile(szSource, szDestination, szPassword)) {
printf("Error encrypting file!\n");
exit(1);
}

exit(0);
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++






Decrypt.c
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wincrypt.h>

#ifdef USE_BLOCK_CIPHER
// defines for RC2 block cipher
#define ENCRYPT_ALGORITHM CALG_RC2
#define ENCRYPT_BLOCK_SIZE 8
#else
// defines for RC4 stream cipher
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 1
#endif

static BOOL CAPIDecryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword);


/ **************************************************
***************************/
static BOOL CAPIDecryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword)
{
FILE *hSource = NULL;
FILE *hDestination = NULL;
INT eof = 0;

HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTHASH hHash = 0;

PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;

PBYTE pbBuffer = NULL;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;

BOOL status = FALSE;

// Open source file.
if((hSource = fopen(szSource,"rb")) == NULL) {
printf("Error opening Ciphertext file!\n");
goto done;
}

// Open destination file.
if((hDestination = fopen(szDestination,"wb")) == NULL) {
printf("Error opening Plaintext file!\n");
goto done;
}

// Get handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
printf("Error %x during CryptAcquireContext!\n", GetLastError());
goto done;
}

if(szPassword == NULL) {
// Decrypt the file with the saved session key.

// Read key blob length from source file and allocate memory.
fread(&dwKeyBlobLen, sizeof(DWORD), 1, hSource);
if(ferror(hSource) || feof(hSource)) {
printf("Error reading file header!\n");
goto done;
}
if((pbKeyBlob = malloc(dwKeyBlobLen)) == NULL) {
printf("Out of memory or improperly formatted source file!\n");
goto done;
}

// Read key blob from source file.
fread(pbKeyBlob, 1, dwKeyBlobLen, hSource);
if(ferror(hSource) || feof(hSource)) {
printf("Error reading file header!\n");
goto done;
}

// Import key blob into CSP.
if(!CryptImportKey(hProv, pbKeyBlob, dwKeyBlobLen, 0, 0, &hKey)) {
printf("Error %x during CryptImportKey!\n", GetLastError());
goto done;
}
} else {
// Decrypt the file with a session key derived from a password.

// Create a hash object.
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
printf("Error %x during CryptCreateHash!\n", GetLastError());
goto done;
}

// Hash in the password data.
if(!CryptHashData(hHash, szPassword, strlen(szPassword), 0)) {
printf("Error %x during CryptHashData!\n", GetLastError());
goto done;
}

// Derive a session key from the hash object.
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey)) {
printf("Error %x during CryptDeriveKey!\n", GetLastError());
goto done;
}

// Destroy the hash object.
CryptDestroyHash(hHash);
hHash = 0;
}

// Determine number of bytes to decrypt at a time. This must be a multiple
// of ENCRYPT_BLOCK_SIZE.
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
dwBufferLen = dwBlockLen;

// Allocate memory.
if((pbBuffer = malloc(dwBufferLen)) == NULL) {
printf("Out of memory!\n");
goto done;
}

// Decrypt source file and write to destination file.
do {
// Read up to 'dwBlockLen' bytes from source file.
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource)) {
printf("Error reading Ciphertext!\n");
goto done;
}
eof = feof(hSource);

// Decrypt data
if(!CryptDecrypt(hKey, 0, eof, 0, pbBuffer, &dwCount)) {
printf("Error %x during CryptDecrypt!\n", GetLastError());
goto done;
}

// Write data to destination file.
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination)) {
printf("Error writing Plaintext!\n");
goto done;
}
} while(!feof(hSource));

status = TRUE;

printf("OK\n");

done:

// Close files.
if(hSource) fclose(hSource);
if(hDestination) fclose(hDestination);

// Free memory.
if(pbKeyBlob) free(pbKeyBlob);
if(pbBuffer) free(pbBuffer);

// Destroy session key.
if(hKey) CryptDestroyKey(hKey);

// Destroy hash object.
if(hHash) CryptDestroyHash(hHash);

// Release provider handle.
if(hProv) CryptReleaseContext(hProv, 0);

return(status);
}

/ **************************************************
***************************/
void init(void)
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
CHAR szUserName[100];
DWORD dwUserNameLen = 100;

// Attempt to acquire a handle to the default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) {
// Some sort of error occured.

// Create default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
printf("Error creating key container!\n");
exit(1);
}

// Get name of default key container.
if(!CryptGetProvParam(hProv, PP_CONTAINER, szUserName, &dwUserNameLen, 0)) {
// Error getting key container name.
szUserName[0] = 0;
}

printf("Create key container '%s'\n",szUserName);
}

// Attempt to get handle to signature key.
if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey)) {
if(GetLastError() == NTE_NO_KEY) {
// Create signature key pair.
printf("Create signature key pair\n");

if(!CryptGenKey(hProv,AT_SIGNATURE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}

// Attempt to get handle to exchange key.
if(!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey)) {
if(GetLastError()==NTE_NO_KEY) {
// Create key exchange key pair.
printf("Create key exchange key pair\n");

if(!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}

CryptReleaseContext(hProv,0);

// printf("OK\n");
}

/ **************************************************
***************************/
void _cdecl main(int argc, char *argv[])
{
PCHAR szSource = NULL;
PCHAR szDestination = NULL;
PCHAR szPassword = NULL;

init();

// Validate argument count.
if(argc != 3 && argc != 4) {
printf("USAGE: decrypt <dest file=""> [ <password> ]\n");
exit(1);
}

// Parse arguments.
szSource = argv[1];
szDestination = argv[2];
if(argc == 4) {
szPassword = argv[3];
}

if(!CAPIDecryptFile(szSource, szDestination, szPassword)) {
printf("Error encrypting file!\n");
exit(1);
}

exit(0);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
General&quot;Files are out of date&quot; Pin
GeraldoLuiz18-Dec-03 5:46
GeraldoLuiz18-Dec-03 5:46 
GeneralRe: &quot;Files are out of date&quot; Pin
Christian Graus18-Dec-03 12:27
protectorChristian Graus18-Dec-03 12:27 
GeneralRe: &quot;Files are out of date&quot; Pin
GeraldoLuiz18-Dec-03 12:51
GeraldoLuiz18-Dec-03 12:51 
GeneralRe: &quot;Files are out of date&quot; Pin
Monty218-Dec-03 19:31
Monty218-Dec-03 19:31 
Generalgettting IP address of the system Pin
Anonymous18-Dec-03 5:34
Anonymous18-Dec-03 5:34 
GeneralRe: gettting IP address of the system Pin
Anonymous18-Dec-03 5:36
Anonymous18-Dec-03 5:36 
GeneralRe: gettting IP address of the system Pin
David Crow18-Dec-03 6:15
David Crow18-Dec-03 6:15 
GeneralRe: gettting IP address of the system Pin
karteek18-Dec-03 6:47
karteek18-Dec-03 6:47 
GeneralRe: gettting IP address of the system Pin
David Crow18-Dec-03 7:46
David Crow18-Dec-03 7:46 
GeneralRe: gettting IP address of the system Pin
karteek18-Dec-03 19:20
karteek18-Dec-03 19:20 
GeneralRe: gettting IP address of the system Pin
David Crow19-Dec-03 2:55
David Crow19-Dec-03 2:55 
GeneralDeclaring an CInternetSession variable globally Pin
harinat18-Dec-03 5:18
harinat18-Dec-03 5:18 
GeneralShell Programming in C Pin
Lunu18-Dec-03 4:17
Lunu18-Dec-03 4:17 
GeneralRe: Shell Programming in C Pin
Jörgen Sigvardsson18-Dec-03 12:07
Jörgen Sigvardsson18-Dec-03 12:07 
GeneralRe: Shell Programming in C Pin
Jörgen Sigvardsson18-Dec-03 12:08
Jörgen Sigvardsson18-Dec-03 12:08 
QuestionWhat's a pseudo-template? Pin
Nish Nishant18-Dec-03 3:07
sitebuilderNish Nishant18-Dec-03 3:07 
AnswerRe: What's a pseudo-template? Pin
Ravi Bhavnani18-Dec-03 7:11
professionalRavi Bhavnani18-Dec-03 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.