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