Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BfCrypt.cpp #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions gimuserver/core/BfCrypt.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#include "BfCrypt.hpp"

#include <json/json.h>

#include <trantor/utils/Logger.h>

#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>

#include <drogon/utils/Utilities.h>

using namespace std;
using namespace BfCrypt; // for CryptSREE, CryptGME, and DecryptGME
using namespace CryptoPP; // for aes, modes, and filters

static const unsigned char SREE_KEY[] = { 0x37, 0x34, 0x31, 0x30, 0x39, 0x35, 0x38, 0x31, 0x36, 0x34, 0x33, 0x35, 0x34, 0x38, 0x37, 0x31 }; // 7410958164354871
static const unsigned char SREE_IV[] = { 0x42, 0x66, 0x77, 0x34, 0x65, 0x6E, 0x63, 0x72, 0x79, 0x70, 0x65, 0x64, 0x50, 0x61, 0x73, 0x73 }; // Bfw4encrypedPass

using namespace CryptoPP;

std::string BfCrypt::CryptSREE(const Json::Value& v)
string CryptSREE(const Json::Value& v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think automatilly setting the namespace for CryptSREE would make the function think it's not part of a class rather part of function outside of any namespace

{
try
{
Expand All @@ -30,7 +28,7 @@ std::string BfCrypt::CryptSREE(const Json::Value& v)
for (int i = 0; i < (AES::BLOCKSIZE - p); i++)
str += " ";

std::string tmp = "", output = "";
string tmp = "", output = "";

CBC_Mode<AES>::Encryption e;
e.SetKeyWithIV(SREE_KEY, sizeof(SREE_KEY), SREE_IV);
Expand All @@ -46,7 +44,7 @@ std::string BfCrypt::CryptSREE(const Json::Value& v)
}
}

std::string BfCrypt::CryptGME(const Json::Value& v, const std::string& key)
string CryptGME(const Json::Value& v, const string& key)
{
try
{
Expand All @@ -61,13 +59,13 @@ std::string BfCrypt::CryptGME(const Json::Value& v, const std::string& key)
byte aeskey[AES::MIN_KEYLENGTH] = { 0x00 };
// keys are setted this way because we know that some keys might not have the minimum requirement
#ifdef _MSC_VER
memcpy_s(aeskey, _countof(aeskey), key.data(), std::min(key.size(), (size_t)sizeof(aeskey)));
memcpy_s(aeskey, _countof(aeskey), key.data(), min(key.size(), (size_t)sizeof(aeskey)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember a compilation error might happend on Windows unless NOMINMAX is defined, is this code buildable with MSVC + Windows?

#else
memcpy(aeskey, key.data(), std::min(key.size(), (size_t)sizeof(aeskey)));
memcpy(aeskey, key.data(), min(key.size(), (size_t)sizeof(aeskey)));
#endif
e.SetKey(aeskey, sizeof(aeskey));

std::string tmp;
string tmp;
StringSource ss(str, true, new StreamTransformationFilter(e, new StringSink(tmp), StreamTransformationFilter::PKCS_PADDING));

return drogon::utils::base64Encode((const unsigned char*)tmp.data(), tmp.size());
Expand All @@ -79,25 +77,25 @@ std::string BfCrypt::CryptGME(const Json::Value& v, const std::string& key)
}
}

void BfCrypt::DecryptGME(const std::string& in, const std::string& key, Json::Value& v)
void DecryptGME(const string& in, const string& key, Json::Value& v)
{
if (key.empty() || in.empty())
return;

try
{
std::string tmp = drogon::utils::base64Decode(in);
string tmp = drogon::utils::base64Decode(in);

ECB_Mode<AES>::Decryption e;
byte aeskey[AES::MIN_KEYLENGTH] = { 0x00 };
#ifdef _MSC_VER
memcpy_s(aeskey, _countof(aeskey), key.data(), std::min(key.size(), (size_t)sizeof(aeskey)));
memcpy_s(aeskey, _countof(aeskey), key.data(), min(key.size(), (size_t)sizeof(aeskey)));
#else
memcpy(aeskey, key.data(), std::min(key.size(), (size_t)sizeof(aeskey)));
memcpy(aeskey, key.data(), min(key.size(), (size_t)sizeof(aeskey)));
#endif
e.SetKey(aeskey, sizeof(aeskey));

std::string output;
string output;
StringSource ss(tmp, true,
new StreamTransformationFilter(e,
new StringSink(output)
Expand Down