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