Skip to content

Instantly share code, notes, and snippets.

@realbardia
Created September 29, 2024 07:24
Show Gist options
  • Save realbardia/71b50f8a62906c73d804f6e25d7fc835 to your computer and use it in GitHub Desktop.
Save realbardia/71b50f8a62906c73d804f6e25d7fc835 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt using OpenSSL's crypto.so library
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <string.h>
#include <cstring>
#include <stdlib.h>
#include <iostream>
using namespace std;
int aes_deep_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) {
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
if(!(ctx = EVP_CIPHER_CTX_new()))
throw false;
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
{
EVP_CIPHER_CTX_free(ctx);
throw false;
}
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
{
EVP_CIPHER_CTX_free(ctx);
throw false;
}
ciphertext_len = len;
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
{
EVP_CIPHER_CTX_free(ctx);
throw false;
}
ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int aes_deep_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
if(!(ctx = EVP_CIPHER_CTX_new()))
throw false;
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
{
EVP_CIPHER_CTX_free(ctx);
throw false;
}
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
{
EVP_CIPHER_CTX_free(ctx);
throw false;
}
plaintext_len = len;
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
plaintext_len = 0;
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
void aes_hashPassword(const char *string, char outputBuffer[65])
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
# define EVP_MD_CTX_new EVP_MD_CTX_create
# define EVP_MD_CTX_free EVP_MD_CTX_destroy
#endif
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
unsigned int hashSize = 65;
unsigned char * tempOutBuff = (unsigned char *) malloc(hashSize * sizeof(unsigned char));
EVP_DigestInit(ctx, EVP_sha256());
EVP_DigestUpdate(ctx, string, strlen(string));
EVP_DigestFinal(ctx, tempOutBuff, &hashSize);
for(unsigned int i = 0 ; i < hashSize ; i++) {
sprintf(outputBuffer + (i * 2), "%02x", tempOutBuff[i]);
}
outputBuffer[64] = 0;
free(tempOutBuff);
}
string aes_encrypt(const string &plainText, const string &pass)
{
char *hash = (char *) malloc(sizeof(char) * 65);
unsigned char *cipher = (unsigned char *) malloc(sizeof(unsigned char) * (plainText.size() + 256));
aes_hashPassword((const char *)pass.c_str(), hash);
string hashStr(hash);
const auto keyStr = hashStr.substr(0, 16);
const auto iv = hashStr.substr(16, 16);
string res;
try {
int cipherSize = aes_deep_encrypt(
(unsigned char *) plainText.c_str(),
plainText.size(),
(unsigned char *) keyStr.c_str(),
(unsigned char *) iv.c_str(),
cipher
);
res = string((char *) cipher, cipherSize);
} catch(...) {
}
free(cipher);
free(hash);
return res;
}
string aes_decrypt(const string &cipher, const string &pass)
{
unsigned char *plainTextBytes = (unsigned char *) malloc(sizeof(unsigned char) * (cipher.size() + 256));
char *hash = (char *) malloc(sizeof(char) * 65);
aes_hashPassword((const char *)pass.c_str(), hash);
string hashStr(hash);
const auto keyStr = hashStr.substr(0, 16);
const auto iv = hashStr.substr(16, 16);
string plainText;
try {
auto len = aes_deep_decrypt(
(unsigned char *) cipher.c_str(),
cipher.size(),
(unsigned char *) keyStr.c_str(),
(unsigned char *) iv.c_str(),
plainTextBytes
);
plainText = string((char *) plainTextBytes, len);
} catch(...) {
}
free(hash);
free(plainTextBytes);
return plainText;
}
int main()
{
string test = "Bardia";
const auto enc = aes_encrypt(test, "12345");
const auto dec = aes_decrypt(enc, "12345");
std::cout << enc << std::endl;
std::cout << dec << std::endl;
return 0;
}
g++ main.cpp -lcrypto -o app
./app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment