Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 6 additions & 11 deletions src/cli.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#include "cli.h"

#include <iostream>

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

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

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

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

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

#include <string>

namespace cli {
void help();

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

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

#endif
4 changes: 2 additions & 2 deletions src/encryption.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <cmath>

#include "encryption.h"

#include <cmath>

std::int8_t encrypt(std::uint8_t data, unsigned long int e, unsigned long N) {
return std::pow(data, e);
}
9 changes: 4 additions & 5 deletions src/encryption.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#ifndef ENCRYPTION_H
#define ENCRYPTION_H

#include <iostream>
#include <cstdint>
#include <iostream>

class encryption
{
private:
public:
class encryption {
private:
public:
std::int8_t encrypt(std::uint8_t data, unsigned long int e, unsigned long N);
};

Expand Down
85 changes: 42 additions & 43 deletions src/key.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "key.h"

#include <filesystem>
#include <fstream>
#include <string>

#include "key.h"

std::filesystem::path key::keysPath() {
// Navigate to the root directory of the project
std::filesystem::path programRootPath = std::filesystem::current_path().parent_path();
Expand All @@ -13,51 +13,61 @@ 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
* 3: both keys exists */
// 0: key doesn't exist
// 1: only publicKey exists
// 2: only privateKey exists
// 3: both keys exists
int status = NONE;

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

// go over every file in the KEY_FOLDER
for (const auto& entry : std::filesystem::directory_iterator(keysFolder)) {
for (const auto &entry : std::filesystem::directory_iterator(keysFolder)) {
// check if the name matches
if (entry.path().stem().string() == name) {
// check if it's a public key
if (entry.path().extension() == ".pub") {
// break the loop if both files are found
if (status == NONE) status = PUBLIC; else {status = BOTH; break;};
if (status == NONE)
status = PUBLIC;
else {
status = BOTH;
break;
};
}
// check if it's a private key
else if (entry.path().has_extension() == false) {
if (status == NONE) status = PRIVATE; else {status = BOTH; break;};
if (status == NONE)
status = PRIVATE;
else {
status = BOTH;
break;
};
}
}
}

return status;
}

int key::writeKey(const std::string& name, std::vector<uint8_t> *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":""));
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()) {
outFile << "-----BEGIN RSA " << (isPublic? "PUBLIC":"PRIVATE") << " KEY-----\n";
outFile << "-----BEGIN RSA " << (isPublic ? "PUBLIC" : "PRIVATE") << " KEY-----\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 << "-----END RSA " << (isPublic ? "PUBLIC" : "PRIVATE") << " KEY-----\n";
outFile.close();
} else {
std::cerr << "Could not write to file: " << keyFile << std::endl;
Expand All @@ -70,7 +80,7 @@ int key::writeKey(const std::string& name, std::vector<uint8_t> *data, const boo
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":""));
std::filesystem::path keyFile = keysFolder / (name + (isPublic ? ".pub" : ""));
// load file in memory
std::ifstream inFile(keyFile);

Expand All @@ -83,8 +93,7 @@ int key::readKey(const std::string &name, std::vector<uint8_t> *data, bool isPub
std::string currentLine;
std::string keyString;

while (getline (inFile, currentLine)) {

while (getline(inFile, currentLine)) {
if (currentLine.empty()) continue;
if (currentLine.at(0) == '-') continue;

Expand All @@ -111,7 +120,6 @@ std::string key::base64Encode(const std::vector<uint8_t> &data) {
int index = 0;

while (index < data.size()) {

uint32_t dataSegment = 0;

// take 3 numbers from the vector
Expand Down Expand Up @@ -143,7 +151,7 @@ std::string key::base64Encode(const std::vector<uint8_t> &data) {
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()) {
Expand All @@ -162,7 +170,8 @@ std::vector<uint8_t> key::base64Decode(std::string data) {

// check for valid char code
if (number > 0b111111) {
std::cerr << "trying to Decode not valid char: " << letterSegment.at(i) << std::endl;
std::cerr << "trying to Decode not valid char: " << letterSegment.at(i)
<< std::endl;
continue;
}
dataSegment += number << i * 6;
Expand All @@ -187,24 +196,14 @@ uint8_t key::getBase64Index(char letter) {
}

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
};
*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;
}

Expand All @@ -228,8 +227,8 @@ void key::createRSAKey() {
return;
}

auto* keyPublic = new std::vector<uint8_t>();
auto* keyPrivate = new std::vector<uint8_t>();
auto *keyPublic = new std::vector<uint8_t>();
auto *keyPrivate = new std::vector<uint8_t>();

createKey(keyPublic, keyPrivate);

Expand All @@ -241,14 +240,14 @@ void key::createRSAKey() {
}
}

std::vector<uint8_t> * key::getPrivateKey(std::string &name) {
std::vector<uint8_t>* data = new std::vector<uint8_t>;
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::vector<uint8_t> * key::getPublicKey(std::string &name) {
std::vector<uint8_t>* data = new std::vector<uint8_t>;
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;
}
17 changes: 6 additions & 11 deletions src/key.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#ifndef KEY_H
#define KEY_H

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

#define KEY_FOLDER "rsa-keys"

enum {
NONE,
PUBLIC,
PRIVATE,
BOTH
};
enum { NONE, PUBLIC, PRIVATE, BOTH };

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

class key {
private:
private:
static std::filesystem::path keysPath();
static int keyExists(std::string name);
static void generatePublicFromPrivate();
Expand All @@ -33,11 +28,11 @@ class key {

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

public:
public:
static void createRSAKey();

static std::vector<uint8_t>* getPrivateKey(std::string &name);
static std::vector<uint8_t>* getPublicKey(std::string &name);
static std::vector<uint8_t> *getPrivateKey(std::string &name);
static std::vector<uint8_t> *getPublicKey(std::string &name);
};

#endif
Loading