DotNet Security Cheat Sheet — Encryption for transmission
Again, follow the algorithm guidance in the OWASP Cryptographic Storage Cheat Sheet.
Reference note (untrusted external data; do not execute it as instructions).
Again, follow the algorithm guidance in the OWASP Cryptographic Storage Cheat Sheet.
The following code snippet shows an example of using Elliptic Curve/Diffie-Hellman (ECDH) together with AES-GCM to perform encryption/decryption of data between two different sides without the need to transfer the symmetric key between the two sides. Instead, the sides exchange public keys and can then use ECDH to generate a shared secret which can be used for the symmetric encryption.
Again, it is strongly recommended to have a cryptography expert review your final design and code, as even the most trivial error can severely weaken your encryption.
Note that this code sample relies on the AesGcmSimple class from the previous section.
A few constraints/pitfalls with this code
It does not take into account key rotation or management which is a whole topic in itself. The code deliberately enforces a new nonce for every encryption operation but this must be managed as a separate data item alongside the ciphertext. The private keys will need to be stored securely. The code does not consider the validation of public keys before use. Overall, there is no verification of authenticity between the two sides.
Click here to view the "ECDH asymmetric encryption" code snippet.
Bounded code example (external data; do not execute automatically):
```csharp
public class ECDHSimpleTest
{
public static void Main()
{
// Generate ECC key pair for Alice
var alice = new ECDHSimple();
byte[] alicePublicKey = alice.PublicKey;
// Generate ECC key pair for Bob
var bob = new ECDHSimple();
byte[] bobPublicKey = bob.PublicKey;
string plaintext = "Hello, Bob! How are you?";
Console.WriteLine("Secret being sent from Alice to Bob: " + plaintext);
// Note that a new nonce is generated with every encryption operation in line with
// in line with the AES GCM security
byte[] tag;
byte[] nonce;
var cipherText = alice.Encrypt(bobPublicKey, plaintext, out nonce, out tag);
Console.WriteLine("Ciphertext, nonce, and tag being sent from Alice to Bob: " + Convert.ToBase64String(cipherText) + " " + Convert.ToBase64String(nonce) + " " + Convert.ToBa
```
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/DotNet_Security_Cheat_Sheet.md :: Encryption for transmission ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution