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 d3 42 61 7c 5b 6d c0 db c5 47 7c
#> [51] 68 9e 8c 8b 29 00 00 00 18 00 00 01 00 8d ac 75 2f cf fc 52 06 be e2 24 2e
#> [76] fe 41 08 9e e8 f9 e1 9c 38 fd d3 5a 77 6a 33 eb a1 4e 23 c7 ef 28 39 81 ba
#> [101] b2 3c 08 2e 7d 76 61 aa ca c4 32 38 28 f3 52 c5 5d 08 91 6c 7c 52 c6 0d 01
#> [126] e0 0c dc 9c 8d 18 c5 cb 9c a8 f5 ba e8 97 d4 9b de e7 a4 96 ff 0e 47 1a f1
#> [151] e7 84 53 a0 8f e8 0f 8e 95 6a 76 46 4e 38 d0 41 7d ed c0 49 1d a6 d2 75 fb
#> [176] 80 20 6d 3a b6 4d 0c 13 22 26 2f a4 00 43 1c 92 b4 26 2a 94 11 9b f1 d7 75
#> [201] 20 7d cb 87 5b ef 3e d8 8f ac e1 fd d5 d9 48 d2 da 26 4d 25 0a 27 07 af df
#> [226] b7 3a 27 a2 c1 c7 e5 7a 65 4f e3 09 ed c0 2f 18 be 6c 20 7b d6 fe df 3c 39
#> [251] f6 37 de 85 21 09 fa dc 76 2c 9c 30 76 27 a9 e3 0f 62 de c5 50 3a 6e 69 2d
#> [276] bb 51 6e ba 6b e7 fe 4b 3a 80 a1 02 bc 38 8d e5 d0 e3 33 6d e5 74 7f 2c 6c
#> [301] 70 fc 40 db cc 04 fd b6 9a 36 1b 27 91 26 81 03 3f fe 91 00 00 00 18 00 00
#> [326] 00 10 51 b9 91 e3 dd 55 30 50 41 a8 51 fb 6f ab bd 8b 00 00 00 18 00 00 01
#> [351] 00 3a 1f 2c 0f 14 fa f3 c8 88 e7 2f 5b b2 e0 f0 58 e2 f2 b4 94 0a ac a2 56
#> [376] 58 51 06 2f 0c 2d 44 4a 1e cb eb a6 6b 3d 7c 77 29 f2 db 3c 74 ff 13 95 39
#> [401] 9a a8 78 19 34 cb a1 96 fa dc 79 c3 7b 66 21 dc f1 07 59 68 03 fc 44 1e 55
#> [426] 4d 97 81 7e 24 9d 20 d2 27 a4 87 bc b8 c5 54 a2 d0 5e 97 6b 26 78 eb d3 21
#> [451] fe 17 c5 7c 3c 5a 72 d2 72 66 2b 26 e7 a9 c3 11 4b 9c 56 c0 bd e3 9f 46 00
#> [476] 76 92 e5 c5 c1 36 b1 26 56 86 e7 e1 27 98 58 02 77 82 a6 0e 91 f5 75 7c c4
#> [501] 92 72 ea 25 02 e5 e7 c4 16 8c d3 13 48 6b 40 b8 e7 98 9b a8 f3 7f e4 70 22
#> [526] b3 73 c2 48 be db 96 70 10 0d c2 be f1 be ad bb 51 74 01 a9 44 65 01 2e 00
#> [551] cc a7 c5 a0 96 01 47 84 58 5a 75 7d 20 bd cd 99 9e 29 28 dc 40 f6 ba ce 81
#> [576] 1f ef 00 b5 89 4f bf a8 63 8e 14 58 55 22 6a f0 80 bb 9c cd ae 03 f5 df 4b
#> [601] 67 db 41 18 70 30 3c 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)
