Namespacified
This commit is contained in:
parent
5bcbe09922
commit
70425d94ef
3
Block.h
3
Block.h
@ -2,4 +2,7 @@
|
||||
#include <bitset>
|
||||
#include "Config.h"
|
||||
|
||||
namespace GhettoCipher
|
||||
{
|
||||
typedef std::bitset<BLOCK_SIZE> Block;
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
#include "GhettoCipher.h"
|
||||
#include "Cipher.h"
|
||||
#include "Util.h"
|
||||
#include <iostream>
|
||||
|
||||
GhettoCipher::GhettoCipher(const Block& key)
|
||||
GhettoCipher::Cipher::Cipher(const Block& key)
|
||||
:
|
||||
key { key }
|
||||
{
|
||||
@ -10,14 +10,14 @@ GhettoCipher::GhettoCipher(const Block& key)
|
||||
return;
|
||||
}
|
||||
|
||||
GhettoCipher::GhettoCipher(const std::string& password)
|
||||
GhettoCipher::Cipher::Cipher(const std::string& password)
|
||||
{
|
||||
key = PasswordToKey(password);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
GhettoCipher::~GhettoCipher()
|
||||
GhettoCipher::Cipher::~Cipher()
|
||||
{
|
||||
// Clear key memory
|
||||
ZeroKeyMemory();
|
||||
@ -25,7 +25,7 @@ GhettoCipher::~GhettoCipher()
|
||||
return;
|
||||
}
|
||||
|
||||
void GhettoCipher::SetKey(const Block& key)
|
||||
void GhettoCipher::Cipher::SetKey(const Block& key)
|
||||
{
|
||||
ZeroKeyMemory();
|
||||
|
||||
@ -33,7 +33,7 @@ void GhettoCipher::SetKey(const Block& key)
|
||||
return;
|
||||
}
|
||||
|
||||
void GhettoCipher::SetPassword(const std::string& password)
|
||||
void GhettoCipher::Cipher::SetPassword(const std::string& password)
|
||||
{
|
||||
ZeroKeyMemory();
|
||||
|
||||
@ -41,7 +41,7 @@ void GhettoCipher::SetPassword(const std::string& password)
|
||||
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
|
||||
std::vector<Block> blocks;
|
||||
@ -73,7 +73,7 @@ Flexblock GhettoCipher::Encipher(const Flexblock& data, bool printProgress) cons
|
||||
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
|
||||
std::vector<Block> blocks;
|
||||
@ -112,11 +112,11 @@ Flexblock GhettoCipher::Decipher(const Flexblock& data, bool printProgress) cons
|
||||
}
|
||||
|
||||
#pragma optimize("", off )
|
||||
void GhettoCipher::ZeroKeyMemory()
|
||||
void GhettoCipher::Cipher::ZeroKeyMemory()
|
||||
{
|
||||
key.reset();
|
||||
return;
|
||||
}
|
||||
#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
|
||||
|
||||
#define BLOCK_SIZE 128
|
||||
#define N_ROUNDS 64
|
||||
namespace GhettoCipher
|
||||
{
|
||||
constexpr int BLOCK_SIZE = 128;
|
||||
constexpr int N_ROUNDS = 64;
|
||||
}
|
||||
|
32
Feistel.cpp
32
Feistel.cpp
@ -2,40 +2,40 @@
|
||||
#include "Util.h"
|
||||
#include "Config.h"
|
||||
|
||||
Feistel::Feistel(const Block& key)
|
||||
GhettoCipher::Feistel::Feistel(const Block& key)
|
||||
{
|
||||
SetKey(key);
|
||||
return;
|
||||
}
|
||||
|
||||
Feistel::~Feistel()
|
||||
GhettoCipher::Feistel::~Feistel()
|
||||
{
|
||||
ZeroKeyMemory();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Feistel::SetKey(const Block& key)
|
||||
void GhettoCipher::Feistel::SetKey(const Block& key)
|
||||
{
|
||||
GenerateRoundKeys(key);
|
||||
return;
|
||||
}
|
||||
|
||||
Block Feistel::Encipher(const Block& data) const
|
||||
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) const
|
||||
{
|
||||
return Run(data, false);
|
||||
}
|
||||
|
||||
Block Feistel::Decipher(const Block& data) const
|
||||
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) const
|
||||
{
|
||||
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);
|
||||
Halfblock l = splitData.first;
|
||||
Halfblock r = splitData.second;
|
||||
GhettoCipher::Halfblock l = splitData.first;
|
||||
GhettoCipher::Halfblock r = splitData.second;
|
||||
|
||||
Halfblock tmp;
|
||||
|
||||
@ -57,7 +57,7 @@ Block Feistel::Run(const Block& data, bool reverseKeys) const
|
||||
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
|
||||
|
||||
@ -85,7 +85,7 @@ Halfblock Feistel::F(Halfblock m, const Block& key)
|
||||
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();
|
||||
|
||||
@ -95,12 +95,12 @@ std::pair<Halfblock, Halfblock> Feistel::FeistelSplit(const Block& block)
|
||||
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());
|
||||
}
|
||||
|
||||
Block Feistel::ExpansionFunction(const Halfblock& block)
|
||||
GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block)
|
||||
{
|
||||
std::stringstream ss;
|
||||
const std::string bits = block.to_string();
|
||||
@ -120,7 +120,7 @@ Block Feistel::ExpansionFunction(const Halfblock& block)
|
||||
return Block(ss.str());
|
||||
}
|
||||
|
||||
Halfblock Feistel::CompressionFunction(const Block& block)
|
||||
GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block)
|
||||
{
|
||||
std::stringstream ss;
|
||||
const std::string bits = block.to_string();
|
||||
@ -151,7 +151,7 @@ Halfblock Feistel::CompressionFunction(const Block& block)
|
||||
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";
|
||||
else if (in == "0001") return "1000";
|
||||
@ -171,7 +171,7 @@ std::string Feistel::SBox(const std::string& in)
|
||||
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
|
||||
|
||||
@ -193,7 +193,7 @@ void Feistel::GenerateRoundKeys(const Block& seedKey)
|
||||
|
||||
// These pragmas only work for MSVC, as far as i know. Beware!!!
|
||||
#pragma optimize("", off )
|
||||
void Feistel::ZeroKeyMemory()
|
||||
void GhettoCipher::Feistel::ZeroKeyMemory()
|
||||
{
|
||||
for (Block& key : roundKeys)
|
||||
key.reset();
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include "Block.h"
|
||||
#include "Halfblock.h"
|
||||
|
||||
namespace GhettoCipher
|
||||
{
|
||||
/** Class to perform a feistel block chipher
|
||||
*/
|
||||
class Feistel
|
||||
@ -55,3 +57,4 @@ private:
|
||||
|
||||
Keyset roundKeys;
|
||||
};
|
||||
}
|
||||
|
@ -141,7 +141,7 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Feistel.cpp" />
|
||||
<ClCompile Include="GhettoCipherWrapper.cpp" />
|
||||
<ClCompile Include="GhettoCipher.cpp" />
|
||||
<ClCompile Include="Cipher.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@ -149,11 +149,12 @@
|
||||
<ClInclude Include="Config.h" />
|
||||
<ClInclude Include="Feistel.h" />
|
||||
<ClInclude Include="GhettoCipherWrapper.h" />
|
||||
<ClInclude Include="GhettoCipher.h" />
|
||||
<ClInclude Include="Cipher.h" />
|
||||
<ClInclude Include="Flexblock.h" />
|
||||
<ClInclude Include="Halfblock.h" />
|
||||
<ClInclude Include="Keyset.h" />
|
||||
<ClInclude Include="Util.h" />
|
||||
<ClInclude Include="Version.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
@ -21,10 +21,10 @@
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="GhettoCipher.cpp">
|
||||
<ClCompile Include="GhettoCipherWrapper.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="GhettoCipherWrapper.cpp">
|
||||
<ClCompile Include="Cipher.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
@ -50,10 +50,13 @@
|
||||
<ClInclude Include="Flexblock.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="GhettoCipher.h">
|
||||
<ClInclude Include="GhettoCipherWrapper.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="GhettoCipherWrapper.h">
|
||||
<ClInclude Include="Version.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Cipher.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
namespace GhettoCipher
|
||||
{
|
||||
//! 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 "GhettoCipher.h"
|
||||
#include "Cipher.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
|
||||
GhettoCipher cipher(password);
|
||||
Cipher cipher(password);
|
||||
|
||||
// Recode the ascii-string to bits
|
||||
const Flexblock cleartext_bits = StringToBits(cleartext);
|
||||
@ -20,10 +20,10 @@ std::string GhettoCipherWrapper::EncryptString(const std::string& cleartext, con
|
||||
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
|
||||
GhettoCipher cipher(password);
|
||||
Cipher cipher(password);
|
||||
|
||||
// Recode the hex-string to bits
|
||||
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
|
||||
@ -38,7 +38,7 @@ std::string GhettoCipherWrapper::DecryptString(const std::string& ciphertext, co
|
||||
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
|
||||
{
|
||||
@ -46,7 +46,7 @@ bool GhettoCipherWrapper::EncryptFile(const std::string& filename_in, const std:
|
||||
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
|
||||
|
||||
// Instanciate our cipher and supply a key
|
||||
GhettoCipher cipher(password);
|
||||
Cipher cipher(password);
|
||||
|
||||
// Encrypt our 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
|
||||
{
|
||||
@ -70,7 +70,7 @@ bool GhettoCipherWrapper::DecryptFile(const std::string& filename_in, const std:
|
||||
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
|
||||
|
||||
// Instanciate our cipher and supply a key
|
||||
GhettoCipher cipher(password);
|
||||
Cipher cipher(password);
|
||||
|
||||
// Decrypt the ciphertext bits
|
||||
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits);
|
||||
|
@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace GhettoCipher
|
||||
{
|
||||
/** This class is a wrapper to make working with the GhettoCipher super easy with a python-like syntax
|
||||
*/
|
||||
class GhettoCipherWrapper
|
||||
class GhettoCryptWrapper
|
||||
{
|
||||
public:
|
||||
//! Will encrypt a string and return it hexadecimally encoded.
|
||||
@ -26,5 +28,6 @@ public:
|
||||
|
||||
private:
|
||||
// No instanciation! >:(
|
||||
GhettoCipherWrapper();
|
||||
GhettoCryptWrapper();
|
||||
};
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
#include <bitset>
|
||||
#include "Config.h"
|
||||
|
||||
#define HALFBLOCK_SIZE (BLOCK_SIZE / 2)
|
||||
|
||||
namespace GhettoCipher
|
||||
{
|
||||
constexpr int HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
|
||||
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
|
||||
}
|
||||
|
3
Keyset.h
3
Keyset.h
@ -3,4 +3,7 @@
|
||||
#include "Block.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace GhettoCipher
|
||||
{
|
||||
typedef std::array<Block, N_ROUNDS> Keyset;
|
||||
}
|
||||
|
3
Util.h
3
Util.h
@ -5,6 +5,8 @@
|
||||
#include "Block.h"
|
||||
#include "Flexblock.h"
|
||||
|
||||
namespace GhettoCipher
|
||||
{
|
||||
//! Mod-operator that works with negative values
|
||||
inline int Mod(int numerator, int denominator)
|
||||
{
|
||||
@ -250,3 +252,4 @@ inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits)
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
10
main.cpp
10
main.cpp
@ -3,6 +3,8 @@
|
||||
#include "Util.h"
|
||||
#include "GhettoCipherWrapper.h"
|
||||
|
||||
using namespace GhettoCipher;
|
||||
|
||||
void ExampleString()
|
||||
{
|
||||
std::cout << "Example on how to encrypt & decrypt a string:" << std::endl;
|
||||
@ -12,11 +14,11 @@ void ExampleString()
|
||||
std::cout << input << std::endl;
|
||||
|
||||
// Encrypt
|
||||
const std::string encrypted = GhettoCipherWrapper::EncryptString(input, "password1");
|
||||
const std::string encrypted = GhettoCryptWrapper::EncryptString(input, "password1");
|
||||
std::cout << encrypted << std::endl;
|
||||
|
||||
// Decrypt
|
||||
const std::string decrypted = GhettoCipherWrapper::DecryptString(encrypted, "password1");
|
||||
const std::string decrypted = GhettoCryptWrapper::DecryptString(encrypted, "password1");
|
||||
std::cout << decrypted << std::endl;
|
||||
|
||||
return;
|
||||
@ -27,10 +29,10 @@ void ExampleFiles()
|
||||
std::cout << "Example on how to encrypt & decrypt any file:" << std::endl;
|
||||
|
||||
// Encrypt
|
||||
GhettoCipherWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1");
|
||||
GhettoCryptWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1");
|
||||
|
||||
// Decrypt
|
||||
GhettoCipherWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1");
|
||||
GhettoCryptWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1");
|
||||
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user