2021-12-06 12:47:15 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
2021-12-06 02:20:47 +01:00
|
|
|
#include "Cipher.h"
|
2021-12-06 00:39:58 +01:00
|
|
|
#include "Util.h"
|
2022-02-06 18:46:07 +01:00
|
|
|
#include "InitializationVector.h"
|
2021-12-06 00:39:58 +01:00
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Cipher::Cipher(const Block& key)
|
2021-12-06 00:39:58 +01:00
|
|
|
:
|
2022-02-06 18:46:07 +01:00
|
|
|
key { key },
|
|
|
|
initializationVector(InitializationVector(key))
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Cipher::Cipher(const std::string& password)
|
2022-02-06 18:46:07 +01:00
|
|
|
:
|
|
|
|
key { PasswordToKey(password) },
|
|
|
|
initializationVector(InitializationVector(key))
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Cipher::~Cipher()
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
// Clear key memory
|
|
|
|
ZeroKeyMemory();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
void GhettoCipher::Cipher::SetKey(const Block& key)
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
ZeroKeyMemory();
|
|
|
|
|
|
|
|
this->key = key;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
void GhettoCipher::Cipher::SetPassword(const std::string& password)
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
ZeroKeyMemory();
|
|
|
|
|
|
|
|
key = PasswordToKey(password);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
// Split cleartext into blocks
|
|
|
|
std::vector<Block> blocks;
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE)
|
|
|
|
blocks.push_back(Block(
|
|
|
|
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Encrypt individual blocks using cipher block chaining
|
|
|
|
Feistel feistel(key);
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < blocks.size(); i++)
|
|
|
|
{
|
2021-12-06 01:33:55 +01:00
|
|
|
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
|
2021-12-06 01:38:43 +01:00
|
|
|
if ((i % ((blocks.size() > 1000)? 100 : 10) == 0) && (printProgress))
|
2021-12-06 02:29:09 +01:00
|
|
|
std::cout << "Encrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100 / blocks.size()) << "%)" << std::endl;
|
2021-12-06 01:33:55 +01:00
|
|
|
|
2022-02-06 18:46:07 +01:00
|
|
|
const Block& lastBlock = (i>0) ? blocks[i-1] : initializationVector;
|
2022-02-06 21:54:43 +01:00
|
|
|
blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock); // Xor last cipher block with new clear text block before E()
|
2021-12-06 00:39:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Concatenate ciphertext blocks back into a flexblock
|
|
|
|
std::stringstream ss;
|
|
|
|
for (Block& b : blocks)
|
|
|
|
ss << b;
|
|
|
|
|
|
|
|
// Return it
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
// Split ciphertext into blocks
|
|
|
|
std::vector<Block> blocks;
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE)
|
|
|
|
blocks.push_back(Block(
|
|
|
|
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Decrypt individual blocks
|
|
|
|
Feistel feistel(key);
|
|
|
|
|
|
|
|
// We can't do this in-loop for decryption, because we are decrypting the blocks in-place.
|
2022-02-06 18:46:07 +01:00
|
|
|
Block lastBlock = initializationVector;
|
2021-12-06 00:39:58 +01:00
|
|
|
|
|
|
|
for (std::size_t i = 0; i < blocks.size(); i++)
|
|
|
|
{
|
2021-12-06 01:33:55 +01:00
|
|
|
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
|
2021-12-06 01:38:43 +01:00
|
|
|
if ((i % ((blocks.size() > 1000) ? 100 : 10) == 0) && (printProgress))
|
2021-12-06 02:29:09 +01:00
|
|
|
std::cout << "Decrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100/ blocks.size()) << "%)" << std::endl;
|
2021-12-06 01:33:55 +01:00
|
|
|
|
2021-12-06 00:39:58 +01:00
|
|
|
Block tmpCopy = blocks[i];
|
|
|
|
|
2022-02-06 21:54:43 +01:00
|
|
|
blocks[i] = feistel.Decipher(blocks[i]) ^ lastBlock; // Decipher cipher block [i] and then xor it with the last cipher block [i-1] we've had
|
2021-12-06 00:39:58 +01:00
|
|
|
|
|
|
|
lastBlock = std::move(tmpCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Concatenate ciphertext blocks back into a flexblock
|
|
|
|
std::stringstream ss;
|
|
|
|
for (Block& b : blocks)
|
|
|
|
ss << b;
|
|
|
|
|
|
|
|
// Return it
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:02:37 +01:00
|
|
|
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
|
2021-12-06 02:57:20 +01:00
|
|
|
#if defined _WIN32 || defined _WIN64
|
2021-12-06 00:39:58 +01:00
|
|
|
#pragma optimize("", off )
|
2021-12-06 12:53:18 +01:00
|
|
|
#elif defined __GNUG__
|
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC optimize ("O0")
|
2021-12-06 02:57:20 +01:00
|
|
|
#endif
|
2021-12-06 02:20:47 +01:00
|
|
|
void GhettoCipher::Cipher::ZeroKeyMemory()
|
2021-12-06 00:39:58 +01:00
|
|
|
{
|
|
|
|
key.reset();
|
|
|
|
return;
|
|
|
|
}
|
2021-12-06 02:57:20 +01:00
|
|
|
#if defined _WIN32 || defined _WIN64
|
2021-12-06 00:39:58 +01:00
|
|
|
#pragma optimize("", on )
|
2021-12-06 12:53:18 +01:00
|
|
|
#elif defined __GNUG__
|
|
|
|
#pragma GCC pop_options
|
2021-12-06 02:57:20 +01:00
|
|
|
#endif
|