On Sun October 30 2011, Ananthasayanan Kandiah wrote:
> #include <stdio.h>
> #include <string.h>
> #include <openssl/aes.h>
> #include <openssl/bio.h>
>
> #define KEY_SIZE 16
>
> int main(void)
> {
> int i;
> AES_KEY key;
> BIO* bio_out;
>
> unsigned char key_data[KEY_SIZE] = {
> 0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
> 0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
> };
>
Ask the compiler to help you:
mszick@wolf466:~/crypto$ gcc -Wall -E aes_test.c | grep "_SIZE"
aes_test.c:6:9: error: macro names must be identifiers
unsigned char key_data[KEY_SIZE] = {
AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
Mike
> unsigned char iv[AES_BLOCK_SIZE];
>
> unsigned char const iv_data[AES_BLOCK_SIZE] = {
> 0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
> 0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
> };
>
> char* data = "Internet is a wonderful mechanism for making a fool
> "
> "of yourself in front of a very large audience";
>
>
> int length = (int) strlen(data);
>
> int num = 0;
>
> /* Allocate some space for the ciphertext and plaintext */
> char* ciphertext = (char*) malloc(sizeof(char) * length);
> char* plaintext = (char*) malloc(sizeof(char) * length);
>
> /* Copy the IV data to the IV array */
> memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
> /* Set the encrypt key structure using the predefined key */
> AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
>
> /* Carry out the encryption */
> AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num, AES_ENCRYPT);
>
> /* Setup output */
> bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
>
> BIO_printf(bio_out, "Original plaintext: %s\n\n", data);
>
> BIO_printf(bio_out, "Ciphertext: ");
>
> /* Print out the ciphertext */
> for (i = 0; i < length; i++)
> BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);
>
> BIO_printf(bio_out, "\n\n");
>
> /* Start the decryption process */
>
> /* First, copy the original IV data back to the IV array - as it was
> overwritten
> * during the encryption process
> */
> memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
> /* Reset how far we've gone through the IV */
> num = 0;
>
> /* Carry out the decryption */
> AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
> AES_DECRYPT);
>
> BIO_printf(bio_out, "Recovered plaintext: ");
>
> /* print out the plaintext */
> for (i = 0; i < length; i++)
> BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);
>
> BIO_printf(bio_out, "\n\n");
>
> BIO_free(bio_out);
>
> free(ciphertext);
> free(plaintext);
>
>
> return 0;
> }
>
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [email protected]
Automated List Manager [email protected]