Encrypt and decrypt raw data, objects, strings and files. The core functions here are encrypt_data and decrypt_data which take raw data and decrypt it, writing either to file or returning a raw vector. The other functions encrypt and decrypt arbitrary R objects (encrypt_object, decrypt_object), strings (encrypt_string, decrypt_string) and files (encrypt_file, decrypt_file).

encrypt_data(data, key, dest = NULL)

encrypt_object(object, key, dest = NULL, rds_version = NULL)

encrypt_string(string, key, dest = NULL)

encrypt_file(path, key, dest = NULL)

decrypt_data(data, key, dest = NULL)

decrypt_object(data, key)

decrypt_string(data, key)

decrypt_file(path, key, dest = NULL)

Arguments

data

(for encrypt_data, decrypt_data, decrypt_object, decrypt_string) a raw vector with the data to be encrypted or decrypted. For the decryption functions this must be data derived by encrypting something or you will get an error.

key

A cyphr_key or cyphr_key object describing the encryption approach to use.

dest

The destination filename for the encrypted or decrypted data, or NULL to return a raw vector. This is not used by decrypt_object or decrypt_string which always return an object or string.

object

(for encrypt_object) an arbitrary R object to encrypt. It will be serialised to raw first (see serialize).

rds_version

RDS serialisation version to use (see serialize. The default in R version 3.3 and below is version 2 - in the R 3.4 series version 3 was introduced and is becoming the default. Version 3 format serialisation is not understood by older versions so if you need to exchange data with older R versions, you will need to use rds_version = 2. The default argument here (NULL) will ensure the same serialisation is used as R would use by default.

string

(for encrypt_string) a scalar character vector to encrypt. It will be converted to raw first with charToRaw.

path

(for encrypt_file) the name of a file to encrypt. It will first be read into R as binary (see readBin).

Examples

key <- key_sodium(sodium::keygen()) # Some super secret data we want to encrypt: x <- runif(10) # Convert the data into a raw vector: data <- serialize(x, NULL) data
#> [1] 58 0a 00 00 00 02 00 03 05 02 00 02 03 00 00 00 00 0e 00 00 00 0a 3f b4 ac #> [26] 0a 80 00 00 00 3f ea b2 db 32 a0 00 00 3f e3 39 6e e4 e0 00 00 3f c4 1f 67 #> [51] fd 80 00 00 3f 7e 4e e0 60 00 00 00 3f dd d9 64 1c 80 00 00 3f df db 95 b1 #> [76] 40 00 00 3f d2 8b 8b e9 c0 00 00 3f e7 73 c4 ec c0 00 00 3f e8 b8 7f 08 40 #> [101] 00 00
# Encrypt the data; without the key above we will never be able to # decrypt this. data_enc <- encrypt_data(data, key) data_enc
#> [1] 40 43 00 d4 81 ed 9c 38 17 25 77 7d 87 88 7d 91 ca 23 df f0 05 59 ec 6b 4e #> [26] 6c e1 f6 58 3a 00 93 c9 50 f0 46 00 15 9a ec eb 61 6b 6e 18 59 8b a8 17 1b #> [51] 27 5b 81 27 1c 6a 78 63 58 78 be a3 9b 5f 89 50 43 c9 32 64 6b a4 91 37 c2 #> [76] 7e 9e b6 b4 4c f5 3b a0 6c ec fb d6 b3 bc 2e eb a5 59 08 63 61 e2 a6 e8 ca #> [101] 2c ba c8 22 ca 82 3c 19 25 d8 e0 0f 6c 02 11 2d 91 00 71 ff 08 f3 ba b5 09 #> [126] 90 d2 3d f1 dc b5 c1 99 e3 50 9d d3 31 2f a8 bb a3
# Our random numbers: unserialize(decrypt_data(data_enc, key))
#> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 0.466393497 #> [7] 0.497777389 0.289767245 0.732881987 0.772521511
# Same as the never-encrypted version: x
#> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 0.466393497 #> [7] 0.497777389 0.289767245 0.732881987 0.772521511
# This can be achieved more easily using `encrypt_object`: data_enc <- encrypt_object(x, key) identical(decrypt_object(data_enc, key), x)
#> [1] TRUE
# Encrypt strings easily: str_enc <- encrypt_string("secret message", key) str_enc
#> [1] 42 b8 c7 f8 99 bc 63 97 e4 d3 8d 3e 9f cc 6b ad 46 d2 c7 92 05 0c 1f 39 a7 #> [26] ed 97 ba b6 3a 72 50 86 88 34 35 11 86 c8 a8 4a 8b fe 42 22 57 b6 04 b9 9a #> [51] 56 a8 cb 1d
decrypt_string(str_enc, key)
#> [1] "secret message"