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] 41 51 a0 50 67 90 17 ae 20 e4 39 25 b3 8c 66 5a 2b 9c bf 21 65 1f df 5e be
#> [26] 19 b8 9c bb e3 f3 9d d9 d3 84 f0 b4 a8 6c 8e b0 56 48 dd 9d 68 a8 f6 c9 bf
#> [51] 73 90 99 6c 93 74 0e 77 86 b5 1c b4 1a 75 7a 7a 24 40 f5 f0 a6 8e 4b 55 32
#> [76] b5 05 83 6b dc 26 c6 ff 1d 1f fc 56 43 2e 29 8e 9b c5 b7 8e a4 89 4f ed ca
#> [101] 5e e7 15 be 43 bf d9 36 08 12 91 89 6d 95 f2 c2 70 7f ad 5a 97 be 46 4a d0
#> [126] b0 e8 f5 86 1f 50 4f 6f c7 99 22 ce 61 ff 44 53 11 f6 a7 11 9f 78 93 95 f1
#> [151] 05
# 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] 51 58 11 2e 89 12 0e d4 65 f7 b8 f6 ef da f3 5e d3 4f 2c 18 99 5d 99 5e 2a
#> [26] 14 10 7c 50 8a 41 f3 4d 29 1c 5b b5 ca 0f 91 1b 8e 63 bf 4b 4e d1 38 ee c2
#> [51] 05 2d ed f8
decrypt_string(str_enc, key)
#> [1] "secret message"
