From 65f80b5d2d958612aaefa7ae899acf410731a129 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Mon, 6 Dec 2021 01:33:55 +0100 Subject: [PATCH] Added utilities to work with files --- Feistel.vcxproj | 6 ------ Feistel.vcxproj.filters | 3 --- FeistelMan.cpp | 12 ++++++++++-- FeistelMan.h | 4 ++-- Util.h | 43 +++++++++++++++++++++++++++++++++++++++++ main.cpp | 27 ++++++++++++++++++-------- 6 files changed, 74 insertions(+), 21 deletions(-) diff --git a/Feistel.vcxproj b/Feistel.vcxproj index ce3f24a..850438c 100644 --- a/Feistel.vcxproj +++ b/Feistel.vcxproj @@ -139,12 +139,6 @@ - - true - true - true - true - diff --git a/Feistel.vcxproj.filters b/Feistel.vcxproj.filters index dc966f1..125b2e7 100644 --- a/Feistel.vcxproj.filters +++ b/Feistel.vcxproj.filters @@ -18,9 +18,6 @@ Quelldateien - - Quelldateien - Quelldateien diff --git a/FeistelMan.cpp b/FeistelMan.cpp index a24631b..1cd09cb 100644 --- a/FeistelMan.cpp +++ b/FeistelMan.cpp @@ -41,7 +41,7 @@ void FeistelMan::SetPassword(const std::string& password) return; } -Flexblock FeistelMan::Encipher(const Flexblock& data) const +Flexblock FeistelMan::Encipher(const Flexblock& data, bool printReports) const { // Split cleartext into blocks std::vector blocks; @@ -56,6 +56,10 @@ Flexblock FeistelMan::Encipher(const Flexblock& data) const for (std::size_t i = 0; i < blocks.size(); i++) { + // Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block. + if ((i % ((blocks.size() > 1000)? 100 : 10) == 0) && (printReports)) + std::cout << "Encrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100 / blocks.size()) << "\%)" << std::endl; + const Block& lastBlock = (i>0) ? blocks[i-1] : emptyBlock; blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock); } @@ -69,7 +73,7 @@ Flexblock FeistelMan::Encipher(const Flexblock& data) const return ss.str(); } -Flexblock FeistelMan::Decipher(const Flexblock& data) const +Flexblock FeistelMan::Decipher(const Flexblock& data, bool printReports) const { // Split ciphertext into blocks std::vector blocks; @@ -87,6 +91,10 @@ Flexblock FeistelMan::Decipher(const Flexblock& data) const for (std::size_t i = 0; i < blocks.size(); i++) { + // Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block. + if ((i % ((blocks.size() > 1000) ? 100 : 10) == 0) && (printReports)) + std::cout << "Decrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100/ blocks.size()) << "\%)" << std::endl; + Block tmpCopy = blocks[i]; blocks[i] = feistel.Decipher(blocks[i]) ^ lastBlock; diff --git a/FeistelMan.h b/FeistelMan.h index ad242ff..dd4893f 100644 --- a/FeistelMan.h +++ b/FeistelMan.h @@ -22,10 +22,10 @@ public: void SetPassword(const std::string& password); //! Will encipher a flexblock of data - Flexblock Encipher(const Flexblock& data) const; + Flexblock Encipher(const Flexblock& data, bool printReports = false) const; //! Will decipher a flexblock of data - Flexblock Decipher(const Flexblock& data) const; + Flexblock Decipher(const Flexblock& data, bool printReports = false) const; private: Block key; diff --git a/Util.h b/Util.h index cb09e28..ca0db72 100644 --- a/Util.h +++ b/Util.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include "Block.h" #include "Flexblock.h" @@ -207,3 +208,45 @@ inline Block PasswordToKey(const std::string& in) return b; } + +//! Will read a file into a flexblock +inline Flexblock ReadFileToBits(const std::string& filepath) +{ + // Read file + std::ifstream ifs(filepath, std::ios::binary); + + if (!ifs.good()) + throw std::runtime_error("Unable to open ifilestream!"); + + std::stringstream ss; + std::copy( + std::istreambuf_iterator(ifs), + std::istreambuf_iterator(), + std::ostreambuf_iterator(ss) + ); + + ifs.close(); + + const std::string bytes = ss.str(); + + // Convert bytes to bits + return StringToBits(bytes); +} + +//! Will save bits to a binary file +inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits) +{ + // Convert bits to bytes + const std::string bytes = BitsToString(bits); + + // Write bits to file + std::ofstream ofs(filepath, std::ios::binary); + + if (!ofs.good()) + throw std::runtime_error("Unable to open ofilestream!"); + + ofs.write(bytes.data(), bytes.length()); + ofs.close(); + + return; +} diff --git a/main.cpp b/main.cpp index f054497..5d44a1b 100644 --- a/main.cpp +++ b/main.cpp @@ -5,23 +5,34 @@ int main() { + // Load file to be encrypted + const Flexblock file = ReadFileToBits("images.jpg"); + std::cout << "Finished reading..." << std::endl; + + + // Prepare cipher FeistelMan feistel("Password yo"); - const std::string message = "I am a veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery long message!"; - - std::cout << "Cleartext: " << message << std::endl; // Encrypt - const Flexblock ciphertext = feistel.Encipher(StringToBits(message)); - const std::string cipherHex = BitsToHexstring(ciphertext); + const Flexblock ciphertext = feistel.Encipher(file, true); + //const std::string cipherHex = BitsToHexstring(ciphertext); + std::cout << "Finished encrypting..." << std::endl; - std::cout << "Ciphertext: " << cipherHex << std::endl; + // Save encrypted file + WriteBitsToFile("encrypted.jpg", ciphertext); + std::cout << "Finished writing..." << std::endl; // Decrypt - const Flexblock cleartextBits = feistel.Decipher(HexstringToBits(cipherHex)); - std::cout << "Decrypted: " << BitsToString(cleartextBits) << std::endl; + //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; return 0; }