Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

ABSTRACT

My Name is JUNAID MALIK, I have


three+ years Experiences in this
field. I work in these language PHP,
LARAVEL, PYTHON
(DJANGUE+FLASK)

JUNAID MALIK
0304-1659294

CS619-519
PROJECT+INTERNSHIP
AL-JUNAID TECH INSTITUTE
CS609 Assignment
#include <stdio.h>

#include <windows.h>

#include <wincrypt.h>

#include <stdbool.h>

#include <string.h>

#define AES_BLOCK_SIZE 16

bool encryptFile(char *sourceFile, char *destFile, char *password);

bool decryptFile(char *sourceFile, char *destFile, char *password);

void handleError(const char *message);

void printUsage();

int main(int argc, char *argv[]) {

if (argc < 3) {

printUsage();

return 1;

char *command = argv[1];

char *sourceFile = argv[2];

char destFile[MAX_PATH];

char password[256];

if (strcmp(command, "encrypt") == 0) {
AL-JUNAID TECH INSTITUTE
printf("Enter encryption password: ");

scanf("%s", password);

printf("Enter destination file path (or press 's' for same path as source): ");

scanf("%s", destFile);

if (strcmp(destFile, "s") == 0) {

strcpy(destFile, sourceFile);

if (encryptFile(sourceFile, destFile, password)) {

printf("File encrypted successfully.\n");

} else {

handleError("Encryption failed.");

} else if (strcmp(command, "decrypt") == 0) {

printf("Enter decryption password: ");

scanf("%s", password);

printf("Enter destination file path (or press 's' for same path as source): ");

scanf("%s", destFile);

if (strcmp(destFile, "s") == 0) {

strcpy(destFile, sourceFile);

if (decryptFile(sourceFile, destFile, password)) {

printf("File decrypted successfully.\n");

} else {

handleError("Decryption failed.");

}
AL-JUNAID TECH INSTITUTE
} else {

printUsage();

return 1;

return 0;

bool encryptFile(char *sourceFile, char *destFile, char *password) {

FILE *source = fopen(sourceFile, "rb");

if (!source) {

handleError("Unable to open source file for reading.");

return false;

FILE *dest = fopen(destFile, "wb");

if (!dest) {

fclose(source);

handleError("Unable to open destination file for writing.");

return false;

HCRYPTPROV hCryptProv;

HCRYPTKEY hKey;

if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {

fclose(source);

fclose(dest);

handleError("Cryptographic context acquisition failed.");


AL-JUNAID TECH INSTITUTE
return false;

if (!CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hKey)) {

fclose(source);

fclose(dest);

CryptReleaseContext(hCryptProv, 0);

handleError("Hash creation failed.");

return false;

// Set password as key

if (!CryptHashData(hKey, (BYTE *)password, strlen(password), 0)) {

fclose(source);

fclose(dest);

CryptDestroyHash(hKey);

CryptReleaseContext(hCryptProv, 0);

handleError("Key setup failed.");

return false;

BYTE buffer[1024];

DWORD bytesRead, bytesWritten;

BOOL success;

while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) {

success = CryptEncrypt(hKey, NULL, feof(source), 0, buffer, &bytesRead, sizeof(buffer));

if (!success) {

fclose(source);
AL-JUNAID TECH INSTITUTE
fclose(dest);

CryptDestroyHash(hKey);

CryptReleaseContext(hCryptProv, 0);

handleError("Encryption failed.");

return false;

bytesWritten = fwrite(buffer, 1, bytesRead, dest);

if (bytesWritten != bytesRead) {

fclose(source);

fclose(dest);

CryptDestroyHash(hKey);

CryptReleaseContext(hCryptProv, 0);

handleError("File write error.");

return false;

fclose(source);

fclose(dest);

CryptDestroyHash(hKey);

CryptReleaseContext(hCryptProv, 0);

return true;

bool decryptFile(char *sourceFile, char *destFile, char *password) {

// Similar to encryptFile, with CryptDecrypt instead of CryptEncrypt

// and handling decryption process instead of encryption.

}
AL-JUNAID TECH INSTITUTE
void handleError(const char *message) {

DWORD errorCode = GetLastError();

LPVOID lpMsgBuf;

FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,

NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0,


NULL);

fprintf(stderr, "%s Error %d: %s\n", message, errorCode, (LPCTSTR)lpMsgBuf);

LocalFree(lpMsgBuf);

void printUsage() {

printf("Usage: file-encryptor [encrypt/decrypt] [sourceFile]\n");

You might also like