rencryptionrdata

How to protect/encrypt R objects in RData files due to EU-GDPR


I want to protect the content of my RData files with a strong encryption algorithm since they may contain sensitive personal data which must not be disclosed due to (legal) EU-GDPR requirements.

How can I do this from within R?

I want to avoid a second manual step to encrypt the RData files after creating them to minimize the risk of forgetting it or overlooking any RData files.

I am working with Windows in this scenario...


Solution

  • library(openssl)
    
    x <- serialize(list(1,2,3), NULL)
    
    passphrase <- charToRaw("This is super secret")
    key <- sha256(passphrase)
    
    encrypted_x <- aes_cbc_encrypt(x, key = key)
    
    saveRDS(encrypted_x, "secret-x.rds")
    
    encrypted_y <- readRDS("secret-x.rds")
    
    y <- unserialize(aes_cbc_decrypt(encrypted_y, key = key))
    

    You need to deal with secrets management (i.e. the key) but this general idiom should work (with a tad more bulletproofing).