GCrypt/GhettoCrypt/src/GhettoCryptWrapper.cpp

85 lines
2.7 KiB
C++
Raw Normal View History

#include "GhettoCryptWrapper.h"
2021-12-06 02:20:47 +01:00
#include "Cipher.h"
#include "Util.h"
2022-05-16 22:01:52 +02:00
std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
2022-05-16 22:01:52 +02:00
// Recode the ascii-string to bits
const Flexblock cleartext_bits = StringToBits(cleartext);
2022-05-16 22:01:52 +02:00
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
2022-05-16 22:01:52 +02:00
// Recode the ciphertext bits to a hex-string
const std::string ciphertext = BitsToHexstring(ciphertext_bits);
2022-05-16 22:01:52 +02:00
// Return it
return ciphertext;
}
2022-05-16 22:01:52 +02:00
std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
2022-05-16 22:01:52 +02:00
// Recode the hex-string to bits
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
2022-05-16 22:01:52 +02:00
// Decrypt the ciphertext bits
const std::string cleartext_bits = cipher.Decipher(ciphertext_bits);
2022-05-16 22:01:52 +02:00
// Recode the cleartext bits to an ascii-string
const std::string cleartext = BitsToString(cleartext_bits);
2022-05-16 22:01:52 +02:00
// Return it
return cleartext;
}
2022-05-16 22:01:52 +02:00
bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
try {
// Read the file to bits
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport);
// Write our ciphertext bits to file
WriteBitsToFile(filename_out, ciphertext_bits);
return true;
}
catch (std::runtime_error&) {
return false;
}
}
2022-05-16 22:01:52 +02:00
bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
try {
// Read the file to bits
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Decrypt the ciphertext bits
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport);
// Write our cleartext bits to file
WriteBitsToFile(filename_out, cleartext_bits);
return true;
}
catch (std::runtime_error&) {
return false;
}
}
2022-05-16 22:01:52 +02:00