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