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