How to Encrypt Data in R

Saving and decrypting encrypted files with encryptr R-package

You seem to be missing the data parameter in your encrypt/decrypt calls and you are opening the wrong file name. Try

data |> 
encrypt(colnames(data)) |>
saveRDS("EncryptedData.rds")

rm(data)

EncryptedData <- readRDS("EncryptedData.rds")
data_decrypted <- EncryptedData |> decrypt(colnames(EncryptedData))

Note that we pass the data into encrypt. If you just run encrypt(colnames(data)) without piping data into the function, you should get an error about "no applicable method ...an object of class character". I used the pipe operator |> but you could use regular function calls as well. Then, since you are writing to "EncryptedData.rds", make sure top open that file. The encrpyt() function changes your data. It does not have any effect on what the saved file name will be. If you aren't using encrypt_file, the file name will not change.

How do I read an encrypted file from disk with R

I have a feeling there's an easier way to do this, but the digest package, which does AES encryption, is the closest thing I came across to what you are asking for. This should get you started.

# write encrypted data frame to file
write.aes <- function(df,filename, key) {
require(digest)
zz <- textConnection("out","w")
write.csv(df,zz, row.names=F)
close(zz)
out <- paste(out,collapse="\n")
raw <- charToRaw(out)
raw <- c(raw,as.raw(rep(0,16-length(raw)%%16)))
aes <- AES(key,mode="ECB")
aes$encrypt(raw)
writeBin(aes$encrypt(raw),filename)
}
# read encypted data frame from file
read.aes <- function(filename,key) {
require(digest)
dat <- readBin(filename,"raw",n=1000)
aes <- AES(key,mode="ECB")
raw <- aes$decrypt(dat, raw=TRUE)
txt <- rawToChar(raw[raw>0])
read.csv(text=txt)
}
# sample data
set.seed(1) # for reproducible example
data <- data.frame(x=rnorm(10),y=rpois(10,1),
z=letters[1:10],w=sample(T:F,10,replace=T))

set.seed(123581321)
key <- as.raw(sample(1:32,32))
write.aes(data,"encrypted.dat",key)
result <- read.aes("encrypted.dat",key)
# did it work?
all.equal(data,result)
# [1] TRUE

This uses ECB mode AES encryption. Obviously you need to use the same key to encrypt and decrypt. write.aes(...) converts the data frame to a csv-formatted text string, converts that to raw (which is required for AES), pads the raw vector out to a multiple of 16 bytes (also required for AES), encrypts, and writes to a binary file. read.aes(...) basically reverses the process.

This is just an example, intended to be modified to suit your needs. For instance, this saves the data frame without row names, which might or might not be a problem.

Encrypt / decrypt object in R with library(gpg)

Support for decrypting raw data has been added to the gpg R package. See https://github.com/jeroen/gpg/issues/5

Encrypted data can be read directly into R working memory without need to store decrypted file on disk.

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

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).



Related Topics



Leave a reply



Submit