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
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kwnan
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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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.
Avatar of kwnan
kwnan

ASKER

Ok thank you very much.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo