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

View File

@@ -0,0 +1,15 @@
# Simple Substitution Cipher
## Description
A simple substitution cipher replaces each letter in the plaintext with another letter from the alphabet, based on a predetermined key.
## 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 `substitution_cipher` in the `build` directory.
2. Run it using `./substitution_cipher`.
## How to Clean
- Run `make clean` in the `build` directory to remove all compiled files.

View File

@@ -0,0 +1,28 @@
# Variables
CC = gcc
CFLAGS = -I../include -Wall -Wextra
OBJDIR = ../obj
SRCDIR = ../src
BINDIR = ../build
# Source files
SOURCES = $(SRCDIR)/main.c $(SRCDIR)/substitution_cipher.c
OBJECTS = $(OBJDIR)/main.o $(OBJDIR)/substitution_cipher.o
# Build target
TARGET = substitution_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

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#ifndef SUBSTITUTION_CIPHER_H
#define SUBSTITUTION_CIPHER_H
void substitution_encrypt(char *message, const char *key);
void substitution_decrypt(char *message, const char *key);
#endif // SUBSTITUTION_CIPHER_H

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,22 @@
#include "substitution_cipher.h"
#include <stdio.h>
#include <string.h>
int main() {
char message[256];
char key[27] = "QWERTYUIOPLKJHGFDSAZXCVBNM"; // Simple substitution key
printf("Enter a message to encrypt: ");
fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = '\0'; // Remove the newline character
// Encrypt the message
substitution_encrypt(message, key);
printf("Encrypted message: %s\n", message);
// Decrypt the message to verify it works
substitution_decrypt(message, key);
printf("Decrypted message: %s\n", message);
return 0;
}

View File

@@ -0,0 +1,36 @@
#include "substitution_cipher.h"
#include <string.h>
#include <ctype.h> // for isalpha, toupper
// Helper function to find the position of a character in the alphabet
static int find_position(char c) {
c = toupper(c);
return c - 'A';
}
// Encrypt the message using the provided key
void substitution_encrypt(char *message, const char *key) {
for (int i = 0; message[i] != '\0'; i++) {
if (isalpha(message[i])) {
int pos = find_position(message[i]);
//char base = isupper(message[i]) ? 'A' : 'a';
message[i] = isupper(message[i]) ? key[pos] : tolower(key[pos]);
}
}
}
// Decrypt the message using the provided key
void substitution_decrypt(char *message, const char *key) {
char reverse_key[26];
for (int i = 0; i < 26; i++) {
reverse_key[key[i] - 'A'] = 'A' + i;
}
for (int i = 0; message[i] != '\0'; i++) {
if (isalpha(message[i])) {
int pos = find_position(message[i]);
//char base = isupper(message[i]) ? 'A' : 'a';
message[i] = isupper(message[i]) ? reverse_key[pos] : tolower(reverse_key[pos]);
}
}
}