testing
This commit is contained in:
21
XOR_Encryption/README.md
Normal file
21
XOR_Encryption/README.md
Normal 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.
|
||||
27
XOR_Encryption/build/Makefile
Normal file
27
XOR_Encryption/build/Makefile
Normal 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
BIN
XOR_Encryption/build/xor_cipher
Executable file
Binary file not shown.
6
XOR_Encryption/include/xor_cipher.h
Normal file
6
XOR_Encryption/include/xor_cipher.h
Normal 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
BIN
XOR_Encryption/obj/main.o
Normal file
Binary file not shown.
BIN
XOR_Encryption/obj/xor_cipher.o
Normal file
BIN
XOR_Encryption/obj/xor_cipher.o
Normal file
Binary file not shown.
54
XOR_Encryption/src/main.c
Normal file
54
XOR_Encryption/src/main.c
Normal 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;
|
||||
}
|
||||
|
||||
10
XOR_Encryption/src/xor_cipher.c
Normal file
10
XOR_Encryption/src/xor_cipher.c
Normal 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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user