Skip to content

Instantly share code, notes, and snippets.

@apselon
Created December 9, 2020 16:27
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/485b4965e109cc7e6de324134c4b8d9f to your computer and use it in GitHub Desktop.
Save apselon/485b4965e109cc7e6de324134c4b8d9f 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 };
enum ErrorCodes {SUCCESS, BAD_ARG };
int main(int argc, char* argv[])
{
if (argc - 1 < 1 ) {
return BAD_ARG;
}
unsigned char data[MAX_SIZE] = "";
int num_read = 0;
num_read = fread(data, sizeof(data[0]), MAX_SIZE, stdin);
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);
printf("Salt=");
for (size_t i = 0; i < 8; ++i) {
printf("%02x", (data + 8)[i]);
}
printf("!\n");
printf("key=");
for (size_t i = 0; i < 32; ++i) {
printf("%02x", (key)[i]);
}
printf("!\n");
printf("iv=");
for (size_t i = 0; i < 32; ++i) {
printf("%02x", (iv)[i]);
}
printf("!\n");
return 0;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
int len = 0;
int plaintext_len = 0;
unsigned char plaintext[MAX_SIZE] = "";
EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
//EVP_CIPHER_CTX_set_key_length(ctx, EVP_MAX_KEY_LENGTH);
EVP_DecryptUpdate(ctx, plaintext, &len, data, num_read);
EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment