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