testing
This commit is contained in:
21
Vigenère_Cipher/README.md
Normal file
21
Vigenère_Cipher/README.md
Normal 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.
|
||||
28
Vigenère_Cipher/build/Makefile
Normal file
28
Vigenère_Cipher/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)/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
|
||||
|
||||
BIN
Vigenère_Cipher/build/vigenere_cipher
Executable file
BIN
Vigenère_Cipher/build/vigenere_cipher
Executable file
Binary file not shown.
7
Vigenère_Cipher/include/vigenere_cipher.h
Normal file
7
Vigenère_Cipher/include/vigenere_cipher.h
Normal 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
BIN
Vigenère_Cipher/obj/main.o
Normal file
Binary file not shown.
BIN
Vigenère_Cipher/obj/vigenere_cipher.o
Normal file
BIN
Vigenère_Cipher/obj/vigenere_cipher.o
Normal file
Binary file not shown.
26
Vigenère_Cipher/src/main.c
Normal file
26
Vigenère_Cipher/src/main.c
Normal 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;
|
||||
}
|
||||
28
Vigenère_Cipher/src/vigenere_cipher.c
Normal file
28
Vigenère_Cipher/src/vigenere_cipher.c
Normal 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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user