testing
This commit is contained in:
3
rsa-algorithms/README.md
Normal file
3
rsa-algorithms/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# rsa-algorithms Project
|
||||
|
||||
This is a C project generated with the setup tool.
|
||||
28
rsa-algorithms/build/Makefile
Normal file
28
rsa-algorithms/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)/rsa.c
|
||||
OBJECTS = $(OBJDIR)/main.o $(OBJDIR)/rsa.o
|
||||
|
||||
# Build target
|
||||
TARGET = rsa
|
||||
|
||||
# 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
rsa-algorithms/build/rsa
Executable file
BIN
rsa-algorithms/build/rsa
Executable file
Binary file not shown.
8
rsa-algorithms/include/rsa.h
Normal file
8
rsa-algorithms/include/rsa.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef RSA_H
|
||||
#define RSA_H
|
||||
|
||||
void rsa_generate_keys(unsigned long *e, unsigned long *d, unsigned long *n);
|
||||
unsigned long rsa_encrypt(unsigned long message, unsigned long e, unsigned long n);
|
||||
unsigned long rsa_decrypt(unsigned long ciphertext, unsigned long d, unsigned long n);
|
||||
|
||||
#endif // RSA_H
|
||||
BIN
rsa-algorithms/obj/main.o
Normal file
BIN
rsa-algorithms/obj/main.o
Normal file
Binary file not shown.
BIN
rsa-algorithms/obj/rsa.o
Normal file
BIN
rsa-algorithms/obj/rsa.o
Normal file
Binary file not shown.
33
rsa-algorithms/src/main.c
Normal file
33
rsa-algorithms/src/main.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "rsa.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
unsigned long e, d, n;
|
||||
|
||||
// Generate public and private keys
|
||||
rsa_generate_keys(&e, &d, &n);
|
||||
printf("Public Key: (e = %lu, n = %lu)\n", e, n);
|
||||
printf("Private Key: (d = %lu, n = %lu)\n", d, n);
|
||||
|
||||
// Ask for a message to encrypt (assuming it's an integer for simplicity)
|
||||
unsigned long message;
|
||||
printf("Enter a message to encrypt (as an integer smaller than %lu): ", n);
|
||||
scanf("%lu", &message);
|
||||
|
||||
// Ensure message is smaller than n
|
||||
if (message >= n) {
|
||||
printf("Error: Message must be smaller than %lu.\n", n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Encrypt the message
|
||||
unsigned long ciphertext = rsa_encrypt(message, e, n);
|
||||
printf("Encrypted message: %lu\n", ciphertext);
|
||||
|
||||
// Decrypt the message
|
||||
unsigned long decrypted_message = rsa_decrypt(ciphertext, d, n);
|
||||
printf("Decrypted message: %lu\n", decrypted_message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
81
rsa-algorithms/src/rsa.c
Normal file
81
rsa-algorithms/src/rsa.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "rsa.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
// Helper function to calculate the greatest common divisor (GCD)
|
||||
static unsigned long gcd(unsigned long a, unsigned long b) {
|
||||
while (b != 0) {
|
||||
unsigned long temp = b;
|
||||
b = a % b;
|
||||
a = temp;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
// Helper function to find the modular inverse using the extended Euclidean algorithm
|
||||
static unsigned long mod_inverse(unsigned long e, unsigned long phi) {
|
||||
long t = 0, newt = 1;
|
||||
unsigned long r = phi, newr = e;
|
||||
|
||||
while (newr != 0) {
|
||||
unsigned long quotient = r / newr;
|
||||
|
||||
// Update t and newt
|
||||
long temp = t;
|
||||
t = newt;
|
||||
newt = temp - quotient * newt;
|
||||
|
||||
// Update r and newr
|
||||
unsigned long temp_r = r;
|
||||
r = newr;
|
||||
newr = temp_r - quotient * newr;
|
||||
}
|
||||
|
||||
if (r > 1) return 0; // e is not invertible
|
||||
if (t < 0) t += phi; // Ensure t is positive
|
||||
|
||||
return (unsigned long)t;
|
||||
}
|
||||
|
||||
|
||||
// Generate RSA keys (public and private)
|
||||
void rsa_generate_keys(unsigned long *e, unsigned long *d, unsigned long *n) {
|
||||
// Two prime numbers (for simplicity, using small values)
|
||||
unsigned long p = 61, q = 53;
|
||||
*n = p * q;
|
||||
unsigned long phi = (p - 1) * (q - 1);
|
||||
|
||||
// Choose e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1
|
||||
*e = 17; // Commonly chosen small prime number
|
||||
if (gcd(*e, phi) != 1) {
|
||||
printf("Error: e and phi(n) are not coprime.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Calculate d, the modular inverse of e mod phi(n)
|
||||
*d = mod_inverse(*e, phi);
|
||||
if (*d == 0) {
|
||||
printf("Error: Could not find modular inverse of e.\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Encrypt message using public key (e, n)
|
||||
unsigned long rsa_encrypt(unsigned long message, unsigned long e, unsigned long n) {
|
||||
unsigned long result = 1;
|
||||
for (unsigned long i = 0; i < e; i++) {
|
||||
result = (result * message) % n;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Decrypt ciphertext using private key (d, n)
|
||||
unsigned long rsa_decrypt(unsigned long ciphertext, unsigned long d, unsigned long n) {
|
||||
unsigned long result = 1;
|
||||
for (unsigned long i = 0; i < d; i++) {
|
||||
result = (result * ciphertext) % n;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user