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,16 @@
# DES Encryption Algorithm
## Description
This project implements a simplified version of the DES encryption algorithm, with encryption and decryption using a symmetric key.
## How to Build
1. Navigate to the `build` directory.
2. Run `make` to compile the project.
## How to Run
1. After building, run the `des` executable using `./des`.
2. You will be prompted to enter a message and a key, both as integers (64-bit).
## How to Clean
- Run `make clean` 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)/des.c
OBJECTS = $(OBJDIR)/main.o $(OBJDIR)/des.o
# Build target
TARGET = des
# 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 DES_H
#define DES_H
void des_encrypt(unsigned long long *message, unsigned long long key);
void des_decrypt(unsigned long long *message, unsigned long long key);
#endif // DES_H

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,76 @@
#include "des.h"
#include <stdio.h>
#include <stdint.h>
// A simplified placeholder for the Initial Permutation
static uint64_t initial_permutation(uint64_t message) {
// Apply an initial permutation (just a placeholder for now)
return message; // In a real implementation, this would rearrange the bits
}
// A simplified placeholder for the Final Permutation
static uint64_t final_permutation(uint64_t message) {
// Apply the final permutation (just a placeholder for now)
return message; // In a real implementation, this would rearrange the bits
}
// A simplified placeholder for the Feistel function used in DES rounds
static uint32_t feistel_function(uint32_t half_block, uint64_t subkey) {
// Apply an expansion, substitution, and permutation (placeholder)
return half_block ^ (uint32_t)(subkey); // Simplified XOR with the key
}
// A simplified function to split the message into left and right halves
static void split_message(uint64_t message, uint32_t *left, uint32_t *right) {
*left = (uint32_t)(message >> 32);
*right = (uint32_t)(message & 0xFFFFFFFF);
}
// A simplified function to merge left and right halves into one message
static uint64_t merge_message(uint32_t left, uint32_t right) {
return ((uint64_t)left << 32) | (uint64_t)right;
}
// The main DES encryption function
void des_encrypt(unsigned long long *message, unsigned long long key) {
// Apply the initial permutation
*message = initial_permutation(*message);
uint32_t left, right;
split_message(*message, &left, &right);
// Perform 16 rounds of the Feistel function (simplified for now)
for (int i = 0; i < 16; i++) {
uint32_t temp = right;
right = left ^ feistel_function(right, key); // Simplified function
left = temp;
}
// Merge the halves back together
*message = merge_message(left, right);
// Apply the final permutation
*message = final_permutation(*message);
}
// The main DES decryption function (similar to encryption, but reverse)
void des_decrypt(unsigned long long *message, unsigned long long key) {
// Apply the initial permutation
*message = initial_permutation(*message);
uint32_t left, right;
split_message(*message, &left, &right);
// Perform 16 rounds of the Feistel function (reversed for decryption)
for (int i = 0; i < 16; i++) {
uint32_t temp = left;
left = right ^ feistel_function(left, key); // Simplified function
right = temp;
}
// Merge the halves back together
*message = merge_message(left, right);
// Apply the final permutation
*message = final_permutation(*message);
}

View File

@@ -0,0 +1,25 @@
#include "des.h"
#include <stdio.h>
int main() {
unsigned long long message;
unsigned long long key;
// Ask for a message and key (both as integers for simplicity)
printf("Enter a message to encrypt (as a 64-bit integer): ");
scanf("%llu", &message);
printf("Enter a key (as a 64-bit integer): ");
scanf("%llu", &key);
// Encrypt the message
des_encrypt(&message, key);
printf("Encrypted message: %llu\n", message);
// Decrypt the message
des_decrypt(&message, key);
printf("Decrypted message: %llu\n", message);
return 0;
}