Created
July 8, 2017 06:30
-
-
Save JaciBrunning/b1dd4e3077d6a5d00e799ab66bcf0ca3 to your computer and use it in GitHub Desktop.
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 <string.h> | |
#include <stdio.h> | |
#include <openssl/bio.h> | |
#include <openssl/evp.h> | |
#include <assert.h> | |
typedef struct { | |
char *test_string; | |
} Config; | |
char compkey[] = "dsS`p%%Tu\"zT70=F*Mm7ivx[T~Mr@HcX"; | |
// --- BASE64 --- // | |
size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded string | |
size_t len = strlen(b64input), | |
padding = 0; | |
if (b64input[len-1] == '=' && b64input[len-2] == '=') //last two chars are = | |
padding = 2; | |
else if (b64input[len-1] == '=') //last char is = | |
padding = 1; | |
return (len*3)/4 - padding; | |
} | |
int Base64Decode(char* b64message, unsigned char** buffer, size_t* length) { //Decodes a base64 encoded string | |
BIO *bio, *b64; | |
int decodeLen = calcDecodeLength(b64message); | |
*buffer = (unsigned char*)malloc(decodeLen + 1); | |
(*buffer)[decodeLen] = '\0'; | |
bio = BIO_new_mem_buf(b64message, -1); | |
b64 = BIO_new(BIO_f_base64()); | |
bio = BIO_push(b64, bio); | |
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer | |
*length = BIO_read(bio, *buffer, strlen(b64message)); | |
assert(*length == decodeLen); //length should equal decodeLen, else something went horribly wrong | |
BIO_free_all(bio); | |
return (0); //success | |
} | |
// --- AES 256 CBC --- // | |
unsigned char *aes_decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len, unsigned char *key, unsigned char *iv) { | |
int p_len = *len, f_len = 0; | |
unsigned char *plaintext = (unsigned char *)malloc(p_len); | |
EVP_CIPHER_CTX_init(e); | |
EVP_DecryptInit_ex(e, EVP_aes_256_cbc(), NULL, key, iv); | |
EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL); | |
EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len); | |
EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len); | |
*len = p_len + f_len; | |
return plaintext; | |
} | |
// --- MAIN --- // | |
int main(int argc, char *argv[]) { | |
int arg, decode_len, decrypt_len; | |
Config c; | |
unsigned char *decode_string, *decrypt_string; | |
unsigned char iv[32], ciphertext[1024]; | |
EVP_CIPHER_CTX ctx; | |
for (arg = 0; arg < argc; arg++) { | |
if (!strcmp(argv[arg], "-t")) { | |
arg++; | |
c.test_string = argv[arg]; | |
} | |
} | |
printf("\n-- CONFIG --\n"); | |
printf(" Test String: %s\n", c.test_string); | |
Base64Decode(c.test_string, &decode_string, (size_t *)&decode_len); | |
strncpy((char *)iv, (const char *)decode_string, 16); | |
strcpy((char *)ciphertext, (const char *)decode_string+16); | |
decrypt_len = decode_len-16; | |
decrypt_string = aes_decrypt(&ctx, ciphertext, &decrypt_len, (unsigned char *)compkey, iv); | |
printf(" Decryted String: %s\n", (char *)decrypt_string); | |
free(decrypt_string); | |
free(decode_string); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment