Quantum Locker - C Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define KEY_SIZE 32
#define HASH_FILE "quantum_key.hash"
#define MAX_PATH 256
/* Generate a pseudo-quantum random key */
void generate_key(unsigned char *key, int size) {
srand((unsigned)time(NULL));
for (int i = 0; i < size; i++) {
key[i] = rand() % 256; // 0–255 random byte
}
}
/* XOR-based encryption/decryption */
void xor_crypt(const char *input, const char *output, const unsigned char *key, int key_size) {
FILE *fin = fopen(input, "rb");
FILE *fout = fopen(output, "wb");
if (!fin || !fout) {
printf("Error: Cannot open file.\n");
if (fin) fclose(fin);
if (fout) fclose(fout);
return;
}
int c, i = 0;
while ((c = fgetc(fin)) != EOF) {
fputc(c ^ key[i % key_size], fout);
i++;
}
fclose(fin);
fclose(fout);
}
/* Simple hash generator for key validation (djb2 algorithm) */
unsigned long compute_hash(const unsigned char *key, int size) {
unsigned long hash = 5381;
for (int i = 0; i < size; i++)
hash = ((hash << 5) + hash) + key[i];
return hash;
}
/* Save key hash for future validation */
void save_hash(unsigned long hash) {
FILE *fp = fopen(HASH_FILE, "w");
if (!fp) {
printf("Error: cannot save hash.\n");
return;
}
fprintf(fp, "%lu", hash);
fclose(fp);
}
/* Verify if provided key is valid */
int verify_key(const unsigned char *key, int size) {
unsigned long stored_hash, input_hash;
FILE *fp = fopen(HASH_FILE, "r");
if (!fp) {
printf("No hash file found. Encrypt something first.\n");
return 0;
}
fscanf(fp, "%lu", &stored_hash);
fclose(fp);
input_hash = compute_hash(key, size);
return (stored_hash == input_hash);
}
/* Save generated key to file (for demonstration) */
void save_key_to_file(const unsigned char *key, int size) {
FILE *fp = fopen("key_save.txt", "wb");
if (!fp) return;
fwrite(key, 1, size, fp);
fclose(fp);
}
/* Display menu */
void show_menu() {
printf("\n==============================\n");
printf(" QUANTUM FILE LOCKER \n");
printf("==============================\n");
printf("1. Encrypt File\n");
printf("2. Decrypt File\n");
printf("3. Exit\n");
printf("==============================\n");
printf("Enter your choice: ");
}
/* Main program */
int main() {
unsigned char key[KEY_SIZE];
char infile[MAX_PATH], outfile[MAX_PATH];
int choice;
while (1) {
show_menu();
scanf("%d", &choice);
getchar();
if (choice == 3) {
printf("Exiting Quantum File Locker...\n");
break;
}
printf("Enter input file path: ");
fgets(infile, MAX_PATH, stdin);
infile[strcspn(infile, "\n")] = 0;
printf("Enter output file path: ");
fgets(outfile, MAX_PATH, stdin);
outfile[strcspn(outfile, "\n")] = 0;
if (choice == 1) {
generate_key(key, KEY_SIZE);
xor_crypt(infile, outfile, key, KEY_SIZE);
unsigned long hash = compute_hash(key, KEY_SIZE);
save_hash(hash);
save_key_to_file(key, KEY_SIZE);
printf("\nFile encrypted successfully!\n");
} else if (choice == 2) {
printf("Enter your 32-byte quantum key file path: ");
char keyfile[MAX_PATH];
fgets(keyfile, MAX_PATH, stdin);
keyfile[strcspn(keyfile, "\n")] = 0;
FILE *kf = fopen(keyfile, "rb");
if (!kf) {
printf("Error: Key file not found.\n");
continue;
}
fread(key, 1, KEY_SIZE, kf);
fclose(kf);
if (verify_key(key, KEY_SIZE)) {
xor_crypt(infile, outfile, key, KEY_SIZE);
printf("\n■ File decrypted successfully!\n");
} else {
printf("\nInvalid or mismatched quantum key!\n");
}
} else {
printf("Invalid choice. Try again.\n");
}
}
return 0;
}