Implemented useful custom bases, fun bases, also with support to decipher them

This commit is contained in:
Leonetienne 2022-03-13 18:10:23 +01:00
parent cdbb52b24c
commit 5fc0e190b9
9 changed files with 309 additions and 1073 deletions

File diff suppressed because it is too large Load Diff

View File

@ -11,5 +11,6 @@ add_executable(GhettoCryptCLI
CommandLineInterface.cpp CommandLineInterface.cpp
Hazelnupp.cpp Hazelnupp.cpp
StringTools.cpp
main.cpp main.cpp
) )

View File

@ -64,22 +64,22 @@ void CommandlineInterface::Init(int argc, const char* const* argv)
nupp.RegisterAbbreviation("-p", "--progress"); nupp.RegisterAbbreviation("-p", "--progress");
nupp.RegisterDescription("--iobase-2", "Interpret and format ciphertexts in base2"); nupp.RegisterDescription("--iobase-2", "Interpret and format ciphertexts in base2");
nupp.RegisterConstraint("--iobase-2", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-uwu", "--iobase-emoji" })); nupp.RegisterConstraint("--iobase-2", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
nupp.RegisterDescription("--iobase-8", "Interpret and format ciphertexts in base8"); nupp.RegisterDescription("--iobase-8", "Interpret and format ciphertexts in base8");
nupp.RegisterConstraint("--iobase-8", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-10", "--iobase-64", "--iobase-uwu", "--iobase-emoji" })); nupp.RegisterConstraint("--iobase-8", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-10", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
nupp.RegisterDescription("--iobase-10", "Interpret and format ciphertexts in base10"); nupp.RegisterDescription("--iobase-10", "Interpret and format ciphertexts in base10");
nupp.RegisterConstraint("--iobase-10", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-64", "--iobase-uwu", "--iobase-emoji" })); nupp.RegisterConstraint("--iobase-10", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
nupp.RegisterDescription("--iobase-64", "Interpret and format ciphertexts in base64"); nupp.RegisterDescription("--iobase-64", "Interpret and format ciphertexts in base64");
nupp.RegisterConstraint("--iobase-64", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-uwu", "--iobase-emoji" })); nupp.RegisterConstraint("--iobase-64", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-uwu", "--iobase-ugh" }));
nupp.RegisterDescription("--iobase-uwu", "Interpret and format ciphertexts in base uwu"); nupp.RegisterDescription("--iobase-uwu", "Interpret and format ciphertexts in base uwu");
nupp.RegisterConstraint("--iobase-uwu", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-emoji" })); nupp.RegisterConstraint("--iobase-uwu", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-ugh" }));
nupp.RegisterDescription("--iobase-emoji", "Interpret and format ciphertexts in base emoji (EXPERIMENTAL)"); nupp.RegisterDescription("--iobase-ugh", "Interpret and format ciphertexts in base ugh");
nupp.RegisterConstraint("--iobase-emoji", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-uwu" })); nupp.RegisterConstraint("--iobase-ugh", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-uwu" }));
nupp.RegisterDescription("--version", "Will supply the version of ghettocrypt used."); nupp.RegisterDescription("--version", "Will supply the version of ghettocrypt used.");
nupp.RegisterConstraint("--version", ParamConstraint(true, DATA_TYPE::VOID, {}, false, {})); nupp.RegisterConstraint("--version", ParamConstraint(true, DATA_TYPE::VOID, {}, false, {}));

View File

