Integrated initialization vector

This commit is contained in:
Leonetienne 2022-02-06 18:46:07 +01:00
parent 40a6b0c4ea
commit e57456e9ae
4 changed files with 11 additions and 10 deletions

View File

@ -41,7 +41,7 @@ void ExampleFiles()
int main()
{
//ExampleString();
ExampleString();
//ExampleFiles();
return 0;

View File

@ -2,19 +2,22 @@
#include <vector>
#include "Cipher.h"
#include "Util.h"
#include "InitializationVector.h"
GhettoCipher::Cipher::Cipher(const Block& key)
:
key { key }
key { key },
initializationVector(InitializationVector(key))
{
return;
}
GhettoCipher::Cipher::Cipher(const std::string& password)
:
key { PasswordToKey(password) },
initializationVector(InitializationVector(key))
{
key = PasswordToKey(password);
return;
}
@ -61,7 +64,7 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bo
if ((i % ((blocks.size() > 1000)? 100 : 10) == 0) && (printProgress))
std::cout << "Encrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100 / blocks.size()) << "%)" << std::endl;
const Block& lastBlock = (i>0) ? blocks[i-1] : emptyBlock;
const Block& lastBlock = (i>0) ? blocks[i-1] : initializationVector;
blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock);
}
@ -88,7 +91,7 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bo
Feistel feistel(key);
// We can't do this in-loop for decryption, because we are decrypting the blocks in-place.
Block lastBlock = emptyBlock;
Block lastBlock = initializationVector;
for (std::size_t i = 0; i < blocks.size(); i++)
{
@ -129,5 +132,3 @@ void GhettoCipher::Cipher::ZeroKeyMemory()
#elif defined __GNUG__
#pragma GCC pop_options
#endif
const GhettoCipher::Block GhettoCipher::Cipher::emptyBlock;

View File

@ -36,6 +36,6 @@ namespace GhettoCipher
void ZeroKeyMemory();
// Initial value for cipher block chaining
static const Block emptyBlock;
const Block initializationVector;
};
}

View File

@ -19,7 +19,7 @@ InitializationVector::InitializationVector(const Block& seed)
for (std::size_t i = 0; i < BLOCK_SIZE; i++)
ss << (mt() % 2 ? '1' : '0');
// And create a bitset
// And create a block
iv = Block(ss.str());
}