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
NULLhere, in which case the environment variableUSER_KEYis checked and if that is not defined then~/.ssh/id_rsawill be used.- envelope
A logical indicating if "envelope" encryption functions should be used. If so, then we use
openssl::encrypt_envelope()andopenssl::decrypt_envelope(). IfFALSEthen we useopenssl::rsa_encrypt()andopenssl::rsa_decrypt(). See the openssl docs for further details. The main effect of this is that usingenvelope = TRUEwill 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
NULLthen 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
TRUEthen 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 02 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 81 d0 4e 10 c6 82 83 b1 0d 9c e7
#> [51] 46 8f 4c 3b 72 00 00 00 18 00 00 01 00 f4 33 ac 9c 1a c4 07 82 c4 80 0b 02
#> [76] c8 90 18 15 83 9e a6 df cf cd b3 29 a8 88 4d d7 75 2e d7 25 8b e5 5c ba 13
#> [101] 33 5f 81 90 b6 6d ef 94 d0 16 43 79 15 bc e2 60 85 6c e2 9b 1f 6b 65 7b 56
#> [126] 22 7e 28 88 64 16 e4 1e 1b 2c 20 e4 23 b6 c5 19 82 e6 c3 86 63 31 96 d2 e8
#> [151] 75 27 b2 6b 05 ac ff 7f 25 f9 85 3d 0e 08 db a9 9f ea ba bb e4 95 d0 05 c1
#> [176] f0 fc 03 d4 d1 b1 6c 01 08 5b 2d ea 41 23 64 e9 35 ad b6 3d 4f a4 54 43 57
#> [201] 1a 3f 9c 26 10 47 68 ed 6a 57 9b 89 51 50 db 7c 78 f6 e5 07 f9 0f 7c 13 65
#> [226] 72 96 0c 3e d9 42 3e cf d3 ab 83 4d b1 07 48 d4 a9 48 16 7a 00 6c 3d 98 b1
#> [251] 27 c8 02 d2 1f 04 05 dd d8 2d 74 81 ea e4 dc d1 ed d7 4f 16 68 33 48 9d 9a
#> [276] c2 ee d5 7e 75 9b de c5 7d a8 6c 0c 48 84 66 7b ee 9a a8 53 1d 00 f8 57 90
#> [301] 6f c2 92 da c0 87 31 8b f6 a7 7f df ad 11 ec 36 f0 9a e0 00 00 00 18 00 00
#> [326] 00 10 f5 d0 13 f9 9c e7 c8 78 c8 2b 86 d1 89 55 71 94 00 00 00 18 00 00 01
#> [351] 00 80 75 b0 b7 64 d8 b1 ae 6c e2 30 e0 90 ad 12 05 71 cc 2c c2 e6 92 76 db
#> [376] 48 ef b1 3e 3d 31 fa da b1 a6 49 f6 35 5f ce f6 04 3b e4 f5 1c 00 58 cc bb
#> [401] 21 e2 1d 77 8c 79 6c 73 a1 78 f8 b5 50 f3 10 6f 31 77 e0 b8 10 57 7f 1a 67
#> [426] 02 25 f6 a2 5b 1b 55 c0 d3 1e 52 d8 ad 25 f9 40 76 7e d8 fb 63 1f 6e b2 da
#> [451] 60 3a 3c 9f 75 e4 93 d8 6c 43 04 1e 7d 45 29 d8 7e 82 ef aa cc 24 41 af ff
#> [476] 2d 73 2a af bb 6d 2b 6f 30 28 93 88 1f e3 e0 e2 62 36 1f cd 28 b3 ed c9 01
#> [501] a9 ab 61 34 ff e4 fb e5 7c 47 ff cb 24 cc a8 fa dc 77 de ab a5 f5 cd 32 73
#> [526] 1d f7 ce ec 43 a0 9e 4c b4 86 6d 5d a4 37 37 33 ff 01 6c 8d f3 2f b8 cc 40
#> [551] 8d a7 c0 3b 1e 41 5a 02 b1 0d b7 fc 62 2b 85 97 26 73 4f d2 85 00 1e 33 b6
#> [576] 57 1b 15 ef 11 4f e0 62 33 67 06 0a 97 e6 6f 37 5a 6c 6f 18 13 04 3c 97 1c
#> [601] 5b cb 33 ec a2 88 2b 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)
