Skip to content

Instantly share code, notes, and snippets.

@GoGim1
Last active November 14, 2022 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GoGim1/77c9bebec1cc71cea066515b4623a051 to your computer and use it in GitHub Desktop.
Save GoGim1/77c9bebec1cc71cea066515b4623a051 to your computer and use it in GitHub Desktop.
OpenSSL AES Decryption fails randomly C++
#include <iostream>
#include <fstream>
#include <string>
#include <openssl/aes.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
using namespace std;
bool aesEncrypt(const std::string input, const std::string& key, std::string& output) {
int ret = 0, len1 = 0, len2 = 0;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
ret = EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key.c_str(), NULL);
if (ret != 1) {
EVP_CIPHER_CTX_free(ctx);
return false;
}
string u;
u.resize(input.size() + EVP_CIPHER_CTX_block_size(ctx));
ret = EVP_EncryptUpdate(ctx, (unsigned char *)u.data(), &len1, (const unsigned char*)input.c_str(), input.size());
u.resize(len1);
if (ret != 1) {
EVP_CIPHER_CTX_free(ctx);
return false;
}
string f;
f.resize(EVP_CIPHER_CTX_block_size(ctx));
ret = EVP_EncryptFinal_ex(ctx, (unsigned char *)f.data(), &len2);
f.resize(len2);
if (ret != 1) {
EVP_CIPHER_CTX_free(ctx);
return false;
}
output = u + f;
EVP_CIPHER_CTX_free(ctx);
return true;
}
// 128 ecb
bool aesDecrypt(const std::string input, const std::string& key, std::string& output) {
int ret = 0, len1 = 0, len2 = 0;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
ret = EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key.c_str(), NULL);
if (ret != 1) {
EVP_CIPHER_CTX_free(ctx);
return false;
}
string u;
u.resize(input.size() + EVP_CIPHER_CTX_block_size(ctx));
ret = EVP_DecryptUpdate(ctx, (unsigned char *)u.data(), &len1, (const unsigned char*)input.c_str(), input.size());
u.resize(len1);
if (ret != 1) {
EVP_CIPHER_CTX_free(ctx);
return false;
}
string f;
f.resize(EVP_CIPHER_CTX_block_size(ctx));
ret = EVP_DecryptFinal_ex(ctx, (unsigned char *)f.data(), &len2);
f.resize(len2);
if (ret != 1) {
EVP_CIPHER_CTX_free(ctx);
return false;
}
output = u + f;
EVP_CIPHER_CTX_free(ctx);
return true;
}
int main(int argc, char* argv[]) {
std::ifstream file(argv[1]);
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
string encrypted_content;
if (!aesEncrypt(content, "input", encrypted_content)) return -1;
// Note: decrypt failed randomly if uncomment
// std::ofstream encrypted_file(argv[2]);
// encrypted_file << encrypted_content;
// encrypted_file.close();
string decrypted_content;
if (!aesDecrypt(encrypted_content, "input", decrypted_content)) {
cout << "decrypt failed." << endl;
return -1;
}
cout << decrypted_content << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment