如何使用R从磁盘读取加密文件 [英] How do I read an encrypted file from disk with R

查看:160
本文介绍了如何使用R从磁盘读取加密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个敏感的数据集,永远不应该被保存在磁盘上未加密。可以R处理这个还是全盘加密我唯一的选择?

解决方案

我有一种感觉有一个更简单的方法来做到这一点,但是使用AES加密的摘要包是我遇到的最接近您要求的内容。这应该让你开始。

 #将加密数据帧写入文件
write.aes< - function(df ,文件名,键){
require(digest)
zz< - textConnection(out,w)
write.csv(df,zz,row.names = F)
close(zz)
out< - paste(out,collapse =\\\

raw< - charToRaw(out)
raw< - c raw,as.raw(rep(0,16-length(raw)%% 16)))
aes< - AES(key,mode =ECB)
aes $ encrypt
writeBin(aes $ encrypt(raw),filename)
}
#从文件读取读取数据帧
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)#可重现的例子
data< - data.frame(x = rnorm(10),y = rpois(10,1),
z =字母[1:10],W =样品(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)

all.equal(data,result)
#[1] TRUE

这使用ECB模式AES加密。显然你需要使用相同的密钥进行加密和解密。将数据帧转换为csv格式的文本字符串,将其转换为raw(AES是必需的),将原始矢量输出到16字节的倍数(AES也是必需的),对二进制文件进行加密和写入。 read.aes(...)基本上反转了这个过程。



这只是一个例子,修改以适应您的需要。例如,这将保存没有行名的数据框,这可能或可能不是问题。


I have a sensitive data set that should never be stored unencrypted on disk. Can R deal with this or is full disk encryption my only option?

解决方案

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.

这篇关于如何使用R从磁盘读取加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