Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My task is to encrypt data using the Elliptic Curve Cryptography in .Net (specific from client), I tried using the sample from Microsoft but it seems to generate its own key everytime. I need to use my own key for this process, like creating a password "S3curEChaNNel_01?" and converting it to byte then use as a key, but i cant find a way to do this.

VB
Using alice As New ECDiffieHellmanCng()
                Dim abData() As Byte
                Dim Str = txtKey.Text 'custom password
                abData = System.Text.Encoding.Default.GetBytes(Str)
                Str = System.Text.Encoding.Default.GetString(abData)

                Dim bobPublicKey() As Byte
                Dim bobKey() As Byte
                Dim bob As New ECDiffieHellmanCng()

                bob.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash
                bob.HashAlgorithm = CngAlgorithm.Sha256
                bobPublicKey = bob.PublicKey.ToByteArray()
                bob.HmacKey = abData

                bobKey = bob.DeriveKeyMaterial(CngKey.Create(CngAlgorithm.Sha256))
'at this line i get an exception, "The requested operation is not supported."

                'Dim aliceKey As Byte() = alice.DeriveKeyMaterial(CngKey.Create(CngAlgorithm.Sha256))
                Dim encryptedMessage As Byte() = Nothing
                Dim iv As Byte() = Nothing
                txtOutput.Text = ByteArrayToString(Encrypt(bobKey, txtPlainStr.Text, encryptedMessage, iv))
            End Using


Pls help me out, any working sample will be very much appreciated

What I have tried:

I have tried using the
bobKey = bob.DeriveKeyMaterial(CngKey.Import(abData, CngKeyBlobFormat.EccPrivateBlob))

and

bobKey = bob.DeriveKeyMaterial(CngKey.Import(abData, CngKeyBlobFormat.EccPublicBlob))
Posted
Updated 4-Oct-16 12:45pm

1 solution

You can't. You need an elliptic curve key. That is way more complex and secure that converting something like "S3curEChaNNel_01?" to a byte array.

An example for a ECC key would be an ECC certificate issued by an certificate authority.

If you don't want to go the PKI road. Create one and and import as follows:
C#
//Save
var key = CngKey.Create(CngAlgorithm.Sha256);
var bob = new ECDiffieHellmanCng(key);
var x = bob.Key.Export(CngKeyBlobFormat.EccPrivateBlob);

//Load
bob = new ECDiffieHellmanCng(CngKey.Import(x, CngKeyBlobFormat.EccPrivateBlob))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900