Friday, November 29, 2013

Encrypting files with AES in R

The recent news about the widespread NSA electronic spying renewed people interest in securing their data.

R seems an unlikely tool for data encryption, but actually can be used for this purpose.

The digest package provides implementation of two crucial cryptographic algorithms:

By joining these two algorithms, it is possible to write a rudimentary code performing encryption/decryption of virtually any file.

The code first creates a 256-bit hash from an arbitrary key-phrase, using SHA-256. This hash key is used as a key to encrypt/decrypt a file using AES-256.

An additional feature of the code is in-memory compression of the data before encryption.

Also, since AES-256 requires 16-bytes blocks of data, some random information may be glued to the file before encryption. 

Definitely this is not the best-in-class implementation of the symmetric encryption, but for many purposes it may be good enough.

A small warning: in-memory decompression may return an error when one tries to decrypt a file with incorrect key-phrase.