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 bydecrypt_object
ordecrypt_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 00 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] 9e 69 be 36 2b 07 79 33 31 b2 d6 d4 f1 b2 e1 80 cc db 66 32 94 c8 72 f9 d2
#> [26] af d9 b9 6a 37 94 02 5c 36 74 51 a3 be 13 09 b9 54 c2 4e b5 51 84 ef a6 ed
#> [51] b8 d4 fe dd f2 b9 0a 17 5d e3 5e a4 50 22 5e 20 59 9e 0a 5b ee 65 80 cb 2f
#> [76] 75 d0 f7 63 66 a5 66 1e 77 c9 e1 63 3a c1 7f 6d 2e 2e 89 70 12 69 27 23 97
#> [101] 4e 5a fb 9e 21 d5 22 9a 86 96 e1 f1 2f 94 33 5b ef f5 c0 5b 5b 60 18 52 c1
#> [126] a8 e7 04 79 cf 9c 46 f7 21 a7 f7 02 8d df 10 47 bf 2c 12 1f 67 e8 bb 6c ac
#> [151] 59
# 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] 24 e8 ff 81 bf b6 1d 36 cc 61 37 71 32 78 e3 b3 3c fa 21 07 eb 9c 6a 16 9c
#> [26] 2a 47 44 18 75 a0 43 d2 72 48 56 04 96 63 d0 cd b6 3f a5 dc c7 49 66 42 f2
#> [51] 37 d5 b0 a0
decrypt_string(str_enc, key)
#> [1] "secret message"