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