Reputation: 2917
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.
Upvotes: 3
Views: 1237
Reputation: 12537
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
.
Upvotes: 2
Reputation: 5706
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.
Upvotes: 4
Reputation: 16199
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?
Upvotes: 1