> For the complete documentation index, see [llms.txt](https://everythingblackkk.gitbook.io/everythingblackkk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://everythingblackkk.gitbook.io/everythingblackkk/malware-development/editor-4.md).

# Encryption / Dec Files "AES"

## encryption /  decryption "XOR" Files&#x20;

By XOR Target File Byte with AES Key File , <mark style="color:red;">It is Not Recoomend</mark>&#x20;

```csharp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

#define AES_KEY_SIZE 32
#define BUFFER_SIZE 4096

void xor_encrypt_decrypt(const unsigned char* key, const char* input_file, const char* output_file) {
    FILE* in = fopen(input_file, "rb");
    FILE* out = fopen(output_file, "wb");

    if (!in || !out) {
        perror("Failed to open file");
        exit(EXIT_FAILURE);
    }

    unsigned char buffer[BUFFER_SIZE];
    size_t bytes_read;

    while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, in)) > 0) {
        for (size_t i = 0; i < bytes_read; i++) {
            buffer[i] ^= key[i % AES_KEY_SIZE]; // XOR with the key
        }
        fwrite(buffer, 1, bytes_read, out);
    }

    fclose(in);
    fclose(out);
    printf("Operation completed. Output saved to %s\n", output_file);
}

void load_aes_key(const char* filename, unsigned char* key) {
    FILE* file = fopen(filename, "rb");
    if (!file) {
        perror("Failed to open key file");
        exit(EXIT_FAILURE);
    }

    fread(key, 1, AES_KEY_SIZE, file);
    fclose(file);
}

int main() {
    char mode;
    char input_file[256], output_file[256];
    const char* key_file = "aes_key.bin";
    unsigned char key[AES_KEY_SIZE];

    load_aes_key(key_file, key);

    printf("Enter mode (e for encrypt, d for decrypt): ");
    scanf(" %c", &mode);
    printf("Enter input file name: ");
    scanf(" %255s", input_file);
    printf("Enter output file name: ");
    scanf(" %255s", output_file);

    if (mode == 'e') {
        xor_encrypt_decrypt(key, input_file, output_file);
    }
    else if (mode == 'd') {
        xor_encrypt_decrypt(key, input_file, output_file);
    }
    else {
        printf("Invalid mode. Use 'e' for encryption or 'd' for decryption.\n");
        return EXIT_FAILURE;
    }

    return 0;
}

```

## 2 - encryption /  decryption Files With AES Key&#x20;

<figure><img src="/files/0tQnztP6OQh3Iyr4Vdas" alt=""><figcaption></figcaption></figure>

This Code :&#x20;

* Gen AES Key Auto And Save It&#x20;
* Enc / Dec Large File&#x20;
* Add .crpt in File end&#x20;

```csharp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <wincrypt.h>
#pragma warning(disable:4996)

#define AES_BLOCK_SIZE 16
#define KEY_FILE "aes_key.bin"

// Function prototypes
void generateKey();
void encryptFile(const char* filename);
void decryptFile(const char* filename);

int main() {
    char mode;
    char filename[MAX_PATH];

    printf("Select mode: Encrypt (e) / Decrypt (d): ");
    scanf(" %c", &mode);

    printf("Enter the file name: ");
    scanf(" %s", filename);

    if (mode == 'e') {
        generateKey();
        encryptFile(filename);
    }
    else if (mode == 'd') {
        decryptFile(filename);
    }
    else {
        printf("Invalid mode. Use 'e' for encryption or 'd' for decryption.\n");
    }

    return 0;
}

void generateKey() {
    HCRYPTPROV hProv = 0;
    HCRYPTKEY hKey = 0;
    BYTE keyBlob[1024];
    DWORD blobLen = sizeof(keyBlob);

    if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
        if (CryptGenKey(hProv, CALG_AES_256, CRYPT_EXPORTABLE, &hKey)) {
            if (CryptExportKey(hKey, 0, PLAINTEXTKEYBLOB, 0, keyBlob, &blobLen)) {
                FILE* keyFile = fopen(KEY_FILE, "wb");
                if (keyFile) {
                    fwrite(keyBlob, 1, blobLen, keyFile);
                    fclose(keyFile);
                    printf("Key generated and saved to %s\n", KEY_FILE);
                }
                else {
                    printf("Failed to save the key.\n");
                }
            }
            CryptDestroyKey(hKey);
        }
        CryptReleaseContext(hProv, 0);
    }
    else {
        printf("Failed to acquire cryptographic context.\n");
    }
}

void encryptFile(const char* filename) {
    HCRYPTPROV hProv = 0;
    HCRYPTKEY hKey = 0;
    BYTE keyBlob[1024];
    DWORD blobLen;

    FILE* keyFile = fopen(KEY_FILE, "rb");
    if (!keyFile) {
        printf("Key file not found. Please generate a key first.\n");
        return;
    }
    blobLen = fread(keyBlob, 1, sizeof(keyBlob), keyFile);
    fclose(keyFile);

    if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
        if (CryptImportKey(hProv, keyBlob, blobLen, 0, 0, &hKey)) {
            FILE* inputFile = fopen(filename, "rb");
            if (!inputFile) {
                printf("Failed to open input file.\n");
                CryptDestroyKey(hKey);
                CryptReleaseContext(hProv, 0);
                return;
            }

            char outputFilename[MAX_PATH];
            snprintf(outputFilename, MAX_PATH, "%s.crpt", filename);

            FILE* outputFile = fopen(outputFilename, "wb");
            if (!outputFile) {
                printf("Failed to create output file.\n");
                fclose(inputFile);
                CryptDestroyKey(hKey);
                CryptReleaseContext(hProv, 0);
                return;
            }

            BYTE buffer[4096];
            DWORD bytesRead, bytesEncrypted;

            while ((bytesRead = fread(buffer, 1, sizeof(buffer), inputFile)) > 0) {
                BOOL finalBlock = (bytesRead < sizeof(buffer));
                if (!CryptEncrypt(hKey, 0, finalBlock, 0, buffer, &bytesRead, sizeof(buffer))) {
                    printf("Encryption failed.\n");
                    fclose(inputFile);
                    fclose(outputFile);
                    CryptDestroyKey(hKey);
                    CryptReleaseContext(hProv, 0);
                    return;
                }
                fwrite(buffer, 1, bytesRead, outputFile);
            }

            fclose(inputFile);
            fclose(outputFile);

            printf("File encrypted successfully: %s\n", outputFilename);
            CryptDestroyKey(hKey);
        }
        CryptReleaseContext(hProv, 0);
    }
}

void decryptFile(const char* filename) {
    HCRYPTPROV hProv = 0;
    HCRYPTKEY hKey = 0;
    BYTE keyBlob[1024];
    DWORD blobLen;

    FILE* keyFile = fopen(KEY_FILE, "rb");
    if (!keyFile) {
        printf("Key file not found.\n");
        return;
    }
    blobLen = fread(keyBlob, 1, sizeof(keyBlob), keyFile);
    fclose(keyFile);

    if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
        if (CryptImportKey(hProv, keyBlob, blobLen, 0, 0, &hKey)) {
            FILE* inputFile = fopen(filename, "rb");
            if (!inputFile) {
                printf("Failed to open input file.\n");
                CryptDestroyKey(hKey);
                CryptReleaseContext(hProv, 0);
                return;
            }

            char outputFilename[MAX_PATH];
            snprintf(outputFilename, MAX_PATH, "%.*s", (int)(strrchr(filename, '.') - filename), filename);

            FILE* outputFile = fopen(outputFilename, "wb");
            if (!outputFile) {
                printf("Failed to create output file.\n");
                fclose(inputFile);
                CryptDestroyKey(hKey);
                CryptReleaseContext(hProv, 0);
                return;
            }

            BYTE buffer[4096];
            DWORD bytesRead, bytesDecrypted;

            while ((bytesRead = fread(buffer, 1, sizeof(buffer), inputFile)) > 0) {
                if (!CryptDecrypt(hKey, 0, feof(inputFile), 0, buffer, &bytesRead)) {
                    printf("Decryption failed.\n");
                    fclose(inputFile);
                    fclose(outputFile);
                    CryptDestroyKey(hKey);
                    CryptReleaseContext(hProv, 0);
                    return;
                }
                fwrite(buffer, 1, bytesRead, outputFile);
            }

            fclose(inputFile);
            fclose(outputFile);

            printf("File decrypted successfully: %s\n", outputFilename);
            CryptDestroyKey(hKey);
        }
        CryptReleaseContext(hProv, 0);
    }
}



```

<figure><img src="/files/1uSKUL063fmZ0v1VjvqM" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/1LPQeBFhto7HUjUVkVUo" alt=""><figcaption></figcaption></figure>

## <mark style="color:yellow;">Alert  !!!!!</mark>

This Code To Explane C prog language , And How to use it to encreption files with C&#x20;

it's not to use a real word ransomware code .
