1

因此,对于上下文,我正在尝试使用 gpg 包解密和 xlsx 文件。如果我解密 as_text = TRUE,我会收到一条错误消息,并在字符串中嵌入了 nul。

writexl::write_xlsx(x = data.frame(x = 1:2, y = 1:2),
                    path = "testtemp/test_1/test2.xlsx")

encrypted_xlsx <- gpg::gpg_encrypt(
  data = list.files("testtemp/test_1/",
                    full.names = TRUE,
                    pattern = "xlsx"
  ),
  receiver = "mypublickey"
)

writeLines(
  text = encrypted_xlsx,
  con = "testtemp/test_1/test2.xlsx.gpg"
)

gpg::gpg_import("myprivatekey")
decrypted_file <- gpg::gpg_decrypt(
  data = "testtemp/test_1/test2.xlsx.gpg",
  verify = FALSE,
  as_text = FALSE
)

xl <- readxl::read_xlsx(path = rawConnection(decrypted_file))

我在最后一行尝试的内容不起作用,但希望能传达我想要实现的目标。

platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 4
minor 0.2
year 2020
month 06
day 22
svn rev 78730
language R
version.string R version 4.0.2 (2020-06-22) 昵称再次起飞

4

1 回答 1

2

解决方案是将解密后的输出从 gpg:gpg_decrypt 写入文件。

filename = tempfile(fileext = '.xlsx') #create temp file with xlsx suffix
writeBin(decrypted_file, filename)     #write data to file

xl <- readxl::read_xlsx(path = filename)

print(xl)

# A tibble: 2 x 2
      x     y
  <dbl> <dbl>
1     1     1
2     2     2

感谢https://stackoverflow.com/users/1968/konrad-rudolph的回答。

于 2021-06-23T15:30:11.940 回答