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] bb a2 f1 46 5f 69 d0 3e 5b 94 03 60 65 5c 37 38 02 42 43 dd 6e b7 6a 4d b1
#> [26] b5 d4 60 89 1c eb 26 52 4f 4c a8 4e 7b bc 63 4c 46 2b 06 a2 15 ba b6 77 91
#> [51] f1 72 66 80 62 3d 47 79 47 c9 2c 39 a7 91 50 b8 f6 b3 25 de a1 a4 f9 e3 7c
#> [76] 7a 41 38 d0 53 22 82 6b 08 de dd f4 ea d4 87 9c 18 7a cb ef fc 38 63 eb b1
#> [101] 2a 1e 17 e0 7d 65 76 ff a4 a4 a0 e7 96 6b 8b d6 75 a2 57 b4 0c 36 42 13 34
#> [126] bb e2 ec 2b a8 0a cc e8 6d 05 06 c2 6f 3f bc a9 4a 84 1e b9 0c 44 ab 6e 72
#> [151] ed
# 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] 01 ea 7d 8f 93 d2 ee cd 1d a3 6f 15 20 82 c8 d2 98 43 ff 58 dd 6e 44 ce 6e
#> [26] 73 4b 30 f5 4a 3b 45 f9 48 76 17 5f 51 44 e7 cf 06 3e 77 56 41 74 8a c1 79
#> [51] ac c6 ac 79
decrypt_string(str_enc, key)
#> [1] "secret message"
