Added utilities to work with files
This commit is contained in:
parent
a322e9ea98
commit
65f80b5d2d
@ -139,12 +139,6 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="all.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Feistel.cpp" />
|
||||
<ClCompile Include="FeistelMan.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
|
@ -18,9 +18,6 @@
|
||||
<ClCompile Include="Feistel.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="all.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
|
@ -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<Block> 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<Block> 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;
|
||||
|
@ -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;
|
||||
|
43
Util.h
43
Util.h
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <bitset>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#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<char>(ifs),
|
||||
std::istreambuf_iterator<char>(),
|
||||
std::ostreambuf_iterator<char>(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;
|
||||
}
|
||||
|
27
main.cpp
27
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user