Created
October 9, 2017 16:58
-
-
Save remi6397/4cd538bf51d93e0c7ad3c4e73e5d6d76 to your computer and use it in GitHub Desktop.
Simple encryption using tiny-aes
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 <stdlib.h> | |
#include <string> | |
#include "../../../../../3rdparty/tiny-AES-c/aes.h" | |
#define AES_BLOCK_SIZE 16 | |
uint8_t* EncryptString(std::string input, std::string key) | |
{ | |
int decryptedLength = input.size(); | |
int keyLength = key.size()+(32-(key.size()%32)); | |
uint8_t* keyBuffer = (uint8_t*)malloc(keyLength); | |
for (size_t i = 0; i < (size_t)keyLength; ++i) | |
{ | |
if (i >= (size_t)(key.size())) | |
{ | |
keyBuffer[i] = 0x00; | |
continue; | |
} | |
keyBuffer[i] = (uint8_t)(key.c_str()[i]); | |
} | |
uint8_t* ivBuffer; | |
ivBuffer = (uint8_t*)malloc(AES_BLOCK_SIZE); | |
srand(time(nullptr)); | |
for (size_t i = 0; i < AES_BLOCK_SIZE; i++) | |
{ | |
ivBuffer[i] = rand(); | |
} | |
int paddedLength = decryptedLength+(64-(decryptedLength%64)); | |
uint8_t* decryptedBuffer = (uint8_t*)malloc(paddedLength); | |
for (size_t i = 0; i < (size_t)paddedLength; ++i) | |
{ | |
if (i >= (size_t)decryptedLength) | |
{ | |
decryptedBuffer[i] = 0x00; | |
continue; | |
} | |
decryptedBuffer[i] = (uint8_t)(input.c_str()[i]); | |
} | |
uint8_t* encryptedBuffer; | |
encryptedBuffer = (uint8_t*)malloc(paddedLength); | |
AES_CBC_encrypt_buffer(encryptedBuffer, decryptedBuffer, (uint8_t)paddedLength, keyBuffer, ivBuffer); | |
uint8_t* outBuffer = (uint8_t*)malloc(1+AES_BLOCK_SIZE+paddedLength); | |
outBuffer[0] = (uint8_t)paddedLength; | |
for (size_t i = 0; i < AES_BLOCK_SIZE; i++) | |
{ | |
outBuffer[1+i] = ivBuffer[i]; | |
} | |
for (size_t i = 0; i < (size_t)paddedLength; i++) | |
{ | |
outBuffer[1+AES_BLOCK_SIZE+i] = (uint8_t)(encryptedBuffer[i]); | |
} | |
return outBuffer; | |
} | |
std::string DecryptString(uint8_t* input, std::string key) | |
{ | |
uint8_t decryptedLength = input[0]; | |
uint8_t* ivBuffer = (uint8_t*)(input + 1); | |
uint8_t* encryptedBuffer = (uint8_t*)(input + 1 + AES_BLOCK_SIZE); | |
int keyLength = key.size()+(32-(key.size()%32)); | |
uint8_t* keyBuffer = (uint8_t*)malloc(keyLength); | |
for (size_t i = 0; i < (size_t)keyLength; ++i) | |
{ | |
if (i >= (size_t)(key.size())) | |
{ | |
keyBuffer[i] = 0x00; | |
continue; | |
} | |
keyBuffer[i] = (uint8_t)(key.c_str()[i]); | |
} | |
uint8_t* decryptedBuffer; | |
decryptedBuffer = (uint8_t*)malloc(decryptedLength); | |
AES_CBC_decrypt_buffer(decryptedBuffer, encryptedBuffer, (uint8_t)decryptedLength, keyBuffer, ivBuffer); | |
char* decryptedCharAry = (char*)malloc(decryptedLength+1); | |
for (size_t i = 0; i < (size_t)decryptedLength; i++) | |
{ | |
decryptedCharAry[i] = (uint8_t)decryptedBuffer[i]; | |
} | |
decryptedCharAry[decryptedLength] = '\0'; | |
return std::string(decryptedCharAry); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment