Net Encryption Library
ECDH Shared Secret
Basic Tasks > ECDH Shared Secret

ECDH Shared Secret Overview

Eliptic Curve Diffie-Hellman (ECDH) is similar to RSA but instead of having a public key and a private key; both parties create keys and then share public keys to be able to encrypt and decrypt each others data. 

 

C# Copy Code
//Create private keys for each person
EcdhPrivateKey johnsSecretKey = new EcdhPrivateKey();
EcdhPrivateKey janesSecretKey = new EcdhPrivateKey();
 
//Get the public keys
string johnsPublicKey = johnsSecretKey.GetPublicKey().PublicKeyString;
string janesPublicKey = janesSecretKey.GetPublicKey().PublicKeyString;
 
Console.WriteLine("John's Public Key");
Console.WriteLine(johnsPublicKey);
 
Console.WriteLine("Janes's Public Key");
Console.WriteLine(janesPublicKey);
 
//Get the shared key (these will calculate the same)
string johnsSharedKey = johnsSecretKey.GetSharedSecretKey(new EcdhPublicKey(janesPublicKey)).SharedKeyString;
string janesSharedKey = janesSecretKey.GetSharedSecretKey(new EcdhPublicKey(johnsPublicKey)).SharedKeyString;
 
Console.WriteLine("John's Shared Key");
Console.WriteLine(johnsSharedKey);
 
Console.WriteLine("Janes's Shared Key");
Console.WriteLine(janesSharedKey);
 
Encryption encryption = new Encryption(); //Trial Mode
//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode
 
const string unencryptedString = "This is the input";
Console.WriteLine("The unencrypted string: {0}", unencryptedString);
 
string encryptedString = encryption.EncryptString(EncryptionProvider.Rijndael, johnsSharedKey, unencryptedString);
Console.WriteLine("The encrypted string: {0}", encryptedString);
 
string decryptedString = encryption.DecryptString(EncryptionProvider.Rijndael, janesSharedKey, encryptedString);
Console.WriteLine("The decrypted string: {0}", decryptedString);
VB.NET Copy Code
'Create private keys for each person
Dim johnsSecretKey As New EcdhPrivateKey()
Dim janesSecretKey As New EcdhPrivateKey()
 
'Get the public keys
Dim johnsPublicKey As String = johnsSecretKey.GetPublicKey().PublicKeyString
Dim janesPublicKey As String = janesSecretKey.GetPublicKey().PublicKeyString
 
Console.WriteLine("John's Public Key")
Console.WriteLine(johnsPublicKey)
 
Console.WriteLine("Janes's Public Key")
Console.WriteLine(janesPublicKey)
 
'Get the shared key (these will calculate the same)
Dim johnsSharedKey As String = johnsSecretKey.GetSharedSecretKey(New EcdhPublicKey(janesPublicKey)).SharedKeyString
Dim janesSharedKey As String = janesSecretKey.GetSharedSecretKey(New EcdhPublicKey(johnsPublicKey)).SharedKeyString
 
Console.WriteLine("John's Shared Key")
Console.WriteLine(johnsSharedKey)
 
Console.WriteLine("Janes's Shared Key")
Console.WriteLine(janesSharedKey)
 
Dim encryption As New Encryption() 'Trial Mode
'Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode
 
Const unencryptedString As String = "This is the input"
Console.WriteLine("The unencrypted string: {0}", unencryptedString)
 
Dim encryptedString As String = encryption.EncryptString(EncryptionProvider.Rijndael, johnsSharedKey, unencryptedString)
Console.WriteLine("The encrypted string: {0}", encryptedString)
 
Dim decryptedString As String = encryption.DecryptString(EncryptionProvider.Rijndael, janesSharedKey, encryptedString)
Console.WriteLine("The decrypted string: {0}", decryptedString)