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