Created
November 6, 2018 18:14
-
-
Save maldevel/ab4bf2ca4645172ba6f262e36789da6c to your computer and use it in GitHub Desktop.
Dexter base 64 encoding snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://github.com/twelvesec/dexter | |
//GNU General Public License v3.0 | |
//@maldevel | |
//... | |
std::string libencode::base64_encode(std::string plaintext) { | |
std::string encodedtext; | |
DWORD size = 0; | |
char *dest; | |
if (CryptBinaryToStringA((BYTE*)plaintext.c_str(), (DWORD)plaintext.length(), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &size) == FALSE) { | |
return ""; | |
} | |
if ((dest = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + 1)) == NULL) { | |
return ""; | |
} | |
if (CryptBinaryToStringA((BYTE*)plaintext.c_str(), (DWORD)plaintext.length(), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &size) == FALSE) | |
{ | |
HeapFree(GetProcessHeap(), 0, dest); | |
dest = NULL; | |
return ""; | |
} | |
dest[size] = 0; | |
encodedtext = std::string(dest); | |
HeapFree(GetProcessHeap(), 0, dest); | |
dest = NULL; | |
return encodedtext; | |
} | |
std::string libencode::base64_decode(std::string encodedtext) { | |
std::string plaintext; | |
DWORD size = 0; | |
BYTE *dest; | |
if (CryptStringToBinaryA(encodedtext.c_str(), (DWORD)encodedtext.length(), CRYPT_STRING_BASE64, NULL, &size, NULL, NULL) == FALSE) { | |
return ""; | |
} | |
if ((dest = (BYTE*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + 1)) == NULL) { | |
return ""; | |
} | |
if (CryptStringToBinaryA(encodedtext.c_str(), (DWORD)encodedtext.length(), CRYPT_STRING_BASE64, dest, &size, NULL, NULL) == FALSE) { | |
HeapFree(GetProcessHeap(), 0, dest); | |
dest = NULL; | |
return ""; | |
} | |
dest[size] = 0; | |
plaintext = std::string((char*)dest); | |
HeapFree(GetProcessHeap(), 0, dest); | |
dest = NULL; | |
return plaintext; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment