This commit is contained in:
klein panic
2025-02-01 16:14:45 -05:00
commit 3aff0baacf
82 changed files with 1694 additions and 0 deletions

21
XOR_Encryption/README.md Normal file
View File

@@ -0,0 +1,21 @@
# XOR Cipher
## Description
This project implements a simple XOR cipher for encrypting and decrypting text using a key. XOR encryption is a symmetric operation, meaning the same function is used to both encrypt and decrypt data.
## Directory Structure
- `include/`: Contains the header files.
- `src/`: Contains the source code files (`main.c` and `xor_cipher.c`).
- `obj/`: Contains the compiled object files.
- `build/`: Contains the Makefile and the compiled binary.
## How to Build
1. Navigate to the `build` directory.
2. Run `make` to compile the project.
## How to Run
1. After building, you will find an executable named `xor_cipher` in the `build` directory.
2. Run it using `./xor_cipher`.
## How to Clean
- Run `make clean` in the `build` directory to remove all compiled files.

View File

@@ -0,0 +1,27 @@
# Variables
CC = gcc
CFLAGS = -I../include -Wall -Wextra
OBJDIR = ../obj
SRCDIR = ../src
BINDIR = ../build
# Source files
SOURCES = $(SRCDIR)/main.c $(SRCDIR)/xor_cipher.c
OBJECTS = $(OBJDIR)/main.o $(OBJDIR)/xor_cipher.o
# Build target
TARGET = xor_cipher
# Rules
all: $(BINDIR)/$(TARGET)
$(BINDIR)/$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) -o $(BINDIR)/$(TARGET)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJDIR)/*.o $(BINDIR)/$(TARGET)
.PHONY: all clean

BIN
XOR_Encryption/build/xor_cipher Executable file

Binary file not shown.

View File

@@ -0,0 +1,6 @@
#ifndef XOR_CIPHER_H
#define XOR_CIPHER_H
void xor_encrypt_decrypt(char *data, const char *key);
#endif // XOR_CIPHER_H

BIN
XOR_Encryption/obj/main.o Normal file

Binary file not shown.

Binary file not shown.

54
XOR_Encryption/src/main.c Normal file
View File

@@ -0,0 +1,54 @@
#include "xor_cipher.h"
#include <stdio.h>
#include <string.h>
// Function to display encrypted message in hexadecimal format
void print_hex(const char *data) {
for (int i = 0; data[i] != '\0'; i++) {
printf("%02x ", (unsigned char)data[i]);
}
printf("\n");
}
// Function to safely print decrypted message, replacing nulls with spaces
void print_decrypted_message(const char *data) {
for (int i = 0; data[i] != '\0'; i++) {
// If the character is NULL, print a space instead
putchar(data[i] == '\0' ? ' ' : data[i]);
}
printf("\n");
}
int main() {
char message[256];
char key[256];
// Get the message to encrypt
printf("Enter a message to encrypt: ");
fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = '\0'; // Remove newline character
// Get the encryption key
printf("Enter the encryption key: ");
fgets(key, sizeof(key), stdin);
key[strcspn(key, "\n")] = '\0'; // Remove newline character
// Check if message or key is empty
if (strlen(message) == 0 || strlen(key) == 0) {
printf("Error: Message or key cannot be empty.\n");
return 1;
}
// Encrypt the message
xor_encrypt_decrypt(message, key);
printf("Encrypted message (hexadecimal): ");
print_hex(message);
// Decrypt the message (using the same function, since XOR is symmetric)
xor_encrypt_decrypt(message, key);
printf("Decrypted message: ");
print_decrypted_message(message);
return 0;
}

View File

@@ -0,0 +1,10 @@
#include "xor_cipher.h"
#include <string.h>
#include <stdio.h>
void xor_encrypt_decrypt(char *data, const char *key) {
int keyLength = strlen(key);
for (int i = 0; data[i] != '\0'; i++) {
data[i] ^= key[i % keyLength];
}
}