From b5dd095f273ee0d6743cdebbac20322e7c24cbee Mon Sep 17 00:00:00 2001 From: lightbulblighter <59720715+lightbulblighter@users.noreply.github.com> Date: Tue, 7 Jun 2022 03:01:14 -0700 Subject: [PATCH] safer Crypt initializer --- PolygonDLL/Hooks/Crypt.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/PolygonDLL/Hooks/Crypt.cpp b/PolygonDLL/Hooks/Crypt.cpp index 58e6eda..80d73ce 100644 --- a/PolygonDLL/Hooks/Crypt.cpp +++ b/PolygonDLL/Hooks/Crypt.cpp @@ -4,7 +4,6 @@ #include "Util.h" #include "Hooks/Crypt.h" - Crypt::Crypt() { if (!CryptAcquireContext(&context, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) @@ -22,18 +21,29 @@ Crypt::Crypt() } } + try + { #ifdef _DEBUG - std::vector publicKey = Util::base64Decode(Util::publicKey); + std::vector publicKey = Util::base64Decode(Util::publicKey); #else - std::vector publicKey = Util::publicKey; + std::vector publicKey = Util::publicKey; #endif - BYTE* blob = new BYTE[publicKey.size()]; - std::copy(publicKey.begin(), publicKey.end(), blob); + BYTE* blob = new BYTE[publicKey.size()]; + std::copy(publicKey.begin(), publicKey.end(), blob); - if (!CryptImportKey(context, blob, publicKey.size(), 0, 0, &key)) + if (!CryptImportKey(context, blob, publicKey.size(), 0, 0, &key)) + { + throw std::runtime_error(""); + } + } + catch (...) { +#ifdef _DEBUG + throw std::runtime_error("Failed to import public key"); +#else throw std::runtime_error("Error during CryptImportKey"); +#endif } }