Skip to content

Instantly share code, notes, and snippets.

@apselon
Created December 9, 2020 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apselon/cd2ece623b4d345d86d74482bf287480 to your computer and use it in GitHub Desktop.
Save apselon/cd2ece623b4d345d86d74482bf287480 to your computer and use it in GitHub Desktop.
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>
enum { MAX_SIZE = 4096 };
int main(int argc, char* argv[])
{
unsigned char data[MAX_SIZE] = "";
int num_read = 0;
num_read = fread(data, sizeof(data[0]), MAX_SIZE, stdin);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
unsigned char key[32], iv[32];
unsigned char* password = (unsigned char*)argv[1];
size_t pass_len = strlen(argv[1]);
EVP_BytesToKey(
EVP_aes_256_cbc(),
EVP_sha256(),
data + 8,
password,
pass_len,
1,
key,
iv);
EVP_DecryptInit(ctx, EVP_aes_256_cbc(), key, iv);
unsigned char decrypted[MAX_SIZE] = "";
int p_len = num_read;
EVP_DecryptUpdate(ctx, decrypted, &p_len, data, num_read);
int num_decrypted = 0;
EVP_DecryptFinal(ctx, decrypted + p_len, &num_decrypted);
//fwrite(decrypted, sizeof(decrypted[0]), num_decrypted + p_len, stdout);
printf("%s", (char*)decrypted);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment