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
NULL
here, in which case the environment variableUSER_KEY
is checked and if that is not defined then~/.ssh/id_rsa
will be used.- envelope
A logical indicating if "envelope" encryption functions should be used. If so, then we use
openssl::encrypt_envelope()
andopenssl::decrypt_envelope()
. IfFALSE
then we useopenssl::rsa_encrypt()
andopenssl::rsa_decrypt()
. See the openssl docs for further details. The main effect of this is that usingenvelope = TRUE
will 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
NULL
then 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
TRUE
then 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 04 01 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 7d f4 34 c5 52 28 e5 93 11 1e 55
#> [51] 49 6e 37 30 e7 00 00 00 18 00 00 01 00 52 df 99 8f 53 e5 9d 32 69 a6 6e 7d
#> [76] 24 7d 87 65 41 ea a5 de 42 95 fe a3 fd c9 fd ad 91 23 e8 67 f7 d4 89 ca df
#> [101] 14 df 42 7b 8f e7 db e7 34 65 21 8d 05 ad ea 6e 15 2b 75 e8 40 ec 0f ed 21
#> [126] b7 31 45 98 3f 24 9d 4a 3b 9f f7 0c 04 8e 3b 7d a3 a0 83 51 29 8a 67 1a 3f
#> [151] 38 e0 d9 76 bc 84 04 ca 59 20 46 62 67 bf 67 cb 4a 4a a3 37 0b 98 16 8a 74
#> [176] 4a a6 f7 19 41 8c a2 4d 34 00 d5 b0 41 26 88 90 5f a4 2b 07 58 a1 d4 17 03
#> [201] e3 89 5d f9 4d a1 0c e6 d2 ee 49 bd b6 cf fe 24 b5 d3 cb bf 03 95 31 1a 79
#> [226] 3c 46 23 cd 95 08 7d b0 9e 84 5b e7 3b 7d ca 01 bc c0 dd eb da f9 2b c3 7b
#> [251] 8c 28 14 95 4d e0 60 00 c0 b7 64 fb d5 8b 8d 42 a3 67 eb 30 1c 67 85 84 38
#> [276] 55 5c 96 12 7c b9 a8 2b 15 ed 73 c0 a0 a0 6d a2 98 94 02 1e ed 01 41 7e eb
#> [301] de 12 4e 63 f4 df a7 46 7d 5d 35 15 87 6a c9 ad 44 a8 f8 00 00 00 18 00 00
#> [326] 00 10 06 41 27 ac 0b a0 45 2e 1c 4e 9f 42 ef 3f 05 1d 00 00 00 18 00 00 01
#> [351] 00 d1 48 9b e9 52 62 b8 90 90 90 82 89 0d a9 d5 d4 88 7a ac c2 36 d3 97 7b
#> [376] 50 27 fc 61 12 19 a3 cb 6f 21 0d 2a 2b 65 a3 6f 65 5b 0a 90 bc ae c2 d0 df
#> [401] 1a 70 50 47 27 3c 0f fc 59 2a 0a 53 dd 74 18 a8 2c e8 82 a5 3e 27 4d 4c 53
#> [426] ce ed c1 3b 1f 18 22 c5 27 9b 33 6d 4e 03 18 58 51 3b 2d 9c 4c ee b2 6e dd
#> [451] d0 c2 9c 66 64 a9 2c 7c a3 b1 f3 dc 5c 16 0d 1a ff 71 37 bb a6 9d b2 fd 77
#> [476] 2f 2c f8 f0 62 07 28 b5 79 46 a5 75 5a 1d d9 a3 2d 94 cb 8d d4 97 b9 d1 eb
#> [501] bf 8d 0f ec 9c 91 6a 1d f3 36 74 e0 ce 2e 0e 89 27 92 49 45 bf fb 8c be b2
#> [526] 0b 09 d2 78 60 49 40 0e 1b 52 e4 95 2e b1 db 67 d0 94 c5 90 11 ec f8 07 fb
#> [551] 5f d1 6e 31 d2 70 10 ad 6c c0 a7 ad 9b c9 31 d5 89 7a d7 a2 df 99 f9 c6 b3
#> [576] e8 9d 6c 44 3e 95 ab b4 6d 3b 5c 53 ed 7a 95 a9 bb ea d5 76 37 ca 13 e3 99
#> [601] 79 91 4c 1a 1d e1 68 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)