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] e8 33 b0 e1 44 71 6a 09 9c 21 92 2f 64 cb d7 bb 94 69 62 72 c1 ac d8 90 c8
#> [26] 7a cc 94 6a 78 38 f7 c8 1b 55 86 c4 de 72 8c 57 37 e0 bc bb dd 4f f1 15 74
#> [51] ff 5c 0a f0 28 c8 42 c4 53 93 96 27 df 82 07 14 2f 9f e5 59 d4 4c 02 ae 27
#> [76] 30 1f 86 e0 8f b0 07 c0 53 26 b6 ec 6e e7 bc d7 c9 84 b6 48 58 41 12 ff f5
#> [101] 26 8b 2d 28 92 00 56 0b 24 a7 58 cb 2d c9 1d 02 5a 3c cd 30 0e 45 8d ca b1
#> [126] d6 24 e9 f2 45 97 9e 80 d9 c7 ad ac f7 c3 8a ea ee 8c 4b d9 84 70 55 a0 54
#> [151] 05
# 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] 66 61 aa af 02 3d 37 bd c8 c2 2c 4c 48 85 1a ce 56 48 f0 cf 37 00 d5 84 9b
#> [26] 6e eb 9a 21 2e ea 9b 41 2e 28 c8 b5 d9 47 51 a4 91 89 34 ed 89 2d 26 08 cc
#> [51] 97 7e 40 7a
decrypt_string(str_enc, key)
#> [1] "secret message"
