Introduction

2019-03-22

This package tries to smooth over some of the differences in encryption approaches (symmetric vs. asymmetric, sodium vs. openssl) to provide a simple interface for users who just want to encrypt or decrypt things.

The scope of the package is to protect data that has been saved to disk. It is not designed to stop an attacker targeting the R process itself to determine the contents of sensitive data. The package does try to prevent you accidentally saving to disk the contents of sensitive information, including the keys that could decrypt such information.

This vignette works through the basic functionality of the package. It does not offer much in the way of an introduction to encryption itself; for that see the excellent vignettes in the openssl and sodium packages (see vignette("crypto101") and vignette("bignum") for information about how encryption works). This package is a wrapper around those packages in order to make them more accessible.

Keys and the like

To encrypt anything we need a key. There are two sorts of key “types” we will concern ourselves with here “symmetric” and “asymmetric”.

• “symmetric” keys are used for storing secrets that multiple people need to access. Everyone has the same key (which is just a bunch of bytes) and with that we can either encrypt data or decrypt it.

• a “key pair” is a public and a private key; this is used in communication. You hold a private key that nobody else ever sees and a public key that you can copy around all over the show. These can be used for a couple of different patterns of communication (see below).

We support symmetric keys and asymmetric key pairs from the openssl and sodium packages (which wrap around industry-standard cryptographic libraries) - this vignette will show how to create and load keys of different types as they’re used.

The openssl keys have the advantage of a standard key format, and that many people (especially on Linux and macOS) have a keypair already (see below if you’re not sure if you do). The sodium keys have the advantage of being a new library, starting from a clean slate rather than carrying with it accumulated ideas from the last 20 years of development.

The idea in cyphr is that we can abstract away some differences in the types of keys and the functions that go with them to create a standardised interface to encrypting and decrypting strings, R objects, files and raw vectors. With that, we can then create wrappers around functions that create files and simplify the process of adding encryption into a data workflow.

Below, I’ll describe the sorts of keys that cyphr supports and in the sections following describe how these can be used to actually do some encryption.

Symmetric encryption

This is the simplest form of encryption because everyone has the same key (like a key to your house or a single password). This raises issues (like how do you store the key without other people reading it) but we can deal with that below.

openssl

To generate a key with openssl, you can use:

k <- openssl::aes_keygen()

which generates a raw vector

k
## aes raw 45:31:02:6f:2b:50:2a:3b:57:6e:f6:1a:a1:da:cb:cf

(this prints nicely but it really is stored as a 16 byte raw vector).

The encryption functions that this key supports are openssl::aes_cbc_encrypt, openssl::aes_ctr_encrypt and openssl::aes_gcm_encrypt (along with the corresponding decryption functions). The cyphr package tries to abstract this away by using a wrapper cyphr::key_openssl

key <- cyphr::key_openssl(k)
key
## <cyphr_key: openssl>

With this key, one can encrypt a string with cyphr::encrypt_string:

secret <- cyphr::encrypt_string("my secret string", key)

and decrypt it again with cyphr::decrypt_string:

cyphr::decrypt_string(secret, key)
## [1] "my secret string"

See below for more functions that use these key objects.

sodium

The interface is almost identical using sodium symmetric keys. To generate a symmetric key with libsodium you would use sodium::keygen

k <- sodium::keygen()

This is really just a raw vector of length 32, without even any class attribute!

The encryption functions that this key supports are sodium::data_encrypt and sodium::data_decrypt. To create a key for use with cyphr that knows this, use:

key <- cyphr::key_sodium(k)
key
## <cyphr_key: sodium>

This key can then be used with the high-level cyphr encryption functions described below.

Asymmetric encryption (“key pairs”)

With asymmetric encryption everybody has two keys that differ from everyone else’s key. One key is public and can be shared freely with anyone you would like to communicate with and the other is private and must never be disclosed.

In the sodium package there is a vignette (vignette("crypto101")) that gives a gentle introduction to how this all works. In practice, you end up creating a pair of keys for yourself. Then to encrypt or decrypt something you encrypt messages with the recipient’s public key and they (and only they) can decrypt it with their private key.

One use for asymmetric encryption is to encrypt a shared secret (such as a symmetric key) - with this you can then safely store or communicate a symmetric key without disclosing it.

openssl

Let’s suppose that we have two parties “Alice” and “Bob” who want to talk with one another. For demonstration purposes we need to generate SSH keys (with no password) in temporary directories (to comply with CRAN policies). In a real situation these would be on different machines (Alice has no access to Bob’s key!) and these keys would be password protected.

path_key_alice <- cyphr::ssh_keygen(password = FALSE)
path_key_bob <- cyphr::ssh_keygen(password = FALSE)

Note that each directory contains a public key (id_rsa.pub) and a private key (id_rsa).

dir(path_key_alice)
## [1] "id_rsa"     "id_rsa.pub"
dir(path_key_bob)
## [1] "id_rsa"     "id_rsa.pub"

Below, the full path to the key (e.g., .../id_rsa) could be used in place of the directory name if you prefer.

If Alice wants to send a message to Bob she needs to use her private key and his public key

pair_a <- cyphr::keypair_openssl(path_key_bob, path_key_alice)
pair_a
## <cyphr_keypair: openssl>

with this pair she can write a message to “bob”:

secret <- cyphr::encrypt_string("secret message", pair_a)

The secret is now just a big pile of bytes

secret
##   [1] 58 0a 00 00 00 02 00 03 05 02 00 02 03 00 00 00 02 13 00 00 00 04 00
##  [24] 00 00 18 00 00 00 10 63 6b d1 89 27 49 74 84 ed 7b 19 ce 74 4a 43 c2
##  [47] 00 00 00 18 00 00 01 00 7c 1b f9 17 f8 2c 08 75 9a bf e0 8e c4 9a 0b
##  [70] 37 9d f1 c1 9f c8 98 a8 21 b0 2c c6 23 5e 64 4b 8d df b5 59 40 ba c2
##  [93] 75 f9 8a 35 2b a9 af d4 5d f1 c9 73 2f 13 c1 34 a4 7f af 38 e9 b8 bc
## [116] 90 83 a1 cb 7f 29 32 c4 92 0c 0d 37 2c 2d a3 a9 0f fd 54 80 b2 71 eb
## [139] 8c 0d c5 8a d5 d3 96 42 41 d4 c6 d1 27 91 10 96 70 e9 48 25 04 97 78
## [162] 4b bb f2 a9 e2 5e 9d a5 ef 2e d4 f6 d1 2e 2b e3 98 f8 4f 3b 4b ac e5
## [185] 25 02 45 43 91 92 19 fc 29 51 4f a1 66 64 65 cd bc 8e dc d6 3d f2 d3
## [208] 5e d1 34 8f 25 8f b1 5f 0c b7 0b 6c 41 74 71 71 9c e8 b0 45 42 d0 d8
## [231] de ae 47 6d 35 ea ac a5 82 69 dc 38 1a e1 0b 68 04 71 94 94 e6 cd 8e
## [254] 84 df 35 0f 0d 64 ef 10 b3 cb 25 f7 b1 fc 5b cb f0 0b e2 76 27 e1 b7
## [277] a3 25 0d 28 7d 98 56 fe 19 e2 f9 e9 19 b4 24 78 02 fe 29 4a 04 d6 79
## [300] 7d ab 79 d1 56 1f be e5 e3 2a 38 00 00 00 18 00 00 00 10 5b 53 fa c5
## [323] 7c 23 8b 13 d8 b3 98 f4 59 20 86 e3 00 00 00 18 00 00 01 00 68 ba 10
## [346] 21 9c e9 67 05 dc b9 5e bc b2 87 0c 3a 1b 59 2d 5c a9 06 56 ef 68 d6
## [369] 97 94 65 ca 1a 39 bf 1c b8 fc 53 e7 de 2a 11 2e b5 04 a5 8e 24 45 a3
## [392] 32 02 46 b7 10 0a 2b 4f 44 f1 2b 0b 55 77 d8 ac b7 a1 71 15 51 e7 cc
## [415] 1f b9 ae 07 43 a4 78 2c 9b 10 a5 97 0d fe 02 9e 02 25 6f 4e 63 53 85
## [438] b6 8f 1e 91 60 be ad d6 fd 7b 40 75 aa f6 fd fb 0e 9f 89 13 96 3d c9
## [461] e4 c6 3c 35 e7 ad 27 b6 6b 82 55 b5 11 ad 98 5c d1 bc 27 04 5a 99 cb
## [484] 35 94 51 58 4b 51 6c fe 4f 2c 1f 19 8a 7a 66 8c 0f 92 3e b9 f3 59 09
## [507] fc a5 7d 49 bf de a5 83 7e a3 21 b0 e7 1a 5f cf b8 e0 a9 7c 68 7b 1d
## [530] 45 70 77 34 c6 1f d5 d3 00 60 a8 86 6a 52 4d a0 14 94 3f 00 13 08 ea
## [553] 32 6c 06 2c f2 a6 af b3 93 04 ed 4b 17 87 b8 70 8d f7 e4 41 37 78 2f
## [576] 9a 74 6b d9 5d 70 f2 6e 50 cd 6d 7b 98 27 1a 5e 30 70 c9 d4 ac d8 a4
## [599] 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 05 6e 61 6d 65 73 00 00
## [622] 00 10 00 00 00 04 00 04 00 09 00 00 00 02 69 76 00 04 00 09 00 00 00
## [645] 07 73 65 73 73 69 6f 6e 00 04 00 09 00 00 00 04 64 61 74 61 00 04 00
## [668] 09 00 00 00 09 73 69 67 6e 61 74 75 72 65 00 00 00 fe

Note that unlike symmetric encryption above, Alice cannot decrypt her own message:

cyphr::decrypt_string(secret, pair_a)
## Error: OpenSSL error in RSA_padding_check_PKCS1_type_2: pkcs decoding error

For Bob to read the message, he uses his private key and Alice’s public key (which she has transmitted to him previously).

pair_b <- cyphr::keypair_openssl(path_key_alice, path_key_bob)

With this keypair, Bob can decrypt Alice’s message

cyphr::decrypt_string(secret, pair_b)
## [1] "secret message"

And send one back of his own:

secret2 <- cyphr::encrypt_string("another message", pair_b)
secret2
##   [1] 58 0a 00 00 00 02 00 03 05 02 00 02 03 00 00 00 02 13 00 00 00 04 00
##  [24] 00 00 18 00 00 00 10 9d bb cf c7 59 6a 7a 44 d6 c1 1d db 07 1f c1 d5
##  [47] 00 00 00 18 00 00 01 00 1a 44 92 13 4f 25 6d 89 a0 60 2b 16 d9 f5 a8
##  [70] 1d a4 d3 f8 90 94 93 fd b8 b2 59 f9 e1 80 86 61 77 73 0c b7 43 e0 61
##  [93] de 10 9f ad ac f5 ee 8e 4c 61 67 e0 b4 3e 91 51 e0 6b d2 32 ff a9 33
## [116] f5 52 2c 32 9d 75 34 56 bf e5 2b d2 df d3 09 d2 05 bd 70 75 28 14 00
## [139] 9f 27 3d 6a bf 26 88 d4 bc a3 61 2f 24 b6 0d ba da 71 e7 f2 af f9 c9
## [162] dd 07 79 8f 63 3e 5c f1 f2 62 4e ce 25 c6 24 b3 77 d4 1d 62 92 db 5b
## [185] 7e b0 75 1a 5e 2f 5c 2a 33 46 64 f4 79 f2 36 2e 9e 8d 76 c9 9c bf 63
## [208] 94 3e 05 3e 1f 90 77 7c 7d ab c5 28 16 d9 f9 3b 22 3a 7a 48 f8 21 0d
## [231] 52 2d 18 47 ff 76 1d 67 56 e9 e5 f2 66 94 91 b7 18 89 56 ce 35 41 84
## [254] 96 b5 21 ad 83 59 a4 f3 16 25 d6 17 6b ed 72 6c 23 af 51 97 3a 70 e1
## [277] ee c5 82 41 ca 36 7c f2 5f a7 0a 27 d2 2e 99 e6 8e 03 e3 f4 84 27 64
## [300] 1b 30 87 a3 4e 5d 27 38 01 f5 1f 00 00 00 18 00 00 00 10 56 5e fc 6a
## [323] 14 3b 63 3e 04 c2 62 c5 b1 9e 4c 9a 00 00 00 18 00 00 01 00 9b 1b 11
## [346] 23 fb 62 67 68 2f 26 7b e1 45 90 63 a4 77 d8 6f 87 16 8b c1 11 b3 77
## [369] c6 db d0 3b 85 21 19 b5 18 e4 56 5a 62 82 7a 09 15 d5 17 78 b1 a9 34
## [392] 33 1d e7 b1 23 95 b0 62 f6 62 64 6b df 85 ea 75 dd de 9d 60 db e6 d4
## [415] c6 fb b3 1f 76 60 44 d8 4a cb 28 47 35 7c 01 fb 69 88 b6 9a 6d f7 f5
## [438] ce c7 40 9c 6b 4a e5 3f 2a 0c 12 76 f3 e8 52 81 9f 9d a7 fb e9 2c 7d
## [461] 1a b4 50 5e 29 3e d2 9f 9b 9a ec 47 64 28 95 6e be c5 f6 d7 d4 04 02
## [484] 96 b5 df 4e 48 da 6c 77 85 ea f7 f1 c9 10 75 36 23 91 5b bb 7f 31 49
## [507] b0 24 c8 b7 f3 9a a6 fb 01 73 fc 08 08 ca bc 24 fd ea bc 9b a2 a2 ce
## [530] a3 d4 3c 4d f4 d3 58 d0 a2 16 96 fc e7 2b 19 7f fd c0 41 40 93 0d 12
## [553] 05 3a 49 4b 5b d9 fd b2 11 00 08 a1 f5 f1 58 c2 a3 7a bf f0 7c f5 ae
## [576] cb c7 13 48 75 5f af d1 01 03 4e 05 79 92 36 12 27 6c 0d bd 1a 62 8d
## [599] 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 05 6e 61 6d 65 73 00 00
## [622] 00 10 00 00 00 04 00 04 00 09 00 00 00 02 69 76 00 04 00 09 00 00 00
## [645] 07 73 65 73 73 69 6f 6e 00 04 00 09 00 00 00 04 64 61 74 61 00 04 00
## [668] 09 00 00 00 09 73 69 67 6e 61 74 75 72 65 00 00 00 fe

which she can decrypt

cyphr::decrypt_string(secret2, pair_a)
## [1] "another message"

Chances are, you have an openssl keypair in your .ssh/ directory. If so, you would pass NULL as the path for the private (or less usefully, the public) key pair part. So to send a message to Bob, we’d include the path to Bob’s public key.

pair_us <- cyphr::keypair_openssl(path_key_bob, NULL)

This all skips over how Alice and Bob will exchange this secret information. Because the secret is bytes, it’s a bit odd to work with. Alice could save the secret to disk with

secret <- cyphr::encrypt_string("secret message", pair_a)
path_for_bob <- file.path(tempdir(), "for_bob_only")
writeBin(secret, path_for_bob)

And then send Bob the file for_bob_only (over email or any other insecure medium).

and bob could read the secret in with:

secret <- readBin(path_for_bob, raw(), file.size(path_for_bob))
cyphr::decrypt_string(secret, pair_b)
## [1] "secret message"

As an alternative, you can “base64 encode” the bytes into something that you can just email around:

secret_base64 <- openssl::base64_encode(secret)
secret_base64
## [1] "WAoAAAACAAMFAgACAwAAAAITAAAABAAAABgAAAAQrno7Y3Q+lwaQxh78eQxsAQAAABgAAAEAinJlFmCOb74GmHzymm1XHfjIlKwAHmwx7UDHDqoy5b/Ad9Vuy4hPqwJM3bjH5+mPMTr+bzz/mi1CH7zQ2fXpW3VNPE091ZLTQWW7JO+s2qqn/H/TyHXQrdsO0rMJTnNg62UFeMn17VZskDedOyahS3HNDs/qNUGF3IKJ7Gf68Rhpb96r30uWCYwXI01apFBx5BSFroM6UJUTQiCMg6LkqxMM/28TI0AkwCzw2+V/2zxEBxAY6+gTAE0PoTEzFw0zjPHz2wgfnU3q8/rX6Fwqt+AoBtZnbZjywIbgaosJqipK83awBkfhae+w08O/x9YXSRr+6EuXorSz1ybsY3Ph1QAAABgAAAAQT+3sDUAtQ5Gp/4pOE4rTDQAAABgAAAEAaLoQIZzpZwXcuV68socMOhtZLVypBlbvaNaXlGXKGjm/HLj8U+feKhEutQSljiRFozICRrcQCitPRPErC1V32Ky3oXEVUefMH7muB0OkeCybEKWXDf4CngIlb05jU4W2jx6RYL6t1v17QHWq9v37Dp+JE5Y9yeTGPDXnrSe2a4JVtRGtmFzRvCcEWpnLNZRRWEtRbP5PLB8ZinpmjA+SPrnzWQn8pX1Jv96lg36jIbDnGl/PuOCpfGh7HUVwdzTGH9XTAGCohmpSTaAUlD8AEwjqMmwGLPKmr7OTBO1LF4e4cI335EE3eC+adGvZXXDyblDNbXuYJxpeMHDJ1KzYpAAABAIAAAABAAQACQAAAAVuYW1lcwAAABAAAAAEAAQACQAAAAJpdgAEAAkAAAAHc2Vzc2lvbgAEAAkAAAAEZGF0YQAEAAkAAAAJc2lnbmF0dXJlAAAA/g=="

This can be converted back with openssl::base64_decode:

identical(openssl::base64_decode(secret_base64), secret)
## [1] TRUE

Or, less compactly but also suitable for email, you might just convert the bytes into their hex representation:

secret_hex <- sodium::bin2hex(secret)
secret_hex
## [1] "580a00000002000305020002030000000213000000040000001800000010ae7a3b63743e970690c61efc790c6c0100000018000001008a726516608e6fbe06987cf29a6d571df8c894ac001e6c31ed40c70eaa32e5bfc077d56ecb884fab024cddb8c7e7e98f313afe6f3cff9a2d421fbcd0d9f5e95b754d3c4d3dd592d34165bb24efacdaaaa7fc7fd3c875d0addb0ed2b3094e7360eb650578c9f5ed566c90379d3b26a14b71cd0ecfea354185dc8289ec67faf118696fdeabdf4b96098c17234d5aa45071e41485ae833a50951342208c83a2e4ab130cff6f13234024c02cf0dbe57fdb3c44071018ebe813004d0fa13133170d338cf1f3db081f9d4deaf3fad7e85c2ab7e02806d6676d98f2c086e06a8b09aa2a4af376b00647e169efb0d3c3bfc7d617491afee84b97a2b4b3d726ec6373e1d500000018000000104fedec0d402d4391a9ff8a4e138ad30d000000180000010068ba10219ce96705dcb95ebcb2870c3a1b592d5ca90656ef68d6979465ca1a39bf1cb8fc53e7de2a112eb504a58e2445a3320246b7100a2b4f44f12b0b5577d8acb7a1711551e7cc1fb9ae0743a4782c9b10a5970dfe029e02256f4e635385b68f1e9160beadd6fd7b4075aaf6fdfb0e9f8913963dc9e4c63c35e7ad27b66b8255b511ad985cd1bc27045a99cb359451584b516cfe4f2c1f198a7a668c0f923eb9f35909fca57d49bfdea5837ea321b0e71a5fcfb8e0a97c687b1d45707734c61fd5d30060a8866a524da014943f001308ea326c062cf2a6afb39304ed4b1787b8708df7e44137782f9a746bd95d70f26e50cd6d7b98271a5e3070c9d4acd8a4000004020000000100040009000000056e616d6573000000100000000400040009000000026976000400090000000773657373696f6e00040009000000046461746100040009000000097369676e6174757265000000fe"

and the reverse with sodium::hex2bin:

identical(sodium::hex2bin(secret_hex), secret)
## [1] TRUE

(this is somewhat less space efficient than base64 encoding.

As a final option, you can just save the secret with saveRDS and read it in with readRDS like any other option. This will be the best route if the secret is saved into a more complicated R object (e.g., a list or data.frame).

See the other cyphr vignette (vignette("data", package = "cyphr")) for a suggested workflow for exchanging secrets within a team, and the wrapper functions below for more convenient ways of working with encrypted data.

Do you already have an ssh keypair? To find out, run

cyphr::keypair_openssl(NULL, NULL)

One of three things will happen:

1. you will be prompted for your password to decrypt your private key, and then after entering it an object <cyphr_keypair: openssl> will be returned - you’re good to go!

2. you were not prompted for your password, but got a <cyphr_keypair: openssl> object. You should consider whether this is appropriate and consider generating a new keypair with the private key encrypted. If you don’t then anyone who can read your private key can decrypt any message intended for you.

3. you get an error like Did not find default ssh public key at ~/.ssh/id_rsa.pub. You need to create a keypair.

To create a keypair, you can use the cyphr::ssh_keygen() function as

cyphr::ssh_keygen("~/.ssh")

This will create the keypair as ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub, which is where cyphr will look for your keys by default. See ?ssh_keygen for more information. (On Linux and macOS you might use the ssh-keygen command line utility. On windows, PuTTY has a utility for creating keys.)

sodium

With sodium, things are largely the same with the exception that there is no standard format for saving sodium keys. The bits below use an in-memory key (which is just a collection of bytes) but these can also be filenames, each of which contains the contents of the key written out with writeBin.

First, generate keys for Alice:

key_a <- sodium::keygen()
pub_a <- sodium::pubkey(key_a)

the public key is derived from the private key, and Alice can share that with Bob. We next generate Bob’s keys

key_b <- sodium::keygen()
pub_b <- sodium::pubkey(key_b)

Bob would now share is public key with Alice.

If Alice wants to send a message to Bob she again uses her private key and Bob’s public key:

pair_a <- cyphr::keypair_sodium(pub_b, key_a)

As above, she can now send a message:

secret <- cyphr::encrypt_string("secret message", pair_a)
secret
##  [1] f3 ac 15 fa f7 d1 cc 3a 39 35 88 a0 ff 5a be 2a c2 38 08 22 f8 7e b1
## [24] cf 32 9e 71 68 38 f9 7c 85 93 bc 5e 4e 84 8e 7c 9e 2c 41 1e 42 18 2b
## [47] 7b 37 cf 85 8d 84 b0 cb

Note how this line is identical to the one in the openssl section.

To decrypt this message, Bob would use Alice’s public key and his private key:

pair_b <- cyphr::keypair_sodium(pub_a, key_b)
cyphr::decrypt_string(secret, pair_b)
## [1] "secret message"

Encrypting things

Above, we used cyphr::encrypt_string and cyphr::decrypt_string to encrypt and decrypt a string. There are several such functions in the package that encrypt and decrypt

• R objects encrypt_object / decrypt_object (using serialization and deserialization)
• strings: encrypt_string / decrypt_string
• raw vectors: encrypt_data / decrypt_data
• files: encrypt_file / decrypt_file

For this section we will just use a sodium symmetric encryption key

key <- cyphr::key_sodium(sodium::keygen())

For the examples below, in the case of asymmetric encryption (using either cyphr::keypair_openssl or cyphr::keypair_sodium) the sender would use their private key and the recipient’s public key and the recipient would use the complementary key pair.

Objects

Here’s an object to encrypt:

obj <- list(x = 1:10, y = "secret")

This creates a bunch of raw bytes corresponding to the data (it’s not really possible to print this as anything nicer than bytes).

secret <- cyphr::encrypt_object(obj, key)
secret
##   [1] 59 57 c0 17 ec 9f de e9 01 43 6f 06 6d 7d 76 66 3a a7 d6 1e a8 87 bc
##  [24] 6e d3 78 b3 53 f1 af f3 38 c3 27 62 50 8c d8 f9 35 34 60 65 ef ac c4
##  [47] 9d a5 ab bc a2 87 7e 8a bf 8a c8 29 62 43 a2 71 3e 7c 84 2f fd fe 47
##  [70] cf 67 8a c5 b3 fc c2 db 5f 05 83 e9 fc 9e 07 41 95 68 bf 0d c9 c1 4a
##  [93] e2 69 6f f0 68 cf 48 59 11 57 45 12 22 7b cf 1f 61 14 41 ac be 56 33
## [116] a4 49 0d ef 47 b3 7b 78 ce d2 f9 1f ae f8 f7 68 13 1c c2 dc 2e f7 16
## [139] 1a e7 64 a1 ef 10 6d cc 0d 0d 09 72 b6 01 f0 ae 6d 82 75 af da 85 60
## [162] 3a fe 8d 2f 1c 0a de 0e ba cf d0 b4 ca b7 41 17 23 3e 43 31 44 0d

The data can be decrypted with the decrypt_object function:

cyphr::decrypt_object(secret, key)
## $x ## [1] 1 2 3 4 5 6 7 8 9 10 ## ##$y
## [1] "secret"

Optionally, this process can go via a file, using a third argument to the functions (note that temporary files are used here for compliance with CRAN policies - any path may be used in practice).

path_secret <- file.path(tempdir(), "secret.rds")
cyphr::encrypt_object(obj, key, path_secret)

There is now a file called secret.rds in the temporary directory:

file.exists(path_secret)
## [1] TRUE

though it is not actually an rds file:

readRDS(path_secret)
## Error in readRDS(path_secret): unknown input format

When passed a filename (as opposed to a raw vector), cyphr::decrypt_object will read the object in before decrypting it

cyphr::decrypt_object(path_secret, key)
## $x ## [1] 1 2 3 4 5 6 7 8 9 10 ## ##$y
## [1] "secret"

Strings

For the case of strings we can do this in a slightly more lightweight way (the above function routes through serialize / deserialize which can be slow and will create larger objects than using charToRaw / rawToChar)

secret <- cyphr::encrypt_string("secret", key)
secret
##  [1] 6e 1b 54 23 f0 df fd 2c 64 7d f1 25 0b 74 09 7e 23 89 af 30 26 99 88
## [24] 75 a9 5e 85 70 39 ec 4d 14 13 99 82 30 f6 cd d1 04 64 e1 da d5 43 70

and decrypt:

cyphr::decrypt_string(secret, key)
## [1] "secret"

Plain raw data

If these are not enough for you, you can work directly with raw objects (bunches of bytes) by using encrypt_data:

dat <- sodium::random(100)
dat # some random bytes
##   [1] 69 29 8f 8b f3 0f c2 eb 3f 3f 3a 20 2b 91 6e ad 2c 81 e7 f8 7a 70 e8
##  [24] db 9a 78 82 56 db cd e9 03 ef 29 14 1c cf 64 0d 65 7c aa a2 b4 e3 e4
##  [47] 6b a3 8c 1f 3a 50 68 cc 1e e3 9b 3a c8 0b 63 c1 75 b7 dd 87 e5 2b b2
##  [70] 5f fa 10 4e fe ad e4 78 b5 a3 5b bd 34 80 5e 80 ee 9e de 23 bf f2 97
##  [93] 0e 10 2c 15 f8 5e 51 0f
secret <- cyphr::encrypt_data(dat, key)
secret
##   [1] 0a 7e 0a 9c 45 b8 4c f7 86 c3 06 5c e4 f6 8a c4 6b cb fb 69 4e c7 3f
##  [24] f0 2a 3d 39 97 a8 43 e2 85 7e 74 4c 60 86 a2 1b 94 b4 74 66 c2 9f 8c
##  [47] 63 37 46 77 f6 0f 94 24 0c d5 eb 93 2f c2 e5 8b 9d e0 74 b1 bc f8 ac
##  [70] ab 9c f5 13 30 9c 05 6f eb 47 8a ac 65 4f 71 0f 20 ab ff fe 42 6c bd
##  [93] 17 fb ac b4 9a 1e 28 10 92 7e bb 97 c2 59 d4 45 06 82 14 53 c8 6f e9
## [116] d0 3c 73 5e 5d 2f e0 98 98 2c d2 d1 91 d9 22 3a 81 48 bc a0 44 7f b8
## [139] 1e ac

Decrypted data is the same as a the original data

identical(cyphr::decrypt_data(secret, key), dat)
## [1] TRUE

Files

Suppose we have written a file that we want to encrypt to send to someone (in a temporary directory for compliance with CRAN policies)

path_data_csv <- file.path(tempdir(), "iris.csv")
write.csv(iris, path_data_csv, row.names = FALSE)

You can encrypt that file with

path_data_enc <- file.path(tempdir(), "iris.csv.enc")
cyphr::encrypt_file(path_data_csv, key, path_data_enc)

This encrypted file can then be decrypted with

path_data_decrypted <- file.path(tempdir(), "idis2.csv")
cyphr::decrypt_file(path_data_enc, key, path_data_decrypted)

Which is identical to the original:

tools::md5sum(c(path_data_csv, path_data_decrypted))
##  /var/folders/z7/c2kx_kt96zn2tt_6179bkc4m0000gp/T//Rtmp4OHgUv/iris.csv
##                                     "5fe92fe6a2c1928ef5a67b8939fdaf8d"
## /var/folders/z7/c2kx_kt96zn2tt_6179bkc4m0000gp/T//Rtmp4OHgUv/idis2.csv
##                                     "5fe92fe6a2c1928ef5a67b8939fdaf8d"

An even higher level interface for files

This is the most user-friendly way of using the package when the aim is to encrypt and decrypt files. The package provides a pair of functions cyphr::encrypt and cyphr::decrypt that wrap file writing and file reading functions. In general you would use encrypt when writing a file and decrypt when reading one. They’re designed to be used like so:

Suppose you have a super-secret object that you want to share privately

key <- cyphr::key_sodium(sodium::keygen())
x <- list(a = 1:10, b = "don't tell anyone else")

If you save x to disk with saveRDS it will be readable by everyone until it is deleted. But if you encrypted the file that saveRDS produced it would be protected and only people with the key can read it:

path_object <- file.path(tempdir(), "secret.rds")
cyphr::encrypt(saveRDS(x, path_object), key)

(see below for some more details on how this works).

This file cannot be read with readRDS:

readRDS(path_object)
## Error in readRDS(path_object): unknown input format

but if we wrap the call with decrypt and pass in the config object it can be decrypted and read:

cyphr::decrypt(readRDS(path_object), key)
## $a ## [1] 1 2 3 4 5 6 7 8 9 10 ## ##$b
## [1] "don't tell anyone else"

What happens in the call above is cyphr uses “non standard evaluation” to rewrite the call above so that it becomes (approximately)

1. use cyphr::decrypt_file to decrypt “secret.rds” as a temporary file
2. call readRDS on that temporary file
3. delete the temporary file (even if there is an error in the above calls)

This non-standard evaluation breaks referential integrity (so may not be suitable for programming). You can always do this manually with encrypt_file / decrypt_file so long as you make sure to clean up after yourself.

The encrypt function inspects the call in the first argument passed to it and works out for the function provided (saveRDS) which argument corresponds to the filename (here "secret.rds"). It then rewrites the call to write out to a temporary file (using tempfile()). Then it calls encrypt_file (see below) on this temporary file to create the file asked for ("secret.rds"). Then it deletes the temporary file, though this will also happen in case of an error in any of the above.

The decrypt function works similarly. It inspects the call and detects that the first argument represents the filename. It decrypts that file to create a temporary file, and then runs readRDS on that file. Again it will delete the temporary file on exit.

The functions supported via this interface are:

• readLines / writeLines
• readRDS / writeRDS
• read / save
• read.table / write.table
• read.csv / read.csv2 / write.csv
• read.delim / read.delim2

But new functions can be added with the rewrite_register function. For example, to support the excellent rio package, whose import and export functions take the filename file you could use:

cyphr::rewrite_register("rio", "import", "file")
cyphr::rewrite_register("rio", "export", "file")

now you can read and write tabular data into and out of a great many different file formats with encryption with calls like

cyphr::encrypt(rio::export(mtcars, "file.json"), key)
cyphr::decrypt(rio::import("file.json"), key)

The functions above use non standard evaluation and so may not be suitable for programming or use in packages. An “escape hatch” is provided via encrypt_ and decrypt_ where the first argument is a quoted expression.

cyphr::encrypt_(quote(saveRDS(x, path_object)), key)
cyphr::decrypt_(quote(readRDS(path_object)), key)
## $a ## [1] 1 2 3 4 5 6 7 8 9 10 ## ##$b
## [1] "don't tell anyone else"

Session keys

When using key_openssl, keypair_openssl, key_sodium, or keypair_sodium we generate something that can decrypt data. The objects that are returned by these functions can encrypt and decrypt data and so it is reasonable to be concerned that if these objects were themselves saved to disk your data would be compromised.

To avoid this, cyphr does not store private or symmetric keys directly in these objects but instead encrypts the sensitive keys with a cyphr-specific session key that is regenerated each time the package is loaded. This means that the objects are practically only useful within one session, and if saved with save.image (perhaps automatically at the end of a session) the keys cannot be used to decrypt data.

To manually invalidate all keys you can use the cyphr::session_key_refresh function. For example, here is a symmetric key:

key <- cyphr::key_sodium(sodium::keygen())

which we can use to encrypt a secret string

secret <- cyphr::encrypt_string("my secret", key)

and decrypt it:

cyphr::decrypt_string(secret, key)
## [1] "my secret"

If we refresh the session key we invalidate the key object

cyphr::session_key_refresh()

and after this point the key cannot be used any further

cyphr::decrypt_string(secret, key)
## Error in is.raw(key): Failed to decrypt key as session key has changed

This approach works because the package holds the session key within its environment (in cyphr:::session\$key) which R will not serialize. As noted above - this approach does not prevent an attacker with the ability to snoop on your R session from discovering your private keys or sensitive data but it does prevent accidentally saving keys in a way that would be useful for an attacker to use in a subsequent session.

• The vignettes in the openssl (vignette(package = "openssl")) and sodium (vignette(package = "openssl")) packages have explanations of how the tools used in cyphr work and interface with R.