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 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] 97 94 cf 45 5f e2 8d fb fb 38 95 84 54 20 91 5f 03 63 f7 df cf f9 3c c6 6e
#> [26] cc 76 d0 05 24 2b cf 62 6e f6 d3 5c 04 d2 07 9b 5d f4 ca 8f 30 7b e1 3e 00
#> [51] 6d 7b 36 2d 92 64 5e c4 8a 88 e5 ff 7f 5d bb 62 8d 67 08 19 17 09 1b 11 62
#> [76] 1e 69 23 45 af 0f 07 65 74 f8 ae f0 5e 49 6c 86 10 e0 58 8c a8 8e c8 65 1b
#> [101] 09 e9 71 07 ee 79 4a d5 50 dc a3 40 2c 22 34 8d e8 69 a5 93 e9 14 3c 8b 1f
#> [126] 18 03 56 e6 c0 0b 7f 80 49 5f 38 80 4d c1 49 7a 43 8d 13 76 13 b4 c4 af d7
#> [151] 7f
# 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] fd 9c ee 74 b4 4a 4f 2b 7a 68 7c 4d 00 f1 b2 b7 f1 17 d3 7a 31 6b bc 4a d2
#> [26] 4d 8e 46 a3 07 cf 59 fe 13 da 4d ee 31 80 e7 ac 09 61 77 1f 2b e6 69 5f 34
#> [51] 7b 88 15 6a
decrypt_string(str_enc, key)
#> [1] "secret message"