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