Next: , Previous: Warnings, Up: Top



5 Writing Encrypted Files

libencio conceptually follows stdio in the naming and function of its function calls. This short example program demonstrates how to write an encrypted file with default encryption settings (rijndael-128/cfb mode/sha1 checksum).

#include <stdio.h>
#include "encio.h"

int main(int argc, char **argv)
{
  ENCFILE *ef;
  char buf[1000];
  char *passphrase = "acomplicatedpassphrase";

  /* open an file encrypted with passphrase */
  ef = enc_fopen("test.txt.nc", "wb", passphrase);

  while(!feof(stdio))
  {
    int nread = fread(stdio, 1, buf, 1000)
    enc_fwrite(ef, 1, buf, nread);
  }

  enc_fclose(ef);
}

This short example program encrypts anything given to it from stdio into a file named test.txt.nc. All enc_* calls function are equivalent to their stdio counterparts, except they transparently encrypt the data written to the output file. There is an additional third parameter to enc_fopen(), compared to fopen(), through which you supply the passphrase to use to encrypt the data.

Note that enc_fseek() is not available when a file is opened in write mode – i.e., you cannot write 10 kb, then seek back to the beginning and rewrite 1 kb. This is due to the properties of the encryption mode, where each subsequent byte written depends on all of the preceeding bytes - changing them would require reencryption of the whole file.