#include "aes_pack.h"
#include <string.h>
AesPack::AesPack()
{}
AesPack::~AesPack()
{}
int AesPack::encrypt(unsigned char *key,
unsigned char *plaintext,
const unsigned int plaintext_len,
unsigned char *ciphertext,
unsigned int *ciphertext_len)
{
EVP_CIPHER_CTX *ctx;
unsigned int len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) goto end;
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if (1 != EVP_EncryptInit_ex(ctx, _mode, NULL, _key.data(), _iv.data())) goto end;
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_EncryptUpdate(ctx, ciphertext, reinterpret_cast<int*>(&len), plaintext, plaintext_len)) goto end;
*ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, reinterpret_cast<int*>(&len))) goto end;
*ciphertext_len += len;
/* Clean up */
if (ctx) EVP_CIPHER_CTX_free(ctx);
return OK;
end:
ERR_print_errors_fp(stderr);
/* Clean up */
if (ctx) EVP_CIPHER_CTX_free(ctx);
return FAIL;
}
int AesPack::decrypt(unsigned char *key,
unsigned char *ciphertext,
const unsigned int ciphertext_len,
unsigned char *plaintext,
unsigned int *plaintext_len)
{
EVP_CIPHER_CTX *ctx;
unsigned int len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) goto end;
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if (1 != EVP_DecryptInit_ex(ctx, _mode, NULL, _key.data(), _iv.data())) goto end;
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_DecryptUpdate(ctx, plaintext, reinterpret_cast<int*>(&len), ciphertext, ciphertext_len)) goto end;
*plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, reinterpret_cast<int*>(&len))) goto end;
*plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return OK;
end:
ERR_print_errors_fp(stderr);
/* Clean up */
if (ctx) EVP_CIPHER_CTX_free(ctx);
return FAIL;
}
int main(void)
{
/* Set up the key and iv. Do I need to say to not hard code these in a
* real application? :-)
*/
/* A 256 bit key */
//unsigned char *key = (unsigned char *)"673d9a02137ce81cbde3b98d87e41f42ce3e8a2633bd7741a6e21672b1da2104";
unsigned char key[]={0x67,0x3d,0x9a,0x02,0x13,0x7c,0xe8,0x1c,0xbd,0xe3,0xb9,0x8d,0x87,0xe4,0x1f,0x42,0xce,0x3e,0x8a,0x26,0x33,0xbd,0x77,0x41,0xa6,0xe2,0x16,0x72,0xb1,0xda,0x21,0x04};
/* A 128 bit IV */
//unsigned char *iv = (unsigned char *)"e488c61c6a93a87b9a7dfe29ae3c8c1a";
unsigned char iv[] = {0xe4,0x88,0xc6,0x1c,0x6a,0x93,0xa8,0x7b,0x9a,0x7d,0xfe,0x29,0xae,0x3c,0x8c,0x1a};
/* Message to be encrypted */
//const char *plaintext="Gwang**02";
unsigned char plaintext[]={0x47,0x77,0x61,0x6e,0x67,0x2a,0x2a,0x30,0x32};
/* Buffer for ciphertext. Ensure the buffer is long enough for the
* ciphertext which may be longer than the plaintext, dependant on the
* algorithm and mode
*/
unsigned char ciphertext[512];
/* Buffer for the decrypted text */
unsigned char decryptedtext[512];
unsigned int decryptedtext_len, ciphertext_len;
AesPack aesPack;
aesPack.setKey(key, 32);
aesPack.setIV(iv, 16);
aesPack.setMode(EVP_aes_256_cbc());
/* Encrypt the plaintext */
aesPack.encrypt(key, plaintext, sizeof(plaintext), ciphertext, &ciphertext_len);
/* Do something useful with the ciphertext here */
printf("Ciphertext is:\n");
BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);
/* Decrypt the ciphertext */
aesPack.decrypt(key, ciphertext, ciphertext_len, decryptedtext, &decryptedtext_len);
/* Add a NULL terminator. We are expecting printable text */
decryptedtext[decryptedtext_len] = '\0';
/* Show the decrypted text */
printf("Decrypted text is:\n");
BIO_dump_fp(stdout, (const char *)decryptedtext, decryptedtext_len);
if (!memcmp(&plaintext[0], &decryptedtext[0], sizeof(plaintext))) {
printf("pass!\n");
}
return 0;
}