From 5bcbe09922bf688ee351cf987eca024dbc540b3b Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Mon, 6 Dec 2021 02:09:03 +0100 Subject: [PATCH] Implemented a wrapper for super easy use --- Feistel.vcxproj | 2 + Feistel.vcxproj.filters | 6 +++ GhettoCipherWrapper.cpp | 87 +++++++++++++++++++++++++++++++++++++++++ GhettoCipherWrapper.h | 30 ++++++++++++++ main.cpp | 64 ++++++++++++++++-------------- 5 files changed, 160 insertions(+), 29 deletions(-) create mode 100644 GhettoCipherWrapper.cpp create mode 100644 GhettoCipherWrapper.h diff --git a/Feistel.vcxproj b/Feistel.vcxproj index 0161647..6b61143 100644 --- a/Feistel.vcxproj +++ b/Feistel.vcxproj @@ -140,6 +140,7 @@ + @@ -147,6 +148,7 @@ + diff --git a/Feistel.vcxproj.filters b/Feistel.vcxproj.filters index c16d76f..12ea42d 100644 --- a/Feistel.vcxproj.filters +++ b/Feistel.vcxproj.filters @@ -24,6 +24,9 @@ Quelldateien + + Quelldateien + @@ -50,5 +53,8 @@ Headerdateien + + Headerdateien + \ No newline at end of file diff --git a/GhettoCipherWrapper.cpp b/GhettoCipherWrapper.cpp new file mode 100644 index 0000000..2aeee23 --- /dev/null +++ b/GhettoCipherWrapper.cpp @@ -0,0 +1,87 @@ +#include "GhettoCipherWrapper.h" +#include "GhettoCipher.h" +#include "Util.h" + +std::string GhettoCipherWrapper::EncryptString(const std::string& cleartext, const std::string& password) +{ + // Instanciate our cipher and supply a key + GhettoCipher cipher(password); + + // Recode the ascii-string to bits + const Flexblock cleartext_bits = StringToBits(cleartext); + + // Encrypt our cleartext bits + const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits); + + // Recode the ciphertext bits to a hex-string + const std::string ciphertext = BitsToHexstring(ciphertext_bits); + + // Return it + return ciphertext; +} + +std::string GhettoCipherWrapper::DecryptString(const std::string& ciphertext, const std::string& password) +{ + // Instanciate our cipher and supply a key + GhettoCipher cipher(password); + + // Recode the hex-string to bits + const Flexblock ciphertext_bits = HexstringToBits(ciphertext); + + // Decrypt the ciphertext bits + const std::string cleartext_bits = cipher.Decipher(ciphertext_bits); + + // Recode the cleartext bits to an ascii-string + const std::string cleartext = BitsToString(cleartext_bits); + + // Return it + return cleartext; +} + +bool GhettoCipherWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password) +{ + try + { + // Read the file to bits + const Flexblock cleartext_bits = ReadFileToBits(filename_in); + + // Instanciate our cipher and supply a key + GhettoCipher cipher(password); + + // Encrypt our cleartext bits + const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits); + + // Write our ciphertext bits to file + WriteBitsToFile(filename_out, ciphertext_bits); + + return true; + } + catch (std::runtime_error&) + { + return false; + } +} + +bool GhettoCipherWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password) +{ + try + { + // Read the file to bits + const Flexblock ciphertext_bits = ReadFileToBits(filename_in); + + // Instanciate our cipher and supply a key + GhettoCipher cipher(password); + + // Decrypt the ciphertext bits + const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits); + + // Write our cleartext bits to file + WriteBitsToFile(filename_out, cleartext_bits); + + return true; + } + catch (std::runtime_error&) + { + return false; + } +} diff --git a/GhettoCipherWrapper.h b/GhettoCipherWrapper.h new file mode 100644 index 0000000..79b4824 --- /dev/null +++ b/GhettoCipherWrapper.h @@ -0,0 +1,30 @@ +#pragma once +#include + +/** This class is a wrapper to make working with the GhettoCipher super easy with a python-like syntax +*/ +class GhettoCipherWrapper +{ +public: + //! Will encrypt a string and return it hexadecimally encoded. + static std::string EncryptString(const std::string& cleartext, const std::string& password); + + //! Will decrypt a hexadecimally encoded string. + static std::string DecryptString(const std::string& ciphertext, const std::string& password); + + //! Will encrypt a file. + //! Returns false if anything goes wrong (like, file-access). + //! @filename_in The file to be read. + //! @filename_out The file the encrypted version should be saved in. + static bool EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password); + + //! Will decrypt a file. + //! Returns false if anything goes wrong (like, file-access). + //! @filename_in The file to be read. + //! @filename_out The file the decrypted version should be saved in. + static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password); + +private: + // No instanciation! >:( + GhettoCipherWrapper(); +}; diff --git a/main.cpp b/main.cpp index ac16d34..c603c80 100644 --- a/main.cpp +++ b/main.cpp @@ -1,38 +1,44 @@ #pragma once #include #include "Util.h" -#include "GhettoCipher.h" +#include "GhettoCipherWrapper.h" + +void ExampleString() +{ + std::cout << "Example on how to encrypt & decrypt a string:" << std::endl; + + // Get some string + const std::string input = "I am a super secret message!"; + std::cout << input << std::endl; + + // Encrypt + const std::string encrypted = GhettoCipherWrapper::EncryptString(input, "password1"); + std::cout << encrypted << std::endl; + + // Decrypt + const std::string decrypted = GhettoCipherWrapper::DecryptString(encrypted, "password1"); + std::cout << decrypted << std::endl; + + return; +} + +void ExampleFiles() +{ + std::cout << "Example on how to encrypt & decrypt any file:" << std::endl; + + // Encrypt + GhettoCipherWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1"); + + // Decrypt + GhettoCipherWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1"); + + return; +} int main() { - // Load file to be encrypted - const Flexblock file = ReadFileToBits("images.jpg"); - std::cout << "Finished reading..." << std::endl; - - - // Prepare cipher - GhettoCipher feistel("Password yo"); - - - // Encrypt - const Flexblock ciphertext = feistel.Encipher(file, true); - //const std::string cipherHex = BitsToHexstring(ciphertext); - std::cout << "Finished encrypting..." << std::endl; - - // Save encrypted file - WriteBitsToFile("encrypted.jpg", ciphertext); - std::cout << "Finished writing..." << std::endl; - - - // Decrypt - //const Flexblock cleartextBits = feistel.Decipher(HexstringToBits(cipherHex)); - const Flexblock cleartextBits = feistel.Decipher(ciphertext, true); - std::cout << "Finished decrypting..." << std::endl; - - - // Save decrypted file - WriteBitsToFile("decrypted.jpg", cleartextBits); - std::cout << "Finished writing..." << std::endl; + ExampleString(); + ExampleFiles(); return 0; }