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_keyobject describing the encryption approach to use.- dest
The destination filename for the encrypted or decrypted data, or
NULLto return a raw vector. This is not used bydecrypt_objectordecrypt_stringwhich 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 06 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0e 00 00 00 0a 3f b4 ac 0a 80 00 00 00 3f ea b2 db 32 a0 00 00 3f e3 39
#> [51] 6e e4 e0 00 00 3f c4 1f 67 fd 80 00 00 3f 7e 4e e0 60 00 00 00 3f dd d9 64
#> [76] 1c 80 00 00 3f df db 95 b1 40 00 00 3f d2 8b 8b e9 c0 00 00 3f e7 73 c4 ec
#> [101] c0 00 00 3f e8 b8 7f 08 40 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] fe 09 22 b5 61 6e cb 30 69 1e c3 6c c4 5f cf 2d 23 86 54 ed 4b c8 05 8c 5c
#> [26] 50 04 70 a9 c6 05 87 bd 5e a2 cf 0f be e0 16 d3 d7 68 84 72 74 b1 7b 24 99
#> [51] 04 1b c2 3c dd f7 b9 27 59 99 97 72 e6 e3 00 fe b2 5b 1e e0 ac d6 4d 09 66
#> [76] cc e5 af 7e d1 77 30 51 70 69 60 ec f6 4c 43 8a 29 f7 75 2b cf 2a dc c6 b8
#> [101] d4 16 78 b0 c0 8b fa dd e4 c2 b0 b2 cc e9 6a 56 87 01 e7 fd 2f 73 1c ef 7f
#> [126] 1a 99 fb ac 30 60 3d 35 24 43 d0 39 e6 99 6e 01 70 bb 73 b9 b9 58 b6 d4 8d
#> [151] f1
# 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] 4b 8f be 77 49 d8 88 ce 0a 9c 2a 52 58 07 b1 75 d0 c9 f0 8c 6c b6 25 26 81
#> [26] 8a 7f c4 2e a1 60 46 71 a2 b6 cc 4b ae 12 65 ef fc 9e a9 3b c6 fb 6f 43 f6
#> [51] ae 4b 18 e0
decrypt_string(str_enc, key)
#> [1] "secret message"
