Namespacified
This commit is contained in:
parent
5bcbe09922
commit
70425d94ef
5
Block.h
5
Block.h
@ -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;
|
||||||
|
}
|
||||||
|
@ -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
41
Cipher.h
Normal 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;
|
||||||
|
};
|
||||||
|
}
|
7
Config.h
7
Config.h
@ -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;
|
||||||
|
}
|
||||||
|
32
Feistel.cpp
32
Feistel.cpp
@ -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();
|
||||||
|
15
Feistel.h
15
Feistel.h
@ -3,11 +3,13 @@
|
|||||||
#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
|
||||||
|
*/
|
||||||
|
class Feistel
|
||||||
|
{
|
||||||
|
public:
|
||||||
explicit Feistel(const Block& key);
|
explicit Feistel(const Block& key);
|
||||||
|
|
||||||
Feistel(const Feistel& other) = delete;
|
Feistel(const Feistel& other) = delete;
|
||||||
@ -25,7 +27,7 @@ public:
|
|||||||
//! 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;
|
||||||
|
|
||||||
@ -54,4 +56,5 @@ private:
|
|||||||
void ZeroKeyMemory();
|
void ZeroKeyMemory();
|
||||||
|
|
||||||
Keyset roundKeys;
|
Keyset roundKeys;
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
@ -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">
|
||||||
|
@ -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>
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
@ -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;
|
|
||||||
};
|
|
@ -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);
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
#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
|
||||||
|
*/
|
||||||
|
class GhettoCryptWrapper
|
||||||
|
{
|
||||||
|
public:
|
||||||
//! Will encrypt a string and return it hexadecimally encoded.
|
//! Will encrypt a string and return it hexadecimally encoded.
|
||||||
static std::string EncryptString(const std::string& cleartext, const std::string& password);
|
static std::string EncryptString(const std::string& cleartext, const std::string& password);
|
||||||
|
|
||||||
@ -24,7 +26,8 @@ public:
|
|||||||
//! @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();
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
5
Keyset.h
5
Keyset.h
@ -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;
|
||||||
|
}
|
||||||
|
123
Util.h
123
Util.h
@ -5,16 +5,18 @@
|
|||||||
#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)
|
|
||||||
{
|
{
|
||||||
|
//! Mod-operator that works with negative values
|
||||||
|
inline int Mod(int numerator, int denominator)
|
||||||
|
{
|
||||||
return (denominator + (numerator % denominator)) % denominator;
|
return (denominator + (numerator % denominator)) % denominator;
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will perform a wrapping left-bitshift on a bitset
|
//! Will perform a wrapping left-bitshift on a bitset
|
||||||
template <std::size_t T>
|
template <std::size_t T>
|
||||||
inline std::bitset<T> Shiftl(const std::bitset<T>& bits, std::size_t amount)
|
inline std::bitset<T> Shiftl(const std::bitset<T>& bits, std::size_t amount)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bitss = bits.to_string();
|
const std::string bitss = bits.to_string();
|
||||||
|
|
||||||
@ -22,12 +24,12 @@ inline std::bitset<T> Shiftl(const std::bitset<T>& bits, std::size_t amount)
|
|||||||
ss << bitss[Mod((i + amount), bitss.size())];
|
ss << bitss[Mod((i + amount), bitss.size())];
|
||||||
|
|
||||||
return std::bitset<T>(ss.str());
|
return std::bitset<T>(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will perform a wrapping right-bitshift on a bitset
|
//! Will perform a wrapping right-bitshift on a bitset
|
||||||
template <std::size_t T>
|
template <std::size_t T>
|
||||||
inline std::bitset<T> Shiftr(const std::bitset<T>& bits, std::size_t amount)
|
inline std::bitset<T> Shiftr(const std::bitset<T>& bits, std::size_t amount)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bitss = bits.to_string();
|
const std::string bitss = bits.to_string();
|
||||||
|
|
||||||
@ -35,10 +37,10 @@ inline std::bitset<T> Shiftr(const std::bitset<T>& bits, std::size_t amount)
|
|||||||
ss << bitss[Mod((i - amount), bitss.size())];
|
ss << bitss[Mod((i - amount), bitss.size())];
|
||||||
|
|
||||||
return std::bitset<T>(ss.str());
|
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)
|
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
|
// Fast-reject: Already above padded length
|
||||||
if (str.length() >= len)
|
if (str.length() >= len)
|
||||||
return str;
|
return str;
|
||||||
@ -61,11 +63,11 @@ inline std::string PadStringToLength(const std::string& str, const std::size_t l
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will convert a string to a fixed data block
|
//! Will convert a string to a fixed data block
|
||||||
inline Block StringToBitblock(const std::string& s)
|
inline Block StringToBitblock(const std::string& s)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
for (std::size_t i = 0; i < s.size(); i++)
|
for (std::size_t i = 0; i < s.size(); i++)
|
||||||
@ -73,22 +75,22 @@ inline Block StringToBitblock(const std::string& s)
|
|||||||
|
|
||||||
// Pad rest with zeores
|
// Pad rest with zeores
|
||||||
return Block(PadStringToLength(ss.str(), 128, '0', false));
|
return Block(PadStringToLength(ss.str(), 128, '0', false));
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will convert a string to a flexible data block
|
//! Will convert a string to a flexible data block
|
||||||
inline Flexblock StringToBits(const std::string& s)
|
inline Flexblock StringToBits(const std::string& s)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
for (std::size_t i = 0; i < s.size(); i++)
|
for (std::size_t i = 0; i < s.size(); i++)
|
||||||
ss << std::bitset<8>(s[i]);
|
ss << std::bitset<8>(s[i]);
|
||||||
|
|
||||||
return Flexblock(ss.str());
|
return Flexblock(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will convert a fixed data block to a string
|
//! Will convert a fixed data block to a string
|
||||||
inline std::string BitblockToString(const Block& bits)
|
inline std::string BitblockToString(const Block& bits)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
const std::string bitstring = bits.to_string();
|
const std::string bitstring = bits.to_string();
|
||||||
@ -99,11 +101,11 @@ inline std::string BitblockToString(const Block& bits)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will convert a flexible data block to a string
|
//! Will convert a flexible data block to a string
|
||||||
inline std::string BitsToString(const Flexblock& bits)
|
inline std::string BitsToString(const Flexblock& bits)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
const std::string bitstring = bits;
|
const std::string bitstring = bits;
|
||||||
@ -114,11 +116,11 @@ inline std::string BitsToString(const Flexblock& bits)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Turns a fixed data block into a hex-string
|
//! Turns a fixed data block into a hex-string
|
||||||
inline std::string BitblockToHexstring(const Block& b)
|
inline std::string BitblockToHexstring(const Block& b)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string charset = "0123456789abcdef";
|
const std::string charset = "0123456789abcdef";
|
||||||
const std::string bstr = b.to_string();
|
const std::string bstr = b.to_string();
|
||||||
@ -127,11 +129,11 @@ inline std::string BitblockToHexstring(const Block& b)
|
|||||||
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
|
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Turns a flexible data block into a hex-string
|
//! Turns a flexible data block into a hex-string
|
||||||
inline std::string BitsToHexstring(const Flexblock& b)
|
inline std::string BitsToHexstring(const Flexblock& b)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string charset = "0123456789abcdef";
|
const std::string charset = "0123456789abcdef";
|
||||||
const std::string bstr = b;
|
const std::string bstr = b;
|
||||||
@ -140,15 +142,15 @@ inline std::string BitsToHexstring(const Flexblock& b)
|
|||||||
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
|
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//! Turns a hex string into a fixed data block
|
//! Turns a hex string into a fixed data block
|
||||||
inline Block HexstringToBitblock(const std::string& hexstring)
|
inline Block HexstringToBitblock(const std::string& hexstring)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
for (std::size_t i = 0; i < hexstring.size(); i ++)
|
for (std::size_t i = 0; i < hexstring.size(); i++)
|
||||||
{
|
{
|
||||||
const char c = hexstring[i];
|
const char c = hexstring[i];
|
||||||
|
|
||||||
@ -168,11 +170,11 @@ inline Block HexstringToBitblock(const std::string& hexstring)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Block(ss.str());
|
return Block(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Turns a hex string into a flexible data block
|
//! Turns a hex string into a flexible data block
|
||||||
inline Flexblock HexstringToBits(const std::string& hexstring)
|
inline Flexblock HexstringToBits(const std::string& hexstring)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
for (std::size_t i = 0; i < hexstring.size(); i++)
|
for (std::size_t i = 0; i < hexstring.size(); i++)
|
||||||
@ -195,11 +197,11 @@ inline Flexblock HexstringToBits(const std::string& hexstring)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Creates a key of size key-size from a password of arbitrary length.
|
//! Creates a key of size key-size from a password of arbitrary length.
|
||||||
inline Block PasswordToKey(const std::string& in)
|
inline Block PasswordToKey(const std::string& in)
|
||||||
{
|
{
|
||||||
Block b;
|
Block b;
|
||||||
|
|
||||||
// Segment the password in segments of key-size, and xor them together.
|
// Segment the password in segments of key-size, and xor them together.
|
||||||
@ -207,11 +209,11 @@ inline Block PasswordToKey(const std::string& in)
|
|||||||
b ^= StringToBitblock(in.substr(i, BLOCK_SIZE / 8));
|
b ^= StringToBitblock(in.substr(i, BLOCK_SIZE / 8));
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will read a file into a flexblock
|
//! Will read a file into a flexblock
|
||||||
inline Flexblock ReadFileToBits(const std::string& filepath)
|
inline Flexblock ReadFileToBits(const std::string& filepath)
|
||||||
{
|
{
|
||||||
// Read file
|
// Read file
|
||||||
std::ifstream ifs(filepath, std::ios::binary);
|
std::ifstream ifs(filepath, std::ios::binary);
|
||||||
|
|
||||||
@ -231,11 +233,11 @@ inline Flexblock ReadFileToBits(const std::string& filepath)
|
|||||||
|
|
||||||
// Convert bytes to bits
|
// Convert bytes to bits
|
||||||
return StringToBits(bytes);
|
return StringToBits(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will save bits to a binary file
|
//! Will save bits to a binary file
|
||||||
inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits)
|
inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits)
|
||||||
{
|
{
|
||||||
// Convert bits to bytes
|
// Convert bits to bytes
|
||||||
const std::string bytes = BitsToString(bits);
|
const std::string bytes = BitsToString(bits);
|
||||||
|
|
||||||
@ -249,4 +251,5 @@ inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits)
|
|||||||
ofs.close();
|
ofs.close();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
10
main.cpp
10
main.cpp
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user