testing
This commit is contained in:
15
Simple_Substitution/README.md
Normal file
15
Simple_Substitution/README.md
Normal 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.
|
||||
28
Simple_Substitution/build/Makefile
Normal file
28
Simple_Substitution/build/Makefile
Normal 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
|
||||
|
||||
BIN
Simple_Substitution/build/substitution_cipher
Executable file
BIN
Simple_Substitution/build/substitution_cipher
Executable file
Binary file not shown.
7
Simple_Substitution/include/substitution_cipher.h
Normal file
7
Simple_Substitution/include/substitution_cipher.h
Normal 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
|
||||
BIN
Simple_Substitution/obj/main.o
Normal file
BIN
Simple_Substitution/obj/main.o
Normal file
Binary file not shown.
BIN
Simple_Substitution/obj/substitution_cipher.o
Normal file
BIN
Simple_Substitution/obj/substitution_cipher.o
Normal file
Binary file not shown.
22
Simple_Substitution/src/main.c
Normal file
22
Simple_Substitution/src/main.c
Normal 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;
|
||||
}
|
||||
36
Simple_Substitution/src/substitution_cipher.c
Normal file
36
Simple_Substitution/src/substitution_cipher.c
Normal 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user