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,21 @@
# Vigenère Cipher
## Description
This project implements a Vigenère cipher to demonstrate basic encryption and decryption using a keyword-based shifting approach.
## Directory Structure
- `include/`: Contains the header files.
- `src/`: Contains the source code files (`main.c` and `vigenere_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 `vigenere_cipher` in the `build` directory.
2. Run it using `./vigenere_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)/vigenere_cipher.c
OBJECTS = $(OBJDIR)/main.o $(OBJDIR)/vigenere_cipher.o
# Build target
TARGET = vigenere_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 VIGENERE_CIPHER_H
#define VIGENERE_CIPHER_H
void encrypt(char *plaintext, const char *key);
void decrypt(char *ciphertext, const char *key);
#endif // VIGENERE_CIPHER_H

BIN
Vigenère_Cipher/obj/main.o Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,26 @@
#include "vigenere_cipher.h"
#include <stdio.h>
#include <string.h>
int main() {
char message[256];
char key[256];
printf("Enter a message to encrypt: ");
fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = '\0'; // Remove newline character
printf("Enter the encryption key: ");
fgets(key, sizeof(key), stdin);
key[strcspn(key, "\n")] = '\0'; // Remove newline character
// Encrypt the message
encrypt(message, key);
printf("Encrypted message: %s\n", message);
// Decrypt the message
decrypt(message, key);
printf("Decrypted message: %s\n", message);
return 0;
}

View File

@@ -0,0 +1,28 @@
#include "vigenere_cipher.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void encrypt(char *plaintext, const char *key) {
int keyLen = strlen(key);
for (int i = 0, j = 0; plaintext[i] != '\0'; i++) {
if (isalpha(plaintext[i])) {
char base = islower(plaintext[i]) ? 'a' : 'A';
int shift = (tolower(key[j % keyLen]) - 'a');
plaintext[i] = (plaintext[i] - base + shift) % 26 + base;
j++;
}
}
}
void decrypt(char *ciphertext, const char *key) {
int keyLen = strlen(key);
for (int i = 0, j = 0; ciphertext[i] != '\0'; i++) {
if (isalpha(ciphertext[i])) {
char base = islower(ciphertext[i]) ? 'a' : 'A';
int shift = (tolower(key[j % keyLen]) - 'a');
ciphertext[i] = (ciphertext[i] - base - shift + 26) % 26 + base;
j++;
}
}
}