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