Implemented a wrapper for super easy use
This commit is contained in:
parent
fbead384e2
commit
5bcbe09922
@ -140,6 +140,7 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Feistel.cpp" />
|
||||
<ClCompile Include="GhettoCipherWrapper.cpp" />
|
||||
<ClCompile Include="GhettoCipher.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
@ -147,6 +148,7 @@
|
||||
<ClInclude Include="Block.h" />
|
||||
<ClInclude Include="Config.h" />
|
||||
<ClInclude Include="Feistel.h" />
|
||||
<ClInclude Include="GhettoCipherWrapper.h" />
|
||||
<ClInclude Include="GhettoCipher.h" />
|
||||
<ClInclude Include="Flexblock.h" />
|
||||
<ClInclude Include="Halfblock.h" />
|
||||
|
@ -24,6 +24,9 @@
|
||||
<ClCompile Include="GhettoCipher.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="GhettoCipherWrapper.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Feistel.h">
|
||||
@ -50,5 +53,8 @@
|
||||
<ClInclude Include="GhettoCipher.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="GhettoCipherWrapper.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
87
GhettoCipherWrapper.cpp
Normal file
87
GhettoCipherWrapper.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include "GhettoCipherWrapper.h"
|
||||
#include "GhettoCipher.h"
|
||||
#include "Util.h"
|
||||
|
||||
std::string GhettoCipherWrapper::EncryptString(const std::string& cleartext, const std::string& password)
|
||||
{
|
||||
// Instanciate our cipher and supply a key
|
||||
GhettoCipher cipher(password);
|
||||
|
||||
// Recode the ascii-string to bits
|
||||
const Flexblock cleartext_bits = StringToBits(cleartext);
|
||||
|
||||
// Encrypt our cleartext bits
|
||||
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
|
||||
|
||||
// Recode the ciphertext bits to a hex-string
|
||||
const std::string ciphertext = BitsToHexstring(ciphertext_bits);
|
||||
|
||||
// Return it
|
||||
return ciphertext;
|
||||
}
|
||||
|
||||
std::string GhettoCipherWrapper::DecryptString(const std::string& ciphertext, const std::string& password)
|
||||
{
|
||||
// Instanciate our cipher and supply a key
|
||||
GhettoCipher cipher(password);
|
||||
|
||||
// Recode the hex-string to bits
|
||||
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
|
||||
|
||||
// Decrypt the ciphertext bits
|
||||
const std::string cleartext_bits = cipher.Decipher(ciphertext_bits);
|
||||
|
||||
// Recode the cleartext bits to an ascii-string
|
||||
const std::string cleartext = BitsToString(cleartext_bits);
|
||||
|
||||
// Return it
|
||||
return cleartext;
|
||||
}
|
||||
|
||||
bool GhettoCipherWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Read the file to bits
|
||||
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
|
||||
|
||||
// Instanciate our cipher and supply a key
|
||||
GhettoCipher cipher(password);
|
||||
|
||||
// Encrypt our cleartext bits
|
||||
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
|
||||
|
||||
// Write our ciphertext bits to file
|
||||
WriteBitsToFile(filename_out, ciphertext_bits);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (std::runtime_error&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool GhettoCipherWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Read the file to bits
|
||||
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
|
||||
|
||||
// Instanciate our cipher and supply a key
|
||||
GhettoCipher cipher(password);
|
||||
|
||||
// Decrypt the ciphertext bits
|
||||
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits);
|
||||
|
||||
// Write our cleartext bits to file
|
||||
WriteBitsToFile(filename_out, cleartext_bits);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (std::runtime_error&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
30
GhettoCipherWrapper.h
Normal file
30
GhettoCipherWrapper.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
/** This class is a wrapper to make working with the GhettoCipher super easy with a python-like syntax
|
||||
*/
|
||||
class GhettoCipherWrapper
|
||||
{
|
||||
public:
|
||||
//! Will encrypt a string and return it hexadecimally encoded.
|
||||
static std::string EncryptString(const std::string& cleartext, const std::string& password);
|
||||
|
||||
//! Will decrypt a hexadecimally encoded string.
|
||||
static std::string DecryptString(const std::string& ciphertext, const std::string& password);
|
||||
|
||||
//! Will encrypt a file.
|
||||
//! Returns false if anything goes wrong (like, file-access).
|
||||
//! @filename_in The file to be read.
|
||||
//! @filename_out The file the encrypted version should be saved in.
|
||||
static bool EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password);
|
||||
|
||||
//! Will decrypt a file.
|
||||
//! Returns false if anything goes wrong (like, file-access).
|
||||
//! @filename_in The file to be read.
|
||||
//! @filename_out The file the decrypted version should be saved in.
|
||||
static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password);
|
||||
|
||||
private:
|
||||
// No instanciation! >:(
|
||||
GhettoCipherWrapper();
|
||||
};
|
64
main.cpp
64
main.cpp
@ -1,38 +1,44 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include "Util.h"
|
||||
#include "GhettoCipher.h"
|
||||
#include "GhettoCipherWrapper.h"
|
||||
|
||||
void ExampleString()
|
||||
{
|
||||
std::cout << "Example on how to encrypt & decrypt a string:" << std::endl;
|
||||
|
||||
// Get some string
|
||||
const std::string input = "I am a super secret message!";
|
||||
std::cout << input << std::endl;
|
||||
|
||||
// Encrypt
|
||||
const std::string encrypted = GhettoCipherWrapper::EncryptString(input, "password1");
|
||||
std::cout << encrypted << std::endl;
|
||||
|
||||
// Decrypt
|
||||
const std::string decrypted = GhettoCipherWrapper::DecryptString(encrypted, "password1");
|
||||
std::cout << decrypted << std::endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ExampleFiles()
|
||||
{
|
||||
std::cout << "Example on how to encrypt & decrypt any file:" << std::endl;
|
||||
|
||||
// Encrypt
|
||||
GhettoCipherWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1");
|
||||
|
||||
// Decrypt
|
||||
GhettoCipherWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Load file to be encrypted
|
||||
const Flexblock file = ReadFileToBits("images.jpg");
|
||||
std::cout << "Finished reading..." << std::endl;
|
||||
|
||||
|
||||
// Prepare cipher
|
||||
GhettoCipher feistel("Password yo");
|
||||
|
||||
|
||||
// Encrypt
|
||||
const Flexblock ciphertext = feistel.Encipher(file, true);
|
||||
//const std::string cipherHex = BitsToHexstring(ciphertext);
|
||||
std::cout << "Finished encrypting..." << std::endl;
|
||||
|
||||
// Save encrypted file
|
||||
WriteBitsToFile("encrypted.jpg", ciphertext);
|
||||
std::cout << "Finished writing..." << std::endl;
|
||||
|
||||
|
||||
// Decrypt
|
||||
//const Flexblock cleartextBits = feistel.Decipher(HexstringToBits(cipherHex));
|
||||
const Flexblock cleartextBits = feistel.Decipher(ciphertext, true);
|
||||
std::cout << "Finished decrypting..." << std::endl;
|
||||
|
||||
|
||||
// Save decrypted file
|
||||
WriteBitsToFile("decrypted.jpg", cleartextBits);
|
||||
std::cout << "Finished writing..." << std::endl;
|
||||
ExampleString();
|
||||
ExampleFiles();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user