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