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