0

I currently am using the following program for encrypting files using AES:

#include <openssl/aes.h>

void encrypt(FILE *ifp, FILE *ofp){
  int bytes_read, bytes_written;
  unsigned char indata[AES_BLOCK_SIZE];
  unsigned char outdata[AES_BLOCK_SIZE];

  /* ckey and ivec are the two 128-bits keys necesary to
     en- and recrypt your data. Note that ckey can be
     192 or 256 bits as well */
  unsigned char ckey[] =  "thiskeyisverybad";
  unsigned char ivec[] = "dontusethisinput";

  /* data structure that contains the key itself */
  AES_KEY key;

  /* set the encryption key */
  AES_set_encrypt_key(ckey, 128, &key);

  /* set where on the 128 bit encrypted block to begin encryption*/
  int num = 0;

  while (1) {
    bytes_read = fread(indata, 1, AES_BLOCK_SIZE, ifp);

    AES_cfb128_encrypt(indata, outdata, bytes_read, &key, ivec, &num,
           AES_ENCRYPT);

    bytes_written = fwrite(outdata, 1, bytes_read, ofp);
    if (bytes_read < AES_BLOCK_SIZE)
  break;
  }
}

However, how can I make this program encrypt all the files in the same folder as the running program itself, instead of taking a file as input?

rshah
  • 675
  • 2
  • 12
  • 32
  • 2
    Basically you create a parent function that fetches all filenames and calls your encryption routine for each file. The base path is `argv[0]` less the program name. – Paul Ogilvie Dec 11 '19 at 09:48
  • 2
    Instead of the `FILE` pointers, pass a single filename. The function encrypts to a temporary file, upon success removes the input file and renames the temporary file to the input filename. You may also want to set the file dates to the original - the contents hasn't changed. – Paul Ogilvie Dec 11 '19 at 09:50

0 Answers0