Skip to contents

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

Usage

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 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 03 00 04 05 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#>  [26] 00 0e 00 00 00 0a 3f e7 73 c4 ec c0 00 00 3f e8 b8 7f 08 40 00 00 3f eb fc
#>  [51] ba 86 60 00 00 3f c6 64 74 57 80 00 00 3f a1 88 14 78 00 00 00 3f d4 81 33
#>  [76] 27 00 00 00 3f d9 bf be f0 80 00 00 3f c9 0b b5 8a 80 00 00 3f d9 d3 91 8a
#> [101] 40 00 00 3f b0 4c 1e 05 00 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] b5 97 56 a3 11 a4 fc e1 d9 97 37 b9 b5 5d a2 37 22 2e 55 f0 52 01 a3 75 f1
#>  [26] bd 2e cb 14 25 06 61 c2 25 1f cc bc 59 84 23 b6 4a 6b 1c d5 f4 ea 25 40 2e
#>  [51] d7 f4 7e 21 8e 25 70 05 e2 57 d0 47 61 b4 b3 c4 ae 05 2c 96 2f 7d b8 9a b6
#>  [76] ed b3 cf 21 c1 f4 31 4b 0a 2c cf b1 51 79 9b 44 17 87 5e 67 2d de 84 d4 10
#> [101] f8 bd f7 37 77 9e 21 65 bd 72 bf 84 f1 0c c8 64 3b c7 11 6a fc d8 73 e5 02
#> [126] be f2 55 64 66 c2 38 10 1b e9 2d 36 a7 f8 e8 28 d5 39 d1 4f 0b a9 8b ff ab
#> [151] 01
# Our random numbers:
unserialize(decrypt_data(data_enc, key))
#>  [1] 0.73288199 0.77252151 0.87460066 0.17494063 0.03424133 0.32038573
#>  [7] 0.40232824 0.19566983 0.40353812 0.06366146
# Same as the never-encrypted version:
x
#>  [1] 0.73288199 0.77252151 0.87460066 0.17494063 0.03424133 0.32038573
#>  [7] 0.40232824 0.19566983 0.40353812 0.06366146

# 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] 54 a0 62 fb ea 4f e7 fb e8 11 55 68 01 42 2c c3 c8 bf 28 16 22 c6 24 a1 04
#> [26] 04 17 15 0f 82 fb 9d 32 1d f5 c2 eb 92 64 a1 b4 6c 52 f3 d0 a5 07 fe 15 27
#> [51] 58 d6 25 57
decrypt_string(str_enc, key)
#> [1] "secret message"