Skip to content

Commit

Permalink
Main commit
Browse files Browse the repository at this point in the history
  • Loading branch information
saintedlittle committed Feb 25, 2023
1 parent 7ceba73 commit 1cd0739
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 0 deletions.
138 changes: 138 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
### C++ ###
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

### CLion+all ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### CLion+all Patch ###
# Ignore everything but code style settings and run configurations
# that are supposed to be shared within teams.

.idea/*

!.idea/codeStyles
!.idea/runConfigurations

### CMake ###
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps

### CMake Patch ###
# External projects
*-prefix/
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.25)
project(ProjectSafe)

set(CMAKE_CXX_STANDARD 20)

find_package(cryptopp CONFIG REQUIRED)

add_executable(ProjectSafe src/main.cpp)

target_link_libraries(ProjectSafe PRIVATE cryptopp::cryptopp)
198 changes: 198 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include <iostream>
#include <fstream>
#include <string>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>
#include "cryptopp/osrng.h"

using namespace std;

using namespace CryptoPP;
using CryptoPP::AES;

class Encryption {
public:
static string encryptToFile(const string& key, const string& input, const string& filename) {
CryptoPP::byte iv[AES::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));

AES::Encryption aesEncryption((CryptoPP::byte*)key.c_str(), AES::DEFAULT_KEYLENGTH);
CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

string ciphertext;
StreamTransformationFilter stfEncryptor(cbcEncryption, new StringSink(ciphertext));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(input.c_str()), input.length());
stfEncryptor.MessageEnd();

ofstream file(filename, ios::out | ios::binary);
file.write(reinterpret_cast<const char*>(iv), AES::BLOCKSIZE);
file.write(ciphertext.c_str(), ciphertext.length());
file.close();
return ciphertext;
}

static string decryptFromFile(const string& key, const string& filename) {
ifstream file(filename, ios::in | ios::binary);
if (!file) {
throw runtime_error("Unable to open file");
}

CryptoPP::byte iv[AES::BLOCKSIZE];
file.read(reinterpret_cast<char*>(iv), AES::BLOCKSIZE);

AES::Decryption aesDecryption((CryptoPP::byte*)key.c_str(), AES::DEFAULT_KEYLENGTH);
CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);

string decryptedData;
vector<CryptoPP::byte> buffer((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
ArraySource arraySource(buffer.data(), buffer.size(), true,
new StreamTransformationFilter(cbcDecryption,
new StringSink(decryptedData)));

file.close();
return decryptedData;
}

private:
static CryptoPP::AutoSeededRandomPool prng;
};

CryptoPP::AutoSeededRandomPool Encryption::prng;

string getKey();
string askKeyInput();

string getInputString();
string getFilename();
int getOperation();

void helpMessage();

int main() {

const string key = getKey();

helpMessage();

const int operation = getOperation();

if (operation == 2) {
const string input = getInputString();
const string filename = getFilename();

// Encrypt and write to file
cout << "Encrypting..." << endl;
Encryption::encryptToFile(key, input, filename);
} else if (operation == 3) {
// Read and decrypt from file
const string filename = getFilename();

cout << "Decrypting..." << endl;
string decrypted = Encryption::decryptFromFile(key, filename);
cout << "Decrypted output: " << decrypted << endl;
} else if (operation == 1) {
askKeyInput();
} else if (operation == 4) {
exit(EXIT_SUCCESS);
} else {
cerr << "SELECT 1/2/3/4 !!!" << endl;
}

return 0;
}

string askKeyInput() {
string key;

string answer;

// Get the key from the user and save it in the file
cout << "Enter the key: \033[1;36m";
cin >> key;
cout << "\033[0m";
cout << "Does i save key? (y/n) \033[1;36m";
cin >> answer;
cout << "\033[0m";
if (answer.empty() || answer.starts_with('n')) {
return key;
}

ofstream configFile("config.txt");
if (configFile.is_open()) {
configFile << key;
configFile.close();
}
else {
cerr << "Failed to save the key in config.txt!" << endl;
}
}

string getKey() {
string key;
ifstream configFile("config.txt");
if (configFile.is_open()) {
cout << "\nFound config.txt! Try load..." << endl;
// Read the key from the file
getline(configFile, key);
configFile.close();
cout << "\033[32mSuccess !\033[0m" << endl;

} else {
cerr << "config.txt not found!" << endl;
key = askKeyInput();
}

if (key.empty()) {
cerr << "config.txt is empty!" << endl;
key = askKeyInput();
}

return key;
}

string getInputString() {
string answer;

cout << "Enter the string: \033[1;36m";
cin >> answer;
cout << "\033[0m";

return answer;
}

string getFilename() {
string answer;

// Get the key from the user and save it in the file
cout << "Enter the filename: \033[1;36m";
cin >> answer;
cout << "\033[0m";

return answer;
}

int getOperation() {
int answer;

// Get the key from the user and save it in the file
cout << "Select operation(1/2/3/4): ";
cin >> answer;

return answer;
}

void helpMessage() {
cout << "\033[1;36m";
cout << "**********************************************\n";
cout << "* Welcome to ProjectSafe! *\n";
cout << "* This program can encrypt and decrypt files *\n";
cout << "* and strings. Choose an option from below *\n";
cout << "**********************************************\n";
cout << "1. Change/Select key.\n";
cout << "2. Encrypt string.\n";
cout << "3. Decrypt file.\n";
cout << "4. Exit\n";
cout << "*********************************************\n";
cout << "\033[0m";
}
9 changes: 9 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name" : "projectsafe",
"version-string" : "1.0.0",
"builtin-baseline" : "82e03905f54fc49d11346e5148e3860747604d86",
"dependencies" : [ {
"name" : "cryptopp",
"version>=" : "8.7.0#3"
} ]
}

0 comments on commit 1cd0739

Please sign in to comment.