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 03 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0e 00 00 00 0a 3f e2 86 fa 61 20 00 00 3f d8 d0 e3 6b c0 00 00 3f e9 3b
#> [51] 5c 4d 00 00 00 3f e7 c8 b8 a1 a0 00 00 3f d9 ab f0 85 c0 00 00 3f c5 34 4e
#> [76] c4 80 00 00 3f e0 01 d0 3c 60 00 00 3f d9 89 01 38 40 00 00 3f c9 f3 5f b0
#> [101] 80 00 00 3f d8 e9 5d c8 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] 73 d8 82 24 d1 40 f1 f7 e2 17 53 e4 74 3b 8e 7c 63 5c b6 16 98 05 5f 5d 0e
#> [26] ea c7 0e a5 98 7f d0 15 b8 a7 bc 5b 0f 5a 0e c0 7a 4b 6d d3 05 38 7f b2 03
#> [51] 94 fd fb ca c9 df ee 87 12 92 f7 43 f0 8f 17 2a 41 20 8d 69 ca a5 1d 1a cf
#> [76] b1 44 c5 95 e2 28 af c1 e4 ca dd d5 1c a1 a0 41 23 46 2a e6 97 63 39 37 de
#> [101] d1 01 4d 62 e1 73 f2 b3 e3 89 02 62 bf 6d 24 33 56 88 a7 63 e1 40 84 86 fd
#> [126] 1d 2a 25 93 f2 18 12 e1 3b 8d d0 1e ff 2e 4a 11 f8 11 12 3a d1 28 02 cc e4
#> [151] 39
# Our random numbers:
unserialize(decrypt_data(data_enc, key))
#> [1] 0.5789768 0.3877495 0.7884962 0.7432521 0.4011194 0.1656588 0.5002214
#> [8] 0.3989871 0.2027397 0.3892436
# Same as the never-encrypted version:
x
#> [1] 0.5789768 0.3877495 0.7884962 0.7432521 0.4011194 0.1656588 0.5002214
#> [8] 0.3989871 0.2027397 0.3892436
# 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] 8d 95 49 46 3f 3f 01 ef a2 31 ab 3c db d4 89 c0 79 15 56 ea 3e fb 84 0e 86
#> [26] 9d 50 b0 dc dc 2f eb fe 0a db 13 ea 1d ef 1c 3d e7 bb ef ae de 3f e0 61 ba
#> [51] cd 7e 07 f3
decrypt_string(str_enc, key)
#> [1] "secret message"