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