@ -142,6 +142,7 @@
<ClCompile Include="CommandlineInterface.cpp" /> <ClCompile Include="CommandlineInterface.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="Hazelnupp.cpp" /> <ClCompile Include="Hazelnupp.cpp" />
<ClCompile Include="StringTools.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\GhettoCrypt\GhettoCrypt.vcxproj"> <ProjectReference Include="..\GhettoCrypt\GhettoCrypt.vcxproj">
@ -150,9 +151,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="BaseConversion.h" /> <ClInclude Include="BaseConversion.h" />
<ClInclude Include="Bases.h" />
<ClInclude Include="CommandlineInterface.h" /> <ClInclude Include="CommandlineInterface.h" />
<ClInclude Include="ContainerUtility.h" /> <ClInclude Include="ContainerUtility.h" />
<ClInclude Include="Hazelnupp.h" /> <ClInclude Include="Hazelnupp.h" />
<ClInclude Include="StringTools.h" />
<ClInclude Include="Version.h" /> <ClInclude Include="Version.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -24,6 +24,9 @@
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="StringTools.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Hazelnupp.h"> <ClInclude Include="Hazelnupp.h">
@ -41,5 +44,11 @@
<ClInclude Include="ContainerUtility.h"> <ClInclude Include="ContainerUtility.h">
<Filter>Headerdateien</Filter> <Filter>Headerdateien</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Bases.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="StringTools.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,134 @@
#include "StringTools.h"
#include <sstream>
std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) {
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
{
if (str[i] != find)
ss << str[i];
else
ss << subst;
}
return ss.str();
}
std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) {
if (find.length() == 0)
return str;
std::stringstream ss;
std::size_t posFound = 0;
std::size_t lastFound = 0;
while (posFound != std::string::npos)
{
lastFound = posFound;
posFound = str.find(find, posFound);
if (posFound != std::string::npos)
{
ss << str.substr(lastFound, posFound - lastFound) << subst;
posFound += find.length();
}
else
{
ss << str.substr(lastFound, (str.length()) - lastFound);
}
}
return ss.str();
}
std::string StringTools::Replace(const std::string& str, const char find, const char subst) {
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
}
std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst) {
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
}
std::string StringTools::Lower(const std::string& str) {
std::stringstream ss;
for (std::size_t i = 0; i < str.size(); i++)
{
const char c = str[i];
// Quick-accept: regular letters
if ((c >= 'A') && (c <= 'Z'))
ss << (char)(c | 32);
// Else: keep the character as is
else ss << c;
}
return ss.str();
}
std::string StringTools::Upper(const std::string& str) {
std::stringstream ss;
for (std::size_t i = 0; i < str.size(); i++)
{
const char c = str[i];
// Quick-accept: regular letters
if ((c >= 'a') && (c <= 'z'))
ss << (char)(c & ~32);
// Else: keep the character as is
else ss << c;
}
return ss.str();
}
std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
std::vector<std::string> toRet;
// Quick-accept: str length is 0
if (str.length() == 0)
toRet.push_back("");
// Quick-accept: seperator length is 0
else if (seperator.length() == 0) {
for (const char c : str)
toRet.push_back(std::string(&c, (&c) + 1));
}
else {
std::size_t idx = 0;
while (idx != std::string::npos) {
std::size_t lastIdx = idx;
idx = str.find(seperator, idx);
// Grab our substring until the next finding of sep
if (idx != std::string::npos) {
toRet.push_back(str.substr(
lastIdx,
idx - lastIdx
));
idx += seperator.length();
}
// No more seperator found. Grab the rest until the end of the string
else {
toRet.push_back(str.substr(
lastIdx
));
}
}
}
return toRet;
}

View File

@ -0,0 +1,37 @@
#ifndef STRINGTOOLS_STRINGTOOLS_H
#define STRINGTOOLS_STRINGTOOLS_H
#include <string>
#include <vector>
/* Handy utensils to manipulate strings */
class StringTools
{
public:
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const char find, const std::string& subst);
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const std::string& find, const std::string& subst);
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const char find, const char subst);
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const std::string& find, const char subst);
//! Will make a string all-lowercase.
static std::string Lower(const std::string& str);
//! Will make a string all-uppercase.
static std::string Upper(const std::string& str);
//! Will split a string by a string seperator
static std::vector<std::string> Split(const std::string& str, const std::string& seperator);
private:
// No instanciation! >:(
StringTools();
};
#endif //STRINGTOOLS_STRINGTOOLS_H

View File

@ -1,2 +1,2 @@
#pragma once #pragma once
#define GHETTOCRYPTCLI_VERSION 0.122 #define GHETTOCRYPTCLI_VERSION 0.123

View File

@ -12,6 +12,7 @@
#include "Bases.h" #include "Bases.h"
#include "BaseConversion.h" #include "BaseConversion.h"
#include "StringTools.h"
// Required for hidden password input // Required for hidden password input
#if defined _WIN32 || defined _WIN64 #if defined _WIN32 || defined _WIN64
@ -24,14 +25,13 @@
using namespace GhettoCipher; using namespace GhettoCipher;
namespace { namespace {
template <class T_CONTAINER> inline std::string Bin2CustomBase(const std::string& bin, const std::vector<std::string>& customSet, const std::string& seperator = "")
inline std::string Hex2CustomBase(const std::string& hex, const T_CONTAINER& customSet, const std::string& seperator = "")
{ {
std::stringstream ss; std::stringstream ss;
// Translate to custom set // Translate to custom set
const T_CONTAINER vec_base_custom = const std::vector<std::string> vec_base_custom =
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::string, T_CONTAINER>(hex, BASE_16, customSet); Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::string, std::vector<std::string>>(bin, "01", customSet);
// Convert to string // Convert to string
for (std::size_t i = 0; i < vec_base_custom.size(); i++) for (std::size_t i = 0; i < vec_base_custom.size(); i++)
@ -44,6 +44,32 @@ namespace {
return ss.str(); return ss.str();
} }
inline std::string CustomBase2Bin(const std::string& in, const std::vector<std::string>& customSet, const std::string& seperator = "")
{
// Split input into symbols
const std::vector<std::string> in_symbols = StringTools::Split(in, seperator);
// Translate to binary
std::string binary =
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::vector<std::string>, std::string>(in_symbols, customSet, std::string("01"));
// Apply padding to be a multiple of BLOCK_SIZE
// Count how many bits we need
std::size_t required_size = 0;
while (required_size < binary.length())
required_size += BLOCK_SIZE;
// Create a string of that many zeroes
std::string padding = "";
for (std::size_t i = binary.length(); i < required_size; i++)
padding += "0";
// Prepend it to our bitstring
binary = padding + binary;
return binary;
}
} }
//! Will prompt a user password from stdin, hiding the input //! Will prompt a user password from stdin, hiding the input
@ -142,9 +168,46 @@ const Flexblock GetInputText(bool encryptionMode)
// Encryption mode: We want to return the text as-is, as bits // Encryption mode: We want to return the text as-is, as bits
if (encryptionMode) if (encryptionMode)
return StringToBits(CommandlineInterface::Get()["--intext"].GetString()); return StringToBits(CommandlineInterface::Get()["--intext"].GetString());
// Decryption mode: We need to first convert hexstring to bitstring
// Decryption mode: We need to first convert our input to a bitstring
else else
return HexstringToBits(CommandlineInterface::Get()["--intext"].GetString()); {
const std::string userInput = CommandlineInterface::Get()["--intext"].GetString();
// Are we using iobase 2?
if (CommandlineInterface::Get().HasParam("--iobase-2"))
// Yes: convert to binary from base 2
return CustomBase2Bin(userInput, BASE_2);
// Are we using iobase 8?
else if (CommandlineInterface::Get().HasParam("--iobase-8"))
// Yes: convert to binary from base 8
return CustomBase2Bin(userInput, BASE_8);
// Are we using iobase 10?
else if (CommandlineInterface::Get().HasParam("--iobase-10"))
// Yes: convert to binary from base 10
return CustomBase2Bin(userInput, BASE_10);
// Are we using iobase 64?
else if (CommandlineInterface::Get().HasParam("--iobase-64"))
// Yes: convert to binary from base 64
return CustomBase2Bin(userInput, BASE_64);
// Are we using iobase uwu?
else if (CommandlineInterface::Get().HasParam("--iobase-uwu"))
// Yes: convert to binary from base uwu
return CustomBase2Bin(userInput, BASE_UWU, " ");
// Are we using iobase emoji?
else if (CommandlineInterface::Get().HasParam("--iobase-ugh"))
// Yes: convert to binary from base ugh
return CustomBase2Bin(userInput, BASE_UGH, " ");
// Else, no special output format specified, use hex
else
return HexstringToBits(userInput);
}
// Case: Read from file // Case: Read from file
@ -218,45 +281,50 @@ int main(int argc, char** argv)
// Else: we are just dealing with a string // Else: we are just dealing with a string
else else
{ {
// Output to stdout as a hexstring
if (shouldEncrypt) { if (shouldEncrypt) {
// We are decrypting:
const std::string hexString = BitsToHexstring(output); // Output to stdout as a formatted bytestring
std::string formattedCiphertext; std::string formattedCiphertext;
// Are we using iobase 2? // Are we using iobase 2?
if (CommandlineInterface::Get().HasParam("--iobase-2")) if (CommandlineInterface::Get().HasParam("--iobase-2"))
// Yes: convert hex to base 2 // Yes: convert hex to base 2
formattedCiphertext = Hex2CustomBase<std::string>(hexString, BASE_2); formattedCiphertext = Bin2CustomBase(output, BASE_2);
// Are we using iobase 8?
else if (CommandlineInterface::Get().HasParam("--iobase-8"))
// Yes: convert binary to base 8
formattedCiphertext = Bin2CustomBase(output, BASE_8);
// Are we using iobase 10? // Are we using iobase 10?
else if (CommandlineInterface::Get().HasParam("--iobase-10")) else if (CommandlineInterface::Get().HasParam("--iobase-10"))
// Yes: convert hex to base 10 // Yes: convert binary to base 10
formattedCiphertext = Hex2CustomBase<std::string>(hexString, BASE_10); formattedCiphertext = Bin2CustomBase(output, BASE_10);
// Are we using iobase 64? // Are we using iobase 64?
else if (CommandlineInterface::Get().HasParam("--iobase-64")) else if (CommandlineInterface::Get().HasParam("--iobase-64"))
// Yes: convert hex to base 64 // Yes: convert binary to base 64
formattedCiphertext = Hex2CustomBase<std::string>(hexString, BASE_64); formattedCiphertext = Bin2CustomBase(output, BASE_64);
// Are we using iobase uwu? // Are we using iobase uwu?
else if (CommandlineInterface::Get().HasParam("--iobase-uwu")) else if (CommandlineInterface::Get().HasParam("--iobase-uwu"))
// Yes: convert hex to base 64 // Yes: convert binary to base 64
formattedCiphertext = Hex2CustomBase<std::vector<std::string>>(hexString, BASE_UWU, " "); formattedCiphertext = Bin2CustomBase(output, BASE_UWU, " ");
// Are we using iobase emoji? // Are we using iobase ugh?
else if (CommandlineInterface::Get().HasParam("--iobase-emoji")) else if (CommandlineInterface::Get().HasParam("--iobase-ugh"))
// Yes: convert hex to base 64 // Yes: convert binary to base 64
formattedCiphertext = Hex2CustomBase<std::vector<std::string>>(hexString, BASE_EMOJI); formattedCiphertext = Bin2CustomBase(output, BASE_UGH, " ");
// Else, no special output format specified, use hex // Else, no special output format specified, use hex
else else
formattedCiphertext = hexString; formattedCiphertext = BitsToHexstring(output);
std::cout << formattedCiphertext << std::endl; std::cout << formattedCiphertext << std::endl;
} }
else { else {
// We are just decrypting:
// Just print it string-formatted
std::cout << BitsToString(output) << std::endl; std::cout << BitsToString(output) << std::endl;
} }
} }