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] f9 64 a6 09 d4 23 d4 c0 83 50 a3 34 5e 3e ca dc 37 ff a6 ac 71 36 4e d5 37
#> [26] 1f 81 cb 22 96 7f c6 15 a5 ac df 8b 4b 4b 6c 91 98 84 c9 93 4b 9a e6 7a 7a
#> [51] 24 19 a9 a9 10 d1 0e fc ff a0 14 96 e5 90 25 9e c2 f6 e1 a3 6b 84 31 90 73
#> [76] 23 7d 8c 5d 32 5d 49 53 2d 76 09 2f 52 16 9a bc c7 c5 3d 6a f8 e6 ac 8f 44
#> [101] 1e 60 68 ed 57 b5 45 93 49 bc 6d ec d0 91 dc a3 bc f1 12 86 ae 8d b5 8e 09
#> [126] 05 a2 ce 83 e8 05 0a 16 04 49 04 a4 21 7e f4 ae 14 64 b0 4c 2b 49 c9 18 c6
#> [151] 13
# 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] b2 91 68 19 c3 2e c9 d5 cf d1 19 f2 6e 56 65 95 75 1d d2 1a 83 1e 39 8c bc
#> [26] 69 08 7b eb cf c4 ea e8 b5 3e b0 9a 18 3b df 4e 56 a7 ed 4f ec de dd da 08
#> [51] c5 f1 f0 6e
decrypt_string(str_enc, key)
#> [1] "secret message"
