Skip to content

Instantly share code, notes, and snippets.

@SamuelTulach
Created August 17, 2020 18:21
Show Gist options
  • Save SamuelTulach/29eb6d67e342befea9b41411f5578bbf to your computer and use it in GitHub Desktop.
Save SamuelTulach/29eb6d67e342befea9b41411f5578bbf to your computer and use it in GitHub Desktop.
#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