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