Created
August 17, 2020 18:21
-
-
Save SamuelTulach/29eb6d67e342befea9b41411f5578bbf to your computer and use it in GitHub Desktop.
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
#include <vector> | |
#include <string> | |
#include "shared.h" | |
#include "AES.h" | |
#include "encryption.h" | |
#include "base64.h" | |
int Encryption::GetExpectedSize() | |
{ | |
Packet packet = { 0 }; | |
auto encoded = Encrypt(packet); | |
return encoded.length(); | |
} | |
std::string Encryption::Encrypt(Packet packet) | |
{ | |
unsigned outLength = 0; | |
auto* encrypted = aes.EncryptECB(reinterpret_cast<unsigned char*>(&packet), sizeof(Packet), key, outLength); | |
auto base64 = Base64::encode(encrypted, outLength); | |
return base64; | |
} | |
Packet Encryption::Decrypt(std::string input) | |
{ | |
if (input.length() != GetExpectedSize()) | |
{ | |
printf("[encryption] Invalid packet size\n"); | |
Packet packet = { 0 }; | |
return packet; | |
} | |
auto base64 = Base64::decode(input); | |
auto* decrypted = aes.DecryptECB(base64.data(), base64.size(), key); | |
Packet packet; | |
memcpy(&packet, decrypted, sizeof(packet)); | |
return packet; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment