A number of existing
R packages support data encryption. However, we haven’t found one that easily suits our needs: to encrypt one or many columns of a data frame or tibble using a private/public key pair in
tidyversefunctions. The emphasis is on the easily.
Encrypting and decrypting data securely is important when it comes to healthcare and sociodemographic data. We have developed a simple and secure package
encryptyr which allows non-experts to encrypt and decrypt columns of data.
There is a simple and easy-to-follow vignette available on our GitHub page which guides you through the process of using
Confidential data – security challenges
Data containing columns of disclosive or confidential information such as a postcode or a patient ID (CHI in Scotland) require extreme care. Storing sensitive information as raw values leaves the data vulnerable to confidentiality breaches.
It is best to just remove confidential information from the records whenever possible. However, this can mean the data can never be re-associated with an individual. This may be a problem if, for example, auditors of a clinical trial need to re-identify an individual from the trial data.
One potential solution currently in common use is to generate a study number which is linked to the confidential data in a separate lookup table, but this still leaves the confidential data available in another file.
Encryptr package solution – storing encrypted data
encryptr package allows users to store confidential data in a pseudoanonymised form, which is far less likely to result in re-identification.
The package allows users to create a public key and a private key to enable RSA encryption and decryption of the data. The public key allows encryption of the data. The private key is required to decrypt the data. The data cannot be decrypted with the public key. This is the basis of many modern encryption systems.
When creating keys, the user sets a password for the private key using a dialogue box. This means that the password is included in an
R script. We recommend creating a secure password with a variety of alphanumeric characters and symbols.
As the password is not stored, it is important that you are able to remember it if you need to decrypt the data later.
Once the keys are created it is possible to encrypt one or more columns of data in a data frame or tibble using the public key. Every time RSA encryption is used it will generate a unique output. Even if the same information is encrypted more than once, the output will always be different. It is not possible therefore to match two encrypted values.
These outputs are also secure from decryption without the private key. This may allow sharing of data within or between research teams without sharing confidential data.
Caution: data often remains potentially disclosive (or only pseudoanomymised) even after encryption of identifiable variables and all of the required permissions for usage and sharing of data must still be in place.
Encryptr package – decrypting the data
Sometimes decrypting data is necessary. For example, participants in a clinical trial may need to be contacted to explain a change or early termination of the trial.
encryptr package allows users to securely and reliably decrypt the data. The decrypt function will use the private key to decrypt one or more columns. The user will be required to enter the password created when the keys were generated.
As the private key is able to decrypt all of the data, we do not recommend sharing this key.
Blinding and unblinding clinical trials – another encryptr package use
Often when working with clinical trial data, the participants are randomised to one or more treatment groups. Often teams working on the trial are unaware of the group to which patients were randomised (blinded).
Using the same method of encryption, it is possible to encrypt the participant allocation group, allowing the sharing of data without compromising blinding. If other members of the trial team are permitted to see treatment allocation (unblinded), then the decryption process can be followed to reveal the group allocation.
What this is not
This is a simple set of wrappers of
openssl aimed at non-experts. It does not seek to replace the many excellent encryption packages available in
R, such as
safer. We believe however that it makes things much easier. Comments and forks welcome.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…