0

I am trying to encrypt/decrypt using AES 256 CBC mode and I am stuck on how you can do it with files. It is required that the file be read in blocks of 4096 bytes and encrypt/decrypt and store the result back to a file. Here is my code.

Previously I was able to do it for random hex strings. This program can generate a random key and plain hex and encrypt/decrypt perfectly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

int main(int argc, char **argv)
{
    int keylength = 256;

    unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char)* (keylength / 8));
    memset(aes_key, 0, keylength / 8);
    if (!RAND_bytes(aes_key, keylength / 8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char)*inputslength);
    memset(aes_input, 'X', inputslength);

    unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char)*AES_BLOCK_SIZE), *iv_dec = (unsigned char*)malloc(sizeof(unsigned char)*AES_BLOCK_SIZE);
    RAND_bytes(iv_enc, AES_BLOCK_SIZE);
    memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);

    const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char)*encslength);
    unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char)*inputslength);
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, inputslength);

    printf("encrypt:\t");
    hex_print(enc_out, encslength);

    printf("decrypt:\t");
    hex_print(dec_out, inputslength);

    // free memory here

    return 0;
}

Now I need to pass a file in 4096 byte increments and be able to write the output back to an output file. I have the following

int main(int argc, char *argv[])
{
    FILE *fp_in, *fp_out;
    int c;
    funct = argv[1];
    /* Check command-line */
    if (argc != 3) {
        printf("EXAMPLE USAGE: ./mycp enc/dec <INFILEE> <OUTFILE>\n");
        return 1;
    }

    /* Open files */
    if (!(fp_in = fopen(argv[2], "rb"))) {
        perror("input file open error");
        return 1;
    }

    if (!(fp_out = fopen(argv[3], "wb"))) {
        perror("output file open error");
        return 1;
    }

    fseek(storeFile, blocksToRead[i] * 4096, SEEK_SET);
    int n = fread(ptr, 4096, 1, fp_in);
    if (funct == "enc")
    {
        enc();          //encrypt
    }
    else if (funct == "dec")
    {
        dec();          //decrypt
    }

    if (!(funct = argv[1], "wb"))) {
        perror("output file open error");
        return 1;
    }
    if (fwrite(ptr, n, fp_out) != n)
    {
        printf("write error\n");
        return -1;
    }
    fclose(fp_out);
    fclose(fp_in);
    return 0;
}
jww
  • 97,681
  • 90
  • 411
  • 885
Paul
  • 1
  • 2
  • Possible duplicate of [OpenSSL AES 256 CBC via EVP api in C](http://stackoverflow.com/questions/24856303/openssl-aes-256-cbc-via-evp-api-in-c). You should probably be using authenticated encryption because it provides *both* confidentiality and authenticity. See [EVP Authenticated Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption) on the OpenSSL wiki. – jww May 21 '15 at 21:10
  • You should *not* use `AES_encrypt` and friends. That's a software-only implementation, so you will not enjoy hardware support, like AES-NI. You should be using `EVP_*` functions. See [EVP Symmetric Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) on the OpenSSL wiki. In fact, you should probably be using authenticated encryption because it provides *both* confidentiality and authenticity. See [EVP Authenticated Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption) on the OpenSSL wiki. – jww May 21 '15 at 22:21

0 Answers0