2022-05-16 22:50:52 +02:00
|
|
|
#include "GCrypt/GCryptWrapper.h"
|
2022-05-16 22:35:28 +02:00
|
|
|
#include "GCrypt/Cipher.h"
|
|
|
|
#include "GCrypt/Util.h"
|
2022-05-16 22:15:34 +02:00
|
|
|
|
|
|
|
namespace Leonetienne::GCrypt {
|
|
|
|
|
2022-05-16 22:50:52 +02:00
|
|
|
std::string GCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) {
|
2022-05-21 20:41:09 +02:00
|
|
|
// Transform the password to a key
|
2022-05-16 22:15:34 +02:00
|
|
|
const Block key = PasswordToKey(password);
|
|
|
|
|
|
|
|
// Recode the ascii-string to bits
|
|
|
|
const Flexblock cleartext_bits = StringToBits(cleartext);
|
|
|
|
|
|
|
|
// Encrypt our cleartext bits
|
2022-05-21 20:41:09 +02:00
|
|
|
const Flexblock ciphertext_bits = DigestFlexblock(cleartext_bits, key, Cipher::CIPHER_DIRECTION::ENCIPHER);
|
2022-05-16 22:15:34 +02:00
|
|
|
|
|
|
|
// Recode the ciphertext bits to a hex-string
|
|
|
|
const std::string ciphertext = BitsToHexstring(ciphertext_bits);
|
|
|
|
|
|
|
|
// Return it
|
|
|
|
return ciphertext;
|
|
|
|
}
|
|
|
|
|
2022-05-16 22:50:52 +02:00
|
|
|
std::string GCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) {
|
2022-05-21 20:41:09 +02:00
|
|
|
// Transform the password to a key
|
2022-05-16 22:15:34 +02:00
|
|
|
const Block key = PasswordToKey(password);
|
|
|
|
|
|
|
|
// Recode the hex-string to bits
|
|
|
|
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
|
|
|
|
|
|
|
|
// Decrypt the ciphertext bits
|
2022-05-21 20:41:09 +02:00
|
|
|
const std::string cleartext_bits = DigestFlexblock(ciphertext_bits, key, Cipher::CIPHER_DIRECTION::DECIPHER);
|
2022-05-16 22:15:34 +02:00
|
|
|
|
|
|
|
// Recode the cleartext bits to an ascii-string
|
|
|
|
const std::string cleartext = BitsToString(cleartext_bits);
|
|
|
|
|
|
|
|
// Return it
|
|
|
|
return cleartext;
|
|
|
|
}
|
|
|
|
|
2022-05-16 22:50:52 +02:00
|
|
|
bool GCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
|
2022-05-16 22:15:34 +02:00
|
|
|
try {
|
|
|
|
// Read the file to bits
|
|
|
|
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
|
|
|
|
|
2022-05-21 20:41:09 +02:00
|
|
|
// Transform the password to a key
|
2022-05-16 22:15:34 +02:00
|
|
|
const Block key = PasswordToKey(password);
|
|
|
|
|
|
|
|
// Encrypt our cleartext bits
|
2022-05-21 20:41:09 +02:00
|
|
|
const Flexblock ciphertext_bits = DigestFlexblock(cleartext_bits, key, Cipher::CIPHER_DIRECTION::ENCIPHER);
|
2022-05-16 22:15:34 +02:00
|
|
|
|
|
|
|
// Write our ciphertext bits to file
|
|
|
|
WriteBitsToFile(filename_out, ciphertext_bits);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (std::runtime_error&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 22:50:52 +02:00
|
|
|
bool GCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
|
2022-05-16 22:15:34 +02:00
|
|
|
try {
|
|
|
|
// Read the file to bits
|
|
|
|
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
|
|
|
|
|
2022-05-21 20:41:09 +02:00
|
|
|
// Transform the password to a key
|
2022-05-16 22:15:34 +02:00
|
|
|
const Block key = PasswordToKey(password);
|
|
|
|
|
|
|
|
// Decrypt the ciphertext bits
|
2022-05-21 20:41:09 +02:00
|
|
|
const Flexblock cleartext_bits = DigestFlexblock(ciphertext_bits, key, Cipher::CIPHER_DIRECTION::DECIPHER);
|
2022-05-16 22:15:34 +02:00
|
|
|
|
|
|
|
// Write our cleartext bits to file
|
|
|
|
WriteBitsToFile(filename_out, cleartext_bits);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (std::runtime_error&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 20:41:09 +02:00
|
|
|
Flexblock GCryptWrapper::DigestFlexblock(const Flexblock& data, const Block& key, const Cipher::CIPHER_DIRECTION direction) {
|
|
|
|
// Split input 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))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create cipher instance
|
|
|
|
Cipher cipher(key, direction);
|
|
|
|
|
|
|
|
// Digest all blocks
|
|
|
|
for (Block& block : blocks) {
|
|
|
|
block = cipher.Digest(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Concatenate ciphertext blocks back into a flexblock
|
|
|
|
std::stringstream ss;
|
|
|
|
for (Block& b : blocks) {
|
|
|
|
ss << b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return it
|
|
|
|
return ss.str();
|
|
|
|
}
|
2022-05-16 22:15:34 +02:00
|
|
|
}
|
|
|
|
|