Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
f511299
fix cmake project not including key class
LordofGhost Mar 7, 2025
417b0a3
remove old files
LordofGhost Mar 9, 2025
dcdaa63
refactor namespace key inside namespace cli into keyManager
LordofGhost Mar 9, 2025
9d629f0
add base64Encode functionality
LordofGhost Mar 9, 2025
0a9cd89
add base64 Decode logic
LordofGhost Mar 9, 2025
0155549
implement read key from file, rework dummy create key
LordofGhost Mar 9, 2025
e92bcc7
Implement mul function for vec
Jochengehtab Mar 10, 2025
b754156
format
Jochengehtab Mar 10, 2025
31971d7
add header guards
LordofGhost Mar 16, 2025
f2dce96
fix documentation
LordofGhost Mar 16, 2025
04d08d7
implement pow operation
LordofGhost Mar 17, 2025
751f283
implement zero check for vector based numbers
LordofGhost Mar 17, 2025
f3b6ce6
create isEqual logic for vector numbers and dummy function for isBigger
LordofGhost Mar 18, 2025
4d69b54
implement getStartBitIndex
LordofGhost Mar 18, 2025
8ddf217
implement subtract
LordofGhost Mar 18, 2025
b19c9fa
move into namespace operations
LordofGhost Mar 18, 2025
7b100a3
fix doc
Jochengehtab Mar 18, 2025
073c1fe
fix cmake project not including key class
LordofGhost Mar 7, 2025
e3a616c
remove old files
LordofGhost Mar 9, 2025
2fa26bc
refactor namespace key inside namespace cli into keyManager
LordofGhost Mar 9, 2025
bc90619
add base64Encode functionality
LordofGhost Mar 9, 2025
83bbecf
add base64 Decode logic
LordofGhost Mar 9, 2025
54fd87c
implement read key from file, rework dummy create key
LordofGhost Mar 9, 2025
bf0f468
Implement mul function for vec
Jochengehtab Mar 10, 2025
6ce67d3
format
Jochengehtab Mar 10, 2025
5db949a
add header guards
LordofGhost Mar 16, 2025
dbc71e0
fix documentation
LordofGhost Mar 16, 2025
53b63d6
implement pow operation
LordofGhost Mar 17, 2025
d06bc8c
implement zero check for vector based numbers
LordofGhost Mar 17, 2025
c2387bc
create isEqual logic for vector numbers and dummy function for isBigger
LordofGhost Mar 18, 2025
cf48952
implement getStartBitIndex
LordofGhost Mar 18, 2025
c6df8c1
implement subtract
LordofGhost Mar 18, 2025
1f00422
move into namespace operations
LordofGhost Mar 18, 2025
ef7fa76
fix doc
Jochengehtab Mar 18, 2025
9cda1dd
Merge branch 'feature/key' of https://github.com/ParallelEngineering/…
Jochengehtab Mar 23, 2025
49be7ed
fix main example
Jochengehtab Mar 23, 2025
cf18da0
simplify isEqual function
Jochengehtab Mar 23, 2025
4db5461
implement basic is bigger function without leading zero check
Jochengehtab Mar 23, 2025
b9daffa
implement leading zero check
Jochengehtab Mar 23, 2025
0059042
add documentation for is bigger
Jochengehtab Mar 23, 2025
e4eab22
fix mul
Jochengehtab Mar 23, 2025
b34dfe9
remove unecessary code
Jochengehtab Mar 23, 2025
14384d1
fix main example
Jochengehtab Mar 23, 2025
b62dd41
Feature/key#zero check (#8)
LordofGhost Mar 23, 2025
255200a
fix isBigger
LordofGhost Mar 23, 2025
0bb3745
implement divide operation
LordofGhost Mar 23, 2025
efe731d
Implement Modulu (#10)
LordofGhost Mar 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_executable(RSA-Encryptor
main.cpp
utility.cpp
keys.cpp
key.cpp
encryption.cpp
cli.cpp
)
10 changes: 5 additions & 5 deletions src/cli.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include <iostream>

#include "cli.h"
#include "keys.h"
#include "key.h"

void cli::help() {
std::cout << "Welcome to RSA-Encryptor" << std::endl;
}

void cli::key::create() {
keys::createRSAKey();
void cli::keyManager::create() {
key::createRSAKey();
}

void cli::key::list() {
void cli::keyManager::list() {
}

void cli::key::print(const std::string& name, bool publicKey, bool privateKey) {
void cli::keyManager::print(const std::string& name, bool publicKey, bool privateKey) {
}
9 changes: 7 additions & 2 deletions src/cli.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#ifndef CLI_H
#define CLI_H

namespace cli {
void help();

namespace key {
namespace keyManager {
void create();
void list();
void print(const std::string& name, bool publicKey, bool privateKey);
}
}
}

#endif
7 changes: 6 additions & 1 deletion src/encryption.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef ENCRYPTION_H
#define ENCRYPTION_H

#include <iostream>
#include <cstdint>

Expand All @@ -6,4 +9,6 @@ class encryption
private:
public:
std::int8_t encrypt(std::uint8_t data, unsigned long int e, unsigned long N);
};
};

#endif
179 changes: 166 additions & 13 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ std::filesystem::path key::keysPath() {
}

int key::keyExists(std::string name) {
// TODO fix function not case sensitive
/* 0: key doesn't exist
* 1: only publicKey exists
* 2: only privateKey exists
Expand Down Expand Up @@ -39,29 +40,172 @@ int key::keyExists(std::string name) {
return status;
}

int key::writeKey(const std::string& name, const unsigned long int data, const bool isPublic) {
int key::writeKey(const std::string& name, std::vector<uint8_t> *data, const bool isPublic) {
if (data == nullptr) return -1;

std::filesystem::path keysFolder = keysPath();

std::filesystem::path keyFile = keysFolder / (name + (isPublic? ".pub":""));
// load file in memory
std::ofstream outFile(keyFile);

// key in base64 format
std::string base64Data = base64Encode(*data);

if (outFile.is_open()) {
std::string dataStr = std::to_string(data);
outFile << "-----BEGIN RSA " << (isPublic? "PUBLIC":"PRIVATE") << " KEY-----\n";
outFile << base64_encode(dataStr) << "\n";
for (int i = 0; i < base64Data.size(); i += 64) {
outFile << base64Data.substr(i, 64) << "\n";
}
outFile << "-----END RSA " << (isPublic? "PUBLIC":"PRIVATE") << " KEY-----\n";
outFile.close();
} else {
std::cerr << "Could not write to file: " << keyFile << std::endl;
return 0;
return -1;
}

return 1;
}

std::string key::base64_encode(std::string &data) {
//TODO Add base 64 encode function
return data;
int key::readKey(const std::string &name, std::vector<uint8_t> *data, bool isPublic) {
std::filesystem::path keysFolder = keysPath();

std::filesystem::path keyFile = keysFolder / (name + (isPublic? ".pub":""));
// load file in memory
std::ifstream inFile(keyFile);

// check if file is loaded correctly
if (!inFile.is_open()) {
std::cerr << "Die Datei konnte nicht geladen werden!" << std::endl;
return -1;
}

std::string currentLine;
std::string keyString;

while (getline (inFile, currentLine)) {

if (currentLine.empty()) continue;
if (currentLine.at(0) == '-') continue;

keyString += currentLine;
}

// remove any line breaks
for (int c = keyString.length() - 1; c >= 0; c--) {
if (keyString.at(c) == '\n') {
keyString.erase(c);
}
}

*data = base64Decode(keyString);

return 1;
}

std::string key::base64Encode(const std::vector<uint8_t> &data) {
// TODO the encoding might not work if the data size is not dividable by 3
if (data.size() % 3 != 0) std::cerr << "Encoding data size not dividable by 3" << std::endl;

std::vector<uint8_t> result;
int index = 0;

while (index < data.size()) {

uint32_t dataSegment = 0;

// take 3 numbers from the vector
for (int i = 2; i >= 0; i--) {
// prevent from reading outside the vector
if (index > data.size() - 1) break;

uint8_t number = data.at(index++);
dataSegment += number << i * 8;
}

// 4 times, put 6 bits from dataSegment into result vector
for (int j = 0; j < 4; j++) {
dataSegment <<= 6;
result.push_back(static_cast<uint8_t>((dataSegment & 0b00111111 << 24) >> 24));
}
}

std::string outString;

// convert the numeric value to the corresponding char of the base64 charset
for (auto c : result) {
outString += base64Chars[c];
}

return outString;
}

std::vector<uint8_t> key::base64Decode(std::string data) {
// TODO the decoding might not work if the data size is not dividable by 4
if (data.size() % 4 != 0) std::cerr << "Decoding data size not dividable by 4" << std::endl;

std::vector<uint8_t> result;

while (!data.empty()) {
// remove 4 chars from the string
std::string letterSegment = data.substr(0, 4);
data.erase(0, 4);

uint32_t dataSegment = 0;

// put 4 chars in one binary string
for (int i = 3; i >= 0; i--) {
// check if there are remaining letters
if (i > letterSegment.length() - 1) continue;

const uint8_t number = getBase64Index(letterSegment.at(3 - i));

// check for valid char code
if (number > 0b111111) {
std::cerr << "trying to Decode not valid char: " << letterSegment.at(i) << std::endl;
continue;
}
dataSegment += number << i * 6;
}

// read data from binary string
for (int j = 0; j < 3; j++) {
dataSegment <<= 8;
result.push_back((dataSegment & 0xFF << 24) >> 24);
}
}
return result;
}

uint8_t key::getBase64Index(char letter) {
for (int i = 0; base64Chars[i] != '\0'; i++) {
if (base64Chars[i] == letter) {
return i;
}
}
return 0;
}

int key::createKey(std::vector<uint8_t> *keyPublic, std::vector<uint8_t> *keyPrivate) {
*keyPublic = {
23, 87, 45, 190, 12, 78, 34, 210, 56, 89,
123, 67, 90, 150, 32, 76, 54, 200, 11, 99,
101, 145, 67, 189, 43, 88, 29, 176, 58, 92,
111, 134, 78, 201, 15, 84, 39, 220, 66, 97,
105, 142, 71, 185, 49, 81, 27, 170, 53, 95,
41
};
*keyPrivate = {
34, 78, 123, 56, 89, 210, 45, 190, 12, 87,
67, 150, 32, 76, 54, 200, 11, 99, 101, 145,
67, 189, 43, 88, 29, 176, 58, 92, 111, 134,
78, 201, 15, 84, 39, 220, 66, 97, 105, 142,
71, 185, 49, 81, 27, 170, 53, 95, 102, 147,
68, 191, 44, 85, 30, 177, 59, 93, 112, 135,
79, 202, 16, 85, 40, 221, 67, 98, 23, 87,
91, 65
};
return 1;
}

void key::createRSAKey() {
Expand All @@ -84,18 +228,27 @@ void key::createRSAKey() {
return;
}

if (writeKey(keyName, 0, true) == 1 && writeKey(keyName, 0, false) == 1) {
auto* keyPublic = new std::vector<uint8_t>();
auto* keyPrivate = new std::vector<uint8_t>();

createKey(keyPublic, keyPrivate);

if (writeKey(keyName, keyPublic, true) == 1 && writeKey(keyName, keyPrivate, false) == 1) {
std::cout << keyName + " key successfully created!\n";
std::cout << "You can find your newly created key here: " << keysFolder << std::endl;
} else {
std::cerr << "Key creation failed!" << std::endl;
}
}

std::pair<unsigned long int, unsigned long int> getPrivateKey(std::string& name) {
return {647'090'566'899, 234'099'456'876'004};
std::vector<uint8_t> * key::getPrivateKey(std::string &name) {
std::vector<uint8_t>* data = new std::vector<uint8_t>;
readKey(name, data, false);
return data;
}

std::pair<unsigned long int, unsigned long int> getPublicKey(std::string& name) {
return {143'548'453'234, 234'099'456'876'004};
}
std::vector<uint8_t> * key::getPublicKey(std::string &name) {
std::vector<uint8_t>* data = new std::vector<uint8_t>;
readKey(name, data, true);
return data;
}
27 changes: 22 additions & 5 deletions src/key.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#ifndef KEY_H
#define KEY_H

#include <iostream>
#include <filesystem>
#include <map>
#include <vector>

#define KEY_FOLDER "rsa-keys"

Expand All @@ -10,17 +15,29 @@ enum {
BOTH
};

const char base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

class key {
private:
static std::filesystem::path keysPath();
static int keyExists(std::string name);
static void generatePublicFromPrivate();
static int writeKey(const std::string &name, unsigned long int data, bool isPublic);
static std::string base64_encode(std::string &data);

static int writeKey(const std::string &name, std::vector<uint8_t> *data, bool isPublic);
static int readKey(const std::string &name, std::vector<uint8_t> *data, bool isPublic);

static std::string base64Encode(const std::vector<uint8_t> &data);
static std::vector<uint8_t> base64Decode(std::string data);

static uint8_t getBase64Index(char letter);

static int createKey(std::vector<uint8_t> *keyPublic, std::vector<uint8_t> *keyPrivate);

public:
static void createRSAKey();

static std::pair<unsigned long int, unsigned long int> getPrivateKey(std::string &name);
static std::pair<unsigned long int, unsigned long int> getPublicKey(std::string &name);
};
static std::vector<uint8_t>* getPrivateKey(std::string &name);
static std::vector<uint8_t>* getPublicKey(std::string &name);
};

#endif
30 changes: 0 additions & 30 deletions src/keys.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions src/keys.h

This file was deleted.

Loading
Loading