Namespacified

This commit is contained in:
Leonetienne 2021-12-06 02:20:47 +01:00
parent 5bcbe09922
commit 70425d94ef
17 changed files with 407 additions and 373 deletions

View File

@ -2,4 +2,7 @@
#include <bitset> #include <bitset>
#include "Config.h" #include "Config.h"
typedef std::bitset<BLOCK_SIZE> Block; namespace GhettoCipher
{
typedef std::bitset<BLOCK_SIZE> Block;
}

View File

@ -1,8 +1,8 @@
#include "GhettoCipher.h" #include "Cipher.h"
#include "Util.h" #include "Util.h"
#include <iostream> #include <iostream>
GhettoCipher::GhettoCipher(const Block& key) GhettoCipher::Cipher::Cipher(const Block& key)
: :
key { key } key { key }
{ {
@ -10,14 +10,14 @@ GhettoCipher::GhettoCipher(const Block& key)
return; return;
} }
GhettoCipher::GhettoCipher(const std::string& password) GhettoCipher::Cipher::Cipher(const std::string& password)
{ {
key = PasswordToKey(password); key = PasswordToKey(password);
return; return;
} }
GhettoCipher::~GhettoCipher() GhettoCipher::Cipher::~Cipher()
{ {
// Clear key memory // Clear key memory
ZeroKeyMemory(); ZeroKeyMemory();
@ -25,7 +25,7 @@ GhettoCipher::~GhettoCipher()
return; return;
} }
void GhettoCipher::SetKey(const Block& key) void GhettoCipher::Cipher::SetKey(const Block& key)
{ {
ZeroKeyMemory(); ZeroKeyMemory();
@ -33,7 +33,7 @@ void GhettoCipher::SetKey(const Block& key)
return; return;
} }
void GhettoCipher::SetPassword(const std::string& password) void GhettoCipher::Cipher::SetPassword(const std::string& password)
{ {
ZeroKeyMemory(); ZeroKeyMemory();
@ -41,7 +41,7 @@ void GhettoCipher::SetPassword(const std::string& password)
return; return;
} }
Flexblock GhettoCipher::Encipher(const Flexblock& data, bool printProgress) const GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const
{ {
// Split cleartext into blocks // Split cleartext into blocks
std::vector<Block> blocks; std::vector<Block> blocks;
@ -73,7 +73,7 @@ Flexblock GhettoCipher::Encipher(const Flexblock& data, bool printProgress) cons
return ss.str(); return ss.str();
} }
Flexblock GhettoCipher::Decipher(const Flexblock& data, bool printProgress) const GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const
{ {
// Split ciphertext into blocks // Split ciphertext into blocks
std::vector<Block> blocks; std::vector<Block> blocks;
@ -112,11 +112,11 @@ Flexblock GhettoCipher::Decipher(const Flexblock& data, bool printProgress) cons
} }
#pragma optimize("", off ) #pragma optimize("", off )
void GhettoCipher::ZeroKeyMemory() void GhettoCipher::Cipher::ZeroKeyMemory()
{ {
key.reset(); key.reset();
return; return;
} }
#pragma optimize("", on ) #pragma optimize("", on )
const Block GhettoCipher::emptyBlock; const GhettoCipher::Block GhettoCipher::Cipher::emptyBlock;

41
Cipher.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include "Feistel.h"
#include "Flexblock.h"
namespace GhettoCipher
{
/** Class to apply a block cipher to messages of arbitrary length in a distributed manner
*/
class Cipher
{
public:
explicit Cipher(const Block& key);
explicit Cipher(const std::string& password);
Cipher(const Cipher& other) = delete;
Cipher(Cipher&& other) noexcept = delete;
~Cipher();
//! Will set the key
void SetKey(const Block& key);
//! Will set the key from a password
void SetPassword(const std::string& password);
//! Will encipher a flexblock of data
Flexblock Encipher(const Flexblock& data, bool printProgress = false) const;
//! Will decipher a flexblock of data
Flexblock Decipher(const Flexblock& data, bool printProgress = false) const;
private:
Block key;
//! Will zero the memory used by the key
void ZeroKeyMemory();
// Initial value for cipher block chaining
static const Block emptyBlock;
};
}

View File

@ -1,4 +1,7 @@
#pragma once #pragma once
#define BLOCK_SIZE 128 namespace GhettoCipher
#define N_ROUNDS 64 {
constexpr int BLOCK_SIZE = 128;
constexpr int N_ROUNDS = 64;
}

View File

@ -2,40 +2,40 @@
#include "Util.h" #include "Util.h"
#include "Config.h" #include "Config.h"
Feistel::Feistel(const Block& key) GhettoCipher::Feistel::Feistel(const Block& key)
{ {
SetKey(key); SetKey(key);
return; return;
} }
Feistel::~Feistel() GhettoCipher::Feistel::~Feistel()
{ {
ZeroKeyMemory(); ZeroKeyMemory();
return; return;
} }
void Feistel::SetKey(const Block& key) void GhettoCipher::Feistel::SetKey(const Block& key)
{ {
GenerateRoundKeys(key); GenerateRoundKeys(key);
return; return;
} }
Block Feistel::Encipher(const Block& data) const GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) const
{ {
return Run(data, false); return Run(data, false);
} }
Block Feistel::Decipher(const Block& data) const GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) const
{ {
return Run(data, true); return Run(data, true);
} }
Block Feistel::Run(const Block& data, bool reverseKeys) const GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys) const
{ {
const auto splitData = FeistelSplit(data); const auto splitData = FeistelSplit(data);
Halfblock l = splitData.first; GhettoCipher::Halfblock l = splitData.first;
Halfblock r = splitData.second; GhettoCipher::Halfblock r = splitData.second;
Halfblock tmp; Halfblock tmp;
@ -57,7 +57,7 @@ Block Feistel::Run(const Block& data, bool reverseKeys) const
return FeistelCombine(r, l); return FeistelCombine(r, l);
} }
Halfblock Feistel::F(Halfblock m, const Block& key) GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key)
{ {
// Made-up F function // Made-up F function
@ -85,7 +85,7 @@ Halfblock Feistel::F(Halfblock m, const Block& key)
return CompressionFunction(m_expanded); return CompressionFunction(m_expanded);
} }
std::pair<Halfblock, Halfblock> Feistel::FeistelSplit(const Block& block) std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feistel::FeistelSplit(const Block& block)
{ {
const std::string bits = block.to_string(); const std::string bits = block.to_string();
@ -95,12 +95,12 @@ std::pair<Halfblock, Halfblock> Feistel::FeistelSplit(const Block& block)
return std::make_pair(l, r); return std::make_pair(l, r);
} }
Block Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r)
{ {
return Block(l.to_string() + r.to_string()); return Block(l.to_string() + r.to_string());
} }
Block Feistel::ExpansionFunction(const Halfblock& block) GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block)
{ {
std::stringstream ss; std::stringstream ss;
const std::string bits = block.to_string(); const std::string bits = block.to_string();
@ -120,7 +120,7 @@ Block Feistel::ExpansionFunction(const Halfblock& block)
return Block(ss.str()); return Block(ss.str());
} }
Halfblock Feistel::CompressionFunction(const Block& block) GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block)
{ {
std::stringstream ss; std::stringstream ss;
const std::string bits = block.to_string(); const std::string bits = block.to_string();
@ -151,7 +151,7 @@ Halfblock Feistel::CompressionFunction(const Block& block)
return Halfblock(ss.str()); return Halfblock(ss.str());
} }
std::string Feistel::SBox(const std::string& in) std::string GhettoCipher::Feistel::SBox(const std::string& in)
{ {
if (in == "0000") return "1100"; if (in == "0000") return "1100";
else if (in == "0001") return "1000"; else if (in == "0001") return "1000";
@ -171,7 +171,7 @@ std::string Feistel::SBox(const std::string& in)
else /*if (in == "1111")*/ return "0110"; else /*if (in == "1111")*/ return "0110";
} }
void Feistel::GenerateRoundKeys(const Block& seedKey) void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
{ {
// Generate round keys via output feedback modus (OFM) method // Generate round keys via output feedback modus (OFM) method
@ -193,7 +193,7 @@ void Feistel::GenerateRoundKeys(const Block& seedKey)
// These pragmas only work for MSVC, as far as i know. Beware!!! // These pragmas only work for MSVC, as far as i know. Beware!!!
#pragma optimize("", off ) #pragma optimize("", off )
void Feistel::ZeroKeyMemory() void GhettoCipher::Feistel::ZeroKeyMemory()
{ {
for (Block& key : roundKeys) for (Block& key : roundKeys)
key.reset(); key.reset();

View File

@ -3,55 +3,58 @@
#include "Block.h" #include "Block.h"
#include "Halfblock.h" #include "Halfblock.h"
/** Class to perform a feistel block chipher namespace GhettoCipher
*/
class Feistel
{ {
public: /** Class to perform a feistel block chipher
explicit Feistel(const Block& key); */
class Feistel
{
public:
explicit Feistel(const Block& key);
Feistel(const Feistel& other) = delete; Feistel(const Feistel& other) = delete;
Feistel(Feistel&& other) noexcept = delete; Feistel(Feistel&& other) noexcept = delete;
~Feistel(); ~Feistel();
//! Will set the seed-key for this feistel network. //! Will set the seed-key for this feistel network.
//! Roundkeys will be derived from this. //! Roundkeys will be derived from this.
void SetKey(const Block& key); void SetKey(const Block& key);
//! Will encipher a data block via the set seed-key //! Will encipher a data block via the set seed-key
Block Encipher(const Block& data) const; Block Encipher(const Block& data) const;
//! Will decipher a data block via the set seed-key //! Will decipher a data block via the set seed-key
Block Decipher(const Block& data) const; Block Decipher(const Block& data) const;
private: private:
//! Will run the feistel rounds, with either regular key order or reversed key order //! Will run the feistel rounds, with either regular key order or reversed key order
Block Run(const Block& data, bool reverseKeys) const; Block Run(const Block& data, bool reverseKeys) const;
//! Arbitrary cipher function //! Arbitrary cipher function
static Halfblock F(Halfblock m, const Block& key); static Halfblock F(Halfblock m, const Block& key);
//! Split a data block into two half blocks (into L and R) //! Split a data block into two half blocks (into L and R)
static std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block); static std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block);
//! Combine two half blocks (L and R) into a regular data block //! Combine two half blocks (L and R) into a regular data block
static Block FeistelCombine(const Halfblock& l, const Halfblock& r); static Block FeistelCombine(const Halfblock& l, const Halfblock& r);
//! Will expand a halfblock to a fullblock //! Will expand a halfblock to a fullblock
static Block ExpansionFunction(const Halfblock& block); static Block ExpansionFunction(const Halfblock& block);
//! Will compress a fullblock to a halfblock //! Will compress a fullblock to a halfblock
static Halfblock CompressionFunction(const Block& block); static Halfblock CompressionFunction(const Block& block);
//! Substitutes four bits by static random others //! Substitutes four bits by static random others
static std::string SBox(const std::string& in); static std::string SBox(const std::string& in);
//! Will generate a the round keys //! Will generate a the round keys
void GenerateRoundKeys(const Block& seedKey); void GenerateRoundKeys(const Block& seedKey);
//! Will zero the memory used by the keyset //! Will zero the memory used by the keyset
void ZeroKeyMemory(); void ZeroKeyMemory();
Keyset roundKeys; Keyset roundKeys;
}; };
}

View File

@ -141,7 +141,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="Feistel.cpp" /> <ClCompile Include="Feistel.cpp" />
<ClCompile Include="GhettoCipherWrapper.cpp" /> <ClCompile Include="GhettoCipherWrapper.cpp" />
<ClCompile Include="GhettoCipher.cpp" /> <ClCompile Include="Cipher.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -149,11 +149,12 @@
<ClInclude Include="Config.h" /> <ClInclude Include="Config.h" />
<ClInclude Include="Feistel.h" /> <ClInclude Include="Feistel.h" />
<ClInclude Include="GhettoCipherWrapper.h" /> <ClInclude Include="GhettoCipherWrapper.h" />
<ClInclude Include="GhettoCipher.h" /> <ClInclude Include="Cipher.h" />
<ClInclude Include="Flexblock.h" /> <ClInclude Include="Flexblock.h" />
<ClInclude Include="Halfblock.h" /> <ClInclude Include="Halfblock.h" />
<ClInclude Include="Keyset.h" /> <ClInclude Include="Keyset.h" />
<ClInclude Include="Util.h" /> <ClInclude Include="Util.h" />
<ClInclude Include="Version.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -21,10 +21,10 @@
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="GhettoCipher.cpp"> <ClCompile Include="GhettoCipherWrapper.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="GhettoCipherWrapper.cpp"> <ClCompile Include="Cipher.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -50,10 +50,13 @@
<ClInclude Include="Flexblock.h"> <ClInclude Include="Flexblock.h">
<Filter>Headerdateien</Filter> <Filter>Headerdateien</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="GhettoCipher.h"> <ClInclude Include="GhettoCipherWrapper.h">
<Filter>Headerdateien</Filter> <Filter>Headerdateien</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="GhettoCipherWrapper.h"> <ClInclude Include="Version.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Cipher.h">
<Filter>Headerdateien</Filter> <Filter>Headerdateien</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>

View File

@ -1,5 +1,8 @@
#pragma once #pragma once
#include <vector> #include <vector>
//! A "bitset" of variable length namespace GhettoCipher
typedef std::string Flexblock; {
//! A "bitset" of variable length
typedef std::string Flexblock;
}

View File

@ -1,38 +0,0 @@
#pragma once
#include "Feistel.h"
#include "Flexblock.h"
/** Class to apply a block cipher to messages of arbitrary length in a distributed manner
*/
class GhettoCipher
{
public:
explicit GhettoCipher(const Block& key);
explicit GhettoCipher(const std::string& password);
GhettoCipher(const GhettoCipher& other) = delete;
GhettoCipher(GhettoCipher&& other) noexcept = delete;
~GhettoCipher();
//! Will set the key
void SetKey(const Block& key);
//! Will set the key from a password
void SetPassword(const std::string& password);
//! Will encipher a flexblock of data
Flexblock Encipher(const Flexblock& data, bool printProgress = false) const;
//! Will decipher a flexblock of data
Flexblock Decipher(const Flexblock& data, bool printProgress = false) const;
private:
Block key;
//! Will zero the memory used by the key
void ZeroKeyMemory();
// Initial value for cipher block chaining
static const Block emptyBlock;
};

View File

@ -1,11 +1,11 @@
#include "GhettoCipherWrapper.h" #include "GhettoCipherWrapper.h"
#include "GhettoCipher.h" #include "Cipher.h"
#include "Util.h" #include "Util.h"
std::string GhettoCipherWrapper::EncryptString(const std::string& cleartext, const std::string& password) std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password)
{ {
// Instanciate our cipher and supply a key // Instanciate our cipher and supply a key
GhettoCipher cipher(password); Cipher cipher(password);
// Recode the ascii-string to bits // Recode the ascii-string to bits
const Flexblock cleartext_bits = StringToBits(cleartext); const Flexblock cleartext_bits = StringToBits(cleartext);
@ -20,10 +20,10 @@ std::string GhettoCipherWrapper::EncryptString(const std::string& cleartext, con
return ciphertext; return ciphertext;
} }
std::string GhettoCipherWrapper::DecryptString(const std::string& ciphertext, const std::string& password) std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password)
{ {
// Instanciate our cipher and supply a key // Instanciate our cipher and supply a key
GhettoCipher cipher(password); Cipher cipher(password);
// Recode the hex-string to bits // Recode the hex-string to bits
const Flexblock ciphertext_bits = HexstringToBits(ciphertext); const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
@ -38,7 +38,7 @@ std::string GhettoCipherWrapper::DecryptString(const std::string& ciphertext, co
return cleartext; return cleartext;
} }
bool GhettoCipherWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password) bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password)
{ {
try try
{ {
@ -46,7 +46,7 @@ bool GhettoCipherWrapper::EncryptFile(const std::string& filename_in, const std:
const Flexblock cleartext_bits = ReadFileToBits(filename_in); const Flexblock cleartext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key // Instanciate our cipher and supply a key
GhettoCipher cipher(password); Cipher cipher(password);
// Encrypt our cleartext bits // Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits); const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
@ -62,7 +62,7 @@ bool GhettoCipherWrapper::EncryptFile(const std::string& filename_in, const std:
} }
} }
bool GhettoCipherWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password) bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password)
{ {
try try
{ {
@ -70,7 +70,7 @@ bool GhettoCipherWrapper::DecryptFile(const std::string& filename_in, const std:
const Flexblock ciphertext_bits = ReadFileToBits(filename_in); const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key // Instanciate our cipher and supply a key
GhettoCipher cipher(password); Cipher cipher(password);
// Decrypt the ciphertext bits // Decrypt the ciphertext bits
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits); const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits);

View File

@ -1,30 +1,33 @@
#pragma once #pragma once
#include <string> #include <string>
/** This class is a wrapper to make working with the GhettoCipher super easy with a python-like syntax namespace GhettoCipher
*/
class GhettoCipherWrapper
{ {
public: /** This class is a wrapper to make working with the GhettoCipher super easy with a python-like syntax
//! Will encrypt a string and return it hexadecimally encoded. */
static std::string EncryptString(const std::string& cleartext, const std::string& password); class GhettoCryptWrapper
{
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. //! Will decrypt a hexadecimally encoded string.
static std::string DecryptString(const std::string& ciphertext, const std::string& password); static std::string DecryptString(const std::string& ciphertext, const std::string& password);
//! Will encrypt a file. //! Will encrypt a file.
//! Returns false if anything goes wrong (like, file-access). //! Returns false if anything goes wrong (like, file-access).
//! @filename_in The file to be read. //! @filename_in The file to be read.
//! @filename_out The file the encrypted version should be saved in. //! @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); static bool EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password);
//! Will decrypt a file. //! Will decrypt a file.
//! Returns false if anything goes wrong (like, file-access). //! Returns false if anything goes wrong (like, file-access).
//! @filename_in The file to be read. //! @filename_in The file to be read.
//! @filename_out The file the decrypted version should be saved in. //! @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); static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password);
private: private:
// No instanciation! >:( // No instanciation! >:(
GhettoCipherWrapper(); GhettoCryptWrapper();
}; };
}

View File

@ -2,6 +2,8 @@
#include <bitset> #include <bitset>
#include "Config.h" #include "Config.h"
#define HALFBLOCK_SIZE (BLOCK_SIZE / 2) namespace GhettoCipher
{
typedef std::bitset<HALFBLOCK_SIZE> Halfblock; constexpr int HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
}

View File

@ -3,4 +3,7 @@
#include "Block.h" #include "Block.h"
#include "Config.h" #include "Config.h"
typedef std::array<Block, N_ROUNDS> Keyset; namespace GhettoCipher
{
typedef std::array<Block, N_ROUNDS> Keyset;
}

449
Util.h
View File

@ -5,248 +5,251 @@
#include "Block.h" #include "Block.h"
#include "Flexblock.h" #include "Flexblock.h"
//! Mod-operator that works with negative values namespace GhettoCipher
inline int Mod(int numerator, int denominator)
{ {
return (denominator + (numerator % denominator)) % denominator; //! Mod-operator that works with negative values
} inline int Mod(int numerator, int denominator)
//! Will perform a wrapping left-bitshift on a bitset
template <std::size_t T>
inline std::bitset<T> Shiftl(const std::bitset<T>& bits, std::size_t amount)
{
std::stringstream ss;
const std::string bitss = bits.to_string();
for (std::size_t i = 0; i < bitss.size(); i++)
ss << bitss[Mod((i + amount), bitss.size())];
return std::bitset<T>(ss.str());
}
//! Will perform a wrapping right-bitshift on a bitset
template <std::size_t T>
inline std::bitset<T> Shiftr(const std::bitset<T>& bits, std::size_t amount)
{
std::stringstream ss;
const std::string bitss = bits.to_string();
for (std::size_t i = 0; i < bitss.size(); i++)
ss << bitss[Mod((i - amount), bitss.size())];
return std::bitset<T>(ss.str());
}
inline std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true)
{
// Fast-reject: Already above padded length
if (str.length() >= len)
return str;
std::stringstream ss;
// Pad left:
if (padLeft)
{ {
for (std::size_t i = 0; i < len - str.size(); i++) return (denominator + (numerator % denominator)) % denominator;
ss << pad;
ss << str;
}
// Pad right:
else
{
ss << str;
for (std::size_t i = 0; i < len - str.size(); i++)
ss << pad;
} }
return ss.str(); //! Will perform a wrapping left-bitshift on a bitset
} template <std::size_t T>
inline std::bitset<T> Shiftl(const std::bitset<T>& bits, std::size_t amount)
//! Will convert a string to a fixed data block
inline Block StringToBitblock(const std::string& s)
{
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++)
ss << std::bitset<8>(s[i]);
// Pad rest with zeores
return Block(PadStringToLength(ss.str(), 128, '0', false));
}
//! Will convert a string to a flexible data block
inline Flexblock StringToBits(const std::string& s)
{
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++)
ss << std::bitset<8>(s[i]);
return Flexblock(ss.str());
}
//! Will convert a fixed data block to a string
inline std::string BitblockToString(const Block& bits)
{
std::stringstream ss;
const std::string bitstring = bits.to_string();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 8)
{ {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong(); std::stringstream ss;
const std::string bitss = bits.to_string();
for (std::size_t i = 0; i < bitss.size(); i++)
ss << bitss[Mod((i + amount), bitss.size())];
return std::bitset<T>(ss.str());
} }
return ss.str(); //! Will perform a wrapping right-bitshift on a bitset
} template <std::size_t T>
inline std::bitset<T> Shiftr(const std::bitset<T>& bits, std::size_t amount)
//! Will convert a flexible data block to a string
inline std::string BitsToString(const Flexblock& bits)
{
std::stringstream ss;
const std::string bitstring = bits;
for (std::size_t i = 0; i < bits.size(); i += 8)
{ {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong(); std::stringstream ss;
const std::string bitss = bits.to_string();
for (std::size_t i = 0; i < bitss.size(); i++)
ss << bitss[Mod((i - amount), bitss.size())];
return std::bitset<T>(ss.str());
} }
return ss.str(); inline std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true)
}
//! Turns a fixed data block into a hex-string
inline std::string BitblockToHexstring(const Block& b)
{
std::stringstream ss;
const std::string charset = "0123456789abcdef";
const std::string bstr = b.to_string();
for (std::size_t i = 0; i < bstr.size(); i += 4)
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
return ss.str();
}
//! Turns a flexible data block into a hex-string
inline std::string BitsToHexstring(const Flexblock& b)
{
std::stringstream ss;
const std::string charset = "0123456789abcdef";
const std::string bstr = b;
for (std::size_t i = 0; i < bstr.size(); i += 4)
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
return ss.str();
}
//! Turns a hex string into a fixed data block
inline Block HexstringToBitblock(const std::string& hexstring)
{
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i ++)
{ {
const char c = hexstring[i]; // Fast-reject: Already above padded length
if (str.length() >= len)
return str;
// Get value std::stringstream ss;
std::size_t value;
if ((c >= '0') && (c <= '9')) // Pad left:
// Is it a number? if (padLeft)
value = (c - '0') + 0; {
else if ((c >= 'a') && (c <= 'f')) for (std::size_t i = 0; i < len - str.size(); i++)
// Else, it is a lowercase letter ss << pad;
value = (c - 'a') + 10; ss << str;
}
// Pad right:
else else
throw std::logic_error("non-hex string detected in HexstringToBits()"); {
ss << str;
for (std::size_t i = 0; i < len - str.size(); i++)
ss << pad;
}
// Append to our bits return ss.str();
ss << std::bitset<4>(value);
} }
return Block(ss.str()); //! Will convert a string to a fixed data block
} inline Block StringToBitblock(const std::string& s)
//! Turns a hex string into a flexible data block
inline Flexblock HexstringToBits(const std::string& hexstring)
{
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i++)
{ {
const char c = hexstring[i]; std::stringstream ss;
// Get value for (std::size_t i = 0; i < s.size(); i++)
std::size_t value; ss << std::bitset<8>(s[i]);
if ((c >= '0') && (c <= '9'))
// Is it a number?
value = (c - '0') + 0;
else if ((c >= 'a') && (c <= 'f'))
// Else, it is a lowercase letter
value = (c - 'a') + 10;
else
throw std::logic_error("non-hex string detected in HexstringToBits()");
// Append to our bits // Pad rest with zeores
ss << std::bitset<4>(value); return Block(PadStringToLength(ss.str(), 128, '0', false));
} }
return ss.str(); //! Will convert a string to a flexible data block
} inline Flexblock StringToBits(const std::string& s)
{
//! Creates a key of size key-size from a password of arbitrary length. std::stringstream ss;
inline Block PasswordToKey(const std::string& in)
{ for (std::size_t i = 0; i < s.size(); i++)
Block b; ss << std::bitset<8>(s[i]);
// Segment the password in segments of key-size, and xor them together. return Flexblock(ss.str());
for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE / 8) }
b ^= StringToBitblock(in.substr(i, BLOCK_SIZE / 8));
//! Will convert a fixed data block to a string
return b; inline std::string BitblockToString(const Block& bits)
} {
std::stringstream ss;
//! Will read a file into a flexblock
inline Flexblock ReadFileToBits(const std::string& filepath) const std::string bitstring = bits.to_string();
{
// Read file for (std::size_t i = 0; i < BLOCK_SIZE; i += 8)
std::ifstream ifs(filepath, std::ios::binary); {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
if (!ifs.good()) }
throw std::runtime_error("Unable to open ifilestream!");
return ss.str();
std::stringstream ss; }
std::copy(
std::istreambuf_iterator<char>(ifs), //! Will convert a flexible data block to a string
std::istreambuf_iterator<char>(), inline std::string BitsToString(const Flexblock& bits)
std::ostreambuf_iterator<char>(ss) {
); std::stringstream ss;
ifs.close(); const std::string bitstring = bits;
const std::string bytes = ss.str(); for (std::size_t i = 0; i < bits.size(); i += 8)
{
// Convert bytes to bits ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
return StringToBits(bytes); }
}
return ss.str();
//! Will save bits to a binary file }
inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits)
{ //! Turns a fixed data block into a hex-string
// Convert bits to bytes inline std::string BitblockToHexstring(const Block& b)
const std::string bytes = BitsToString(bits); {
std::stringstream ss;
// Write bits to file const std::string charset = "0123456789abcdef";
std::ofstream ofs(filepath, std::ios::binary); const std::string bstr = b.to_string();
if (!ofs.good()) for (std::size_t i = 0; i < bstr.size(); i += 4)
throw std::runtime_error("Unable to open ofilestream!"); ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
ofs.write(bytes.data(), bytes.length()); return ss.str();
ofs.close(); }
return; //! Turns a flexible data block into a hex-string
inline std::string BitsToHexstring(const Flexblock& b)
{
std::stringstream ss;
const std::string charset = "0123456789abcdef";
const std::string bstr = b;
for (std::size_t i = 0; i < bstr.size(); i += 4)
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
return ss.str();
}
//! Turns a hex string into a fixed data block
inline Block HexstringToBitblock(const std::string& hexstring)
{
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i++)
{
const char c = hexstring[i];
// Get value
std::size_t value;
if ((c >= '0') && (c <= '9'))
// Is it a number?
value = (c - '0') + 0;
else if ((c >= 'a') && (c <= 'f'))
// Else, it is a lowercase letter
value = (c - 'a') + 10;
else
throw std::logic_error("non-hex string detected in HexstringToBits()");
// Append to our bits
ss << std::bitset<4>(value);
}
return Block(ss.str());
}
//! Turns a hex string into a flexible data block
inline Flexblock HexstringToBits(const std::string& hexstring)
{
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i++)
{
const char c = hexstring[i];
// Get value
std::size_t value;
if ((c >= '0') && (c <= '9'))
// Is it a number?
value = (c - '0') + 0;
else if ((c >= 'a') && (c <= 'f'))
// Else, it is a lowercase letter
value = (c - 'a') + 10;
else
throw std::logic_error("non-hex string detected in HexstringToBits()");
// Append to our bits
ss << std::bitset<4>(value);
}
return ss.str();
}
//! Creates a key of size key-size from a password of arbitrary length.
inline Block PasswordToKey(const std::string& in)
{
Block b;
// Segment the password in segments of key-size, and xor them together.
for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE / 8)
b ^= StringToBitblock(in.substr(i, BLOCK_SIZE / 8));
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;
}
} }

2
Version.h Normal file
View File

@ -0,0 +1,2 @@
#pragma once
#define GHETTOCRYPT_VERSION 0.1

View File

@ -3,6 +3,8 @@
#include "Util.h" #include "Util.h"
#include "GhettoCipherWrapper.h" #include "GhettoCipherWrapper.h"
using namespace GhettoCipher;
void ExampleString() void ExampleString()
{ {
std::cout << "Example on how to encrypt & decrypt a string:" << std::endl; std::cout << "Example on how to encrypt & decrypt a string:" << std::endl;
@ -12,11 +14,11 @@ void ExampleString()
std::cout << input << std::endl; std::cout << input << std::endl;
// Encrypt // Encrypt
const std::string encrypted = GhettoCipherWrapper::EncryptString(input, "password1"); const std::string encrypted = GhettoCryptWrapper::EncryptString(input, "password1");
std::cout << encrypted << std::endl; std::cout << encrypted << std::endl;
// Decrypt // Decrypt
const std::string decrypted = GhettoCipherWrapper::DecryptString(encrypted, "password1"); const std::string decrypted = GhettoCryptWrapper::DecryptString(encrypted, "password1");
std::cout << decrypted << std::endl; std::cout << decrypted << std::endl;
return; return;
@ -27,10 +29,10 @@ void ExampleFiles()
std::cout << "Example on how to encrypt & decrypt any file:" << std::endl; std::cout << "Example on how to encrypt & decrypt any file:" << std::endl;
// Encrypt // Encrypt
GhettoCipherWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1"); GhettoCryptWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1");
// Decrypt // Decrypt
GhettoCipherWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1"); GhettoCryptWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1");
return; return;
} }