Wrap a pair of openssl keys. You should pass your private key and the public key of the person that you are communicating with.
Arguments
- pub
An openssl public key. Usually this will be the path to the key, in which case it may either the path to a public key or be the path to a directory containing a file
id_rsa.pub. IfNULL, then your public key will be used (found via the environment variableUSER_PUBKEY, then~/.ssh/id_rsa.pub). However, it is not that common to use your own public key - typically you want either the sender of a message you are going to decrypt, or the recipient of a message you want to send.- key
An openssl private key. Usually this will be the path to the key, in which case it may either the path to a private key or be the path to a directory containing a file. You may specify
NULLhere, in which case the environment variableUSER_KEYis checked and if that is not defined then~/.ssh/id_rsawill be used.- envelope
A logical indicating if "envelope" encryption functions should be used. If so, then we use
openssl::encrypt_envelope()andopenssl::decrypt_envelope(). IfFALSEthen we useopenssl::rsa_encrypt()andopenssl::rsa_decrypt(). See the openssl docs for further details. The main effect of this is that usingenvelope = TRUEwill allow you to encrypt much larger data thanenvelope = FALSE; this is because openssl asymmetric encryption can only encrypt data up to the size of the key itself.- password
A password for the private key. If
NULLthen you will be prompted interactively for your password, and if a string then that string will be used as the password (but be careful in scripts!)- authenticated
Logical, indicating if the result should be signed with your public key. If
TRUEthen your key will be verified on decryption. This provides tampering detection.
See also
keypair_sodium() for a similar function using
sodium keypairs
Examples
# Note this uses password = FALSE for use in examples only, but
# this should not be done for any data you actually care about.
# Note that the vignette contains much more information than this
# short example and should be referred to before using these
# functions.
# Generate two keypairs, one for Alice, and one for Bob
path_alice <- tempfile()
path_bob <- tempfile()
cyphr::ssh_keygen(path_alice, password = FALSE)
cyphr::ssh_keygen(path_bob, password = FALSE)
# Alice wants to send Bob a message so she creates a key pair with
# her private key and bob's public key (she does not have bob's
# private key).
pair_alice <- cyphr::keypair_openssl(pub = path_bob, key = path_alice)
# She can then encrypt a secret message:
secret <- cyphr::encrypt_string("hi bob", pair_alice)
secret
#> [1] 58 0a 00 00 00 03 00 04 05 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 02 13 00 00 00 04 00 00 00 18 00 00 00 10 ef e6 05 cb f1 6e 03 13 02 2c 4a
#> [51] 61 2c 30 f5 47 00 00 00 18 00 00 01 00 30 5c 8b 5f d9 d6 f1 59 0b e5 b8 20
#> [76] 2c 1d d7 67 35 73 fb 75 5a 8f b0 ab 56 ec c3 f8 58 7c 3d 6d e5 08 21 00 c8
#> [101] 9c e8 bc cc ac 0a 3a 44 c3 7b c4 ea 20 24 99 f2 0a 68 eb 08 f2 2d 7b 96 af
#> [126] ef ef 0e 94 f7 0c 61 17 e1 93 91 eb a1 9a e4 f0 48 3f e0 dc 70 22 50 d5 f6
#> [151] e0 17 64 bb c5 50 2c fd 12 a4 3a ed 5b 78 36 3e cc dd 99 55 67 5b e6 d5 f1
#> [176] e8 24 d8 7c 37 a6 8f a2 d4 35 ba 72 8c 3a f6 be c7 43 94 12 3a 31 0a d2 67
#> [201] 8b 5a c2 c9 d6 d7 20 ae 91 77 5b 82 a2 c7 42 40 e6 f2 62 9f 3d 77 ba 3b 71
#> [226] 1f a8 cc 65 c3 19 e5 5d 35 63 b2 da 67 72 ce 75 c8 fd 28 da 28 08 0e 1a 52
#> [251] 3b d7 b6 c9 6f 62 ac 24 a0 aa c7 39 85 4f 86 38 77 9a 08 4f 2f be fd 33 ff
#> [276] 2d 1f 51 b9 e4 33 16 6a 13 0a e2 f3 c7 6d 89 b8 2d 0a 20 6b 9c 4e ea ab d7
#> [301] 71 62 a7 b8 58 ff fa 0b 43 2a 37 fa 43 5d a6 99 d1 96 15 00 00 00 18 00 00
#> [326] 00 10 d3 99 06 7b c3 e2 23 06 fc 86 c1 d1 2b 92 8b 83 00 00 00 18 00 00 01
#> [351] 00 0f 91 ab 14 04 e4 9d ec 79 4b fa 48 a8 69 37 14 02 e3 3d e1 58 d6 85 76
#> [376] 1b c6 48 9e 4e f3 cc f6 69 f2 d8 b3 f2 7f 07 38 1b 18 57 2a d5 b5 6a 8f 22
#> [401] 86 3a 96 a2 cb fc c4 6b 1d 40 47 91 ff d3 85 0b 49 5c 39 4c 31 12 68 4e c2
#> [426] 6e d7 b9 d4 c6 1c 5d c3 98 db 1e 2f 1f d5 b4 8c a7 1a 40 2e c4 05 df 3b fe
#> [451] 47 fa de 79 00 21 f9 96 82 2b e1 35 dc 12 80 af ab 96 de bc b7 6e 96 80 23
#> [476] 6f f0 31 07 40 c2 cd 62 bb 85 23 35 0b ab 0f 90 53 45 08 9b 85 18 1e 55 08
#> [501] 6a 9b 28 90 37 1a ac e5 6b c4 8e 27 ee 69 59 1b af d1 95 6f fb 04 17 43 bd
#> [526] b2 d4 e0 9c ba 6a 9f 8c 81 72 9d 10 de 4b c4 7b df b4 32 9f ac d1 ae d7 bf
#> [551] 70 b3 0a b3 20 69 74 83 f7 19 cb 71 3d 81 ac ee 85 68 af 72 ed bc 12 24 fd
#> [576] 32 0d 46 2b e2 0a 4b 4e 9e f3 56 ac a2 a4 ec b3 f6 d0 7c 3e 09 bd 7d bd 9d
#> [601] 01 d7 0d 67 17 a3 bf 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 05 6e 61
#> [626] 6d 65 73 00 00 00 10 00 00 00 04 00 04 00 09 00 00 00 02 69 76 00 04 00 09
#> [651] 00 00 00 07 73 65 73 73 69 6f 6e 00 04 00 09 00 00 00 04 64 61 74 61 00 04
#> [676] 00 09 00 00 00 09 73 69 67 6e 61 74 75 72 65 00 00 00 fe
# Bob wants to read the message so he creates a key pair using
# Alice's public key and his private key:
pair_bob <- cyphr::keypair_openssl(pub = path_alice, key = path_bob)
cyphr::decrypt_string(secret, pair_bob)
#> [1] "hi bob"
# Clean up
unlink(path_alice, recursive = TRUE)
unlink(path_bob, recursive = TRUE)
