← BackROYAL ANKIT AHIRAN
Skill 02 · C

SecureCrypt

A command-line C file encryption exercise that accepts a file path and password, transforms file bytes in binary mode, and locks the output file.

DEMO

What Was Built

A lightweight C command-line utility that prompts for a target file path and a password, opens the file in safe binary read/write buffers, applies a key-derived byte lock transform, and saves the locked output cleanly.

How It Looked

A clean terminal session prompting step by step for the file path and password, displaying a compact byte-locking progress bar before printing "File Encrypted Successfully" at completion.

What Was Learned

  • Working directly with C binary file pointers (`FILE*`), byte buffers, and clean memory cleanup.
  • Handling user input safely in CLI tools and checking file-open errors before processing.
  • Understanding the difference between learning exercises and production-audited cryptography tools.

Step-by-Step How to Run

  1. Compile the C source file cleanly using gcc with standard warnings enabled.
  2. Prepare a sample plain text file inside your local practice folder.
  3. Run the compiled binary, type the file path and password, and verify the confirmation message.
Compile & Run
gcc -Wall securecrypt.c -o securecrypt && ./securecrypt
Sample Code: securecrypt.c

Sample for learning, not my actual file

securecrypt.c
/* Sample for learning, not my actual file.
 * WARNING: For learning only — NOT for real security. */
#include <stdio.h>
#include <string.h>

static void strip_nl(char *s) {
    size_t n = strlen(s);
    if (n > 0 && s[n - 1] == '\n') s[n - 1] = '\0';
}

int main(void) {
    char path[256], out[264], pass[128];
    printf("WARNING: For learning only - NOT for real security.\n");
    printf("Enter file path : ");
    if (!fgets(path, sizeof(path), stdin)) return 1;
    strip_nl(path);
    printf("Enter password  : ");
    if (!fgets(pass, sizeof(pass), stdin)) return 1;
    strip_nl(pass);
    size_t klen = strlen(pass);
    if (!path[0] || klen == 0) { puts("Invalid input."); return 1; }
    snprintf(out, sizeof(out), "%s.enc", path);
    FILE *in_f = fopen(path, "rb");
    FILE *out_f = in_f ? fopen(out, "wb") : NULL;
    if (!in_f || !out_f) {
        if (in_f) fclose(in_f);
        puts("Could not open input or output file.");
        return 1;
    }
    int ch; size_t idx = 0;
    while ((ch = fgetc(in_f)) != EOF) {
        fputc((unsigned char)ch ^ (unsigned char)pass[idx % klen], out_f);
        idx++;
    }
    fclose(in_f);
    fclose(out_f);
    puts("File Encrypted Successfully");
    return 0;
}

Proof

Sample OutputCLI
$ ./securecrypt
Enter file path : notes.txt
Enter password  : **********
Locking bytes   : [##################] 100%
File Encrypted Successfully
Interface Capture Slot
✓ BINARY OUTPUT SAVED
CLI prompt confirming path lock completion with output file notes.txt.enc.
Terminal confirmation frame for SecureCrypt CLI practice run.