Avatar of kwnan
kwnan
 asked on

Generating AES-256 bits encryption key in C# ??

I want to generate AES encryption key to be sent to the the other party in order to communicate securely. In the beginning the two nodes will create a shared session key by using Deffie-Helman protocol, then one of them will genreate AES key and send it to the other node through the secure channel(i.e. DH protocol).

So Could you please help in generating AES-256 bits in C# without using it to encrypt any plaintext. I just want the key itself.

Regards
C#EncryptionNetworking Protocols

Avatar of undefined
Last Comment
kwnan

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
kwnan

ASKER
Thanks yes that what I need but I want to print the key itself and I have added the statment Console.WriteLine("My key is {0}", key); and did not work, it prints this: My key is System.Byte[]

Could you help in how to print the key.

Regards
kaufmed

Since we are dealing with bytes, I'm not sure printing it to the screen is going to yield friendly, readable text. We're dealing with values from 0 - 255, and the first 32 values (i.e. 0 - 31) are the "non-printable" characters. As it sounds, you wouldn't see anything for those characters.

To print the numeric values themselves, you can loop through the array:

foreach (byte b in key)
{
    Console.WriteLine(b.ToString());
}

Open in new window


If you'd like to try and turn these into characters, then you can use the ASCII class to decode the values:

string strKey = System.Text.Encoding.ASCII.GetString(key);

Open in new window


...but as I mentioned, I don't think this will be reliable for your purposes.
kwnan

ASKER
Ok thank you very much.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck