3

I am trying to encrypt/decrypt a string using c#. I've found countless tutorials on how to this ex: this However most of them assume you already have the key. So my question is:

How do I generate the key ?

Thanks in advance.

Jonny
  • 2,787
  • 10
  • 40
  • 62

3 Answers3

4

If you're using the encryption classes in the System.Security.Cryptography namespace, use the Rfc2898DeriveBytes class (@CodeInChaos points out that it supersedes PasswordDeriveBytes) to derive a key from a password.

If a random key is OK, the SymmetricAlgorithm class has a GenerateKey method.

anton.burger
  • 5,637
  • 32
  • 48
2

It depends on you handle the keys.

If you automatically generate the key and just exchange the key over some channel with a key-exchange method then you should generate the key with some strong random number generator like RNGCryptoServiceProvider. Actually most Ciphers in .NET generate a random key automatically.

If you want to have some kind of user entered password then I suggest you to use the Rfc2898DeriveBytes class. There is also a Tutorial on the .NET Security blog about Rfc2898DeriveBytes.

vstm
  • 12,407
  • 1
  • 51
  • 47
1

Well depending on what key you want you can generate one here

http://randomkeygen.com/ or https://www.grc.com/passwords.htm

But what type of key are you after?

Adam
  • 16,089
  • 6
  • 66
  • 109