|
/*
Sample (5 minute solution) to give a rough idea of what exactly I have on my mind. I particularly want to avoid encrypting data to HDD and running it from there (never mind that the user might dump the image from memory etc etc)
Thanks to Nir Dremer's FileEnc code
*/
#include "stdafx.h"
#include "BlowFish.h"
#include "HashLibProper.h"
#include <stdio.h>
#pragma comment(lib,"HashLibProper.lib")//Explicit linking call to our library(you don't need to put the library in your project after this)
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char szInputFile[MAX_PATH+1],szOutputFile[MAX_PATH+1],szKey[1024];
GetCurrentDirectory(MAX_PATH,szInputFile);
strcpy(szOutputFile,szInputFile);
////////////////////Starting usage of HashLibProper routine/////////////
llCD llCd;
char temp1[256],temp2[256],szMsg[1024];
llCd.GetMeDrives(3);//GetMeDrives of type DRIVE_FIXED (HDD)
llCd.validate();
llCd.CheckMachine(temp1,temp2);
wsprintf(szMsg,"Your current machine configuration yeilded the string %s\nWith MD5 Digest %s",temp1,temp2);
MessageBox(NULL,szMsg,"Information..",MB_OK|MB_ICONINFORMATION);//Shows the machine data and the MD5 digest
/////////////////////End of HashLibProper routine/////////////
strcpy(szKey,temp2);//We copy the MD5 digest the key here
//MessageBox(NULL,szInputFile,"Debug",MB_OK);
int bEnc=MessageBox(NULL,"Press OK to 'Encrypt' or 'Cancel' to decrypt ","Debug",MB_OKCANCEL);
if(bEnc==IDOK)
{
strcat(szInputFile,"\\OriginalData.dat");
strcat(szOutputFile,"\\EncryptedData.dat");
}
else
{
strcat(szInputFile,"\\EncryptedData.dat");
strcat(szOutputFile,"\\DecryptedData.dat");
}
FILE *readFile = fopen(szInputFile, "rb");
if (readFile == 0) {MessageBox(NULL,"Unable to open source file.","Debug",MB_OK);return -1;}
const size_t bufferSize = 1024;
size_t readRet = 0;
bool abort = false;
int encRet;
char readBuffer[bufferSize];
char outBuffer[bufferSize];
FILE *writeFile = fopen(szOutputFile, "wb");
if (writeFile == 0) {MessageBox(NULL,"Unable to open destination file.","Debug",MB_OK);return -1;}
BlowFishEnc encryption(szKey);
while (!feof(readFile))
{
readRet = fread(readBuffer, sizeof(char), bufferSize, readFile);
//decrypt
if(bEnc!=IDOK)
{
encRet = encryption.decryptStream(readBuffer, (DWORD)readRet, outBuffer);
if feof(readFile)
{
int pos = 0;
// removing trailing zeros - encrypted file must be x8 bytes.
while ((pos < 8) && ((outBuffer + encRet - pos)[0] == 0)) pos++;
// if found trailing zeros - decreasing the writing buffer marker (not writing them).
if (pos) encRet -= (pos - 1);
}
}else
{
//encrypt
encRet = encryption.encryptStream(readBuffer, (DWORD)readRet, outBuffer);
}
fwrite(outBuffer, sizeof(char), encRet, writeFile);
}
fflush(writeFile);
fclose(writeFile);
fclose(readFile);
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.