简体   繁体   中英

How to set up .net RSA with a specific key?

From what I understand, RSA is set up for the whole public/private key mumbojumbo but what if I have no need to establish a key online?

Bob and Alice meet in a pub and agree on a 1024bit number. This number is never to be repeated anywhere except while encrypting a message to send from one to the other.

Is it possible to use the RSA encryption tools built into .net to facilitate that scenario? I'd imagine it would go down like this

Dim encryptor as new something(ByteArrayContainingTheKey)
Dim EncryptedBytes() as byte = encryptor.encrypt(NotEncryptedBytes)

And the other way around at the other end, simple right? Yet all the material about RSA in .net focuses on the private/public keys and I have no clue how to manipulate it to a simpler state.

You're talking about symmetric encryption where only a single secret key is used which must be present for both (or more) parties. See also Difference between asymmetric and symmetric encryption methods?

This question provides code, but uses ECB mode which is not recommended. You should use some authenticated mode like GCM or CCM. It seems to me that the API doesn't provide it , so CBC mode should be used with a message authentication code. An AES key can be 128, 192 or 256-bit wide which is probably more than you need.

You can of course achieve a comparable thing with RSA which might give you more flexibility. Both parties generate two private/public key pairs (one for encryption and one for signature). Both parties meet and exchange their public keys or both key pairs so that the keys are verified. Now you can either directly encrypt something for the other or you can somehow establish an ephemeral symmetric key and use hybrid encryption or you can establish a ephemeral symmetric key by using Diffie-Hellman key exchange and sign the messages with the keys that were explicitly generated for signature. Diffie-Hellman key exchange can be done in the clear, but it must be verified/authentic. Of course a combination of the two is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM