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 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] fe c6 5d e5 ad b2 a8 48 c7 a5 06 13 71 88 15 90 08 fa 37 a6 41 37 df 1b df
#> [26] 9b 5c b7 2f d8 61 6c e1 68 a4 4b 04 85 c4 bc 0d 8c f9 6c f5 bd 52 f6 70 99
#> [51] 10 68 ec 0e 71 67 fd 88 e0 d1 65 b9 3e 24 5c c6 62 df 0e 2a 26 da 23 42 a9
#> [76] 6e cb bb 3e d3 24 52 3f bb 3a d0 3f c2 4c 80 40 02 4b 55 14 b0 8e 73 4a 19
#> [101] d4 33 74 5a c2 5f 01 5c 65 8a 7f 01 b7 f3 a1 b2 0b c5 4f 3a ab e4 85 73 5b
#> [126] b7 cb 0d 17 0d a9 5d ed 1d 47 2e 11 c9 a8 8c df 89 95 10 f6 d9 74 03 7b 4b
#> [151] 21
# 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] 69 35 94 6e 85 13 a1 a1 1f 86 48 b8 4e ef b5 05 e1 13 e4 52 17 03 36 04 16
#> [26] a7 68 4c 9e a8 1f 3e 46 f7 4c 6e 85 4e 70 99 61 92 8e fd 26 5c 6c 29 6f b5
#> [51] 84 c4 17 a5
decrypt_string(str_enc, key)
#> [1] "secret message"
