I am developing a class project in C# that encrypts the users and admins passwords. To encrypt I'm using the TripleDESCryptoServiceProvider
.
In the configuration app the user enters the key to be used for encryption and decryption of the passwords. I want to have a button to generate a key to help the user but I'm at a loss as to how I generate the 128 bits randomly. How a generate a key with 128-bits?
Take a look at the
RNGCryptoServiceProvider
class, specifically theGetNonZeroBytes
method. You could run that string of bytes though a base64 encoding to make it human readable, perhaps.To generate a random value for crypto use, you should use
RNGCryptoServiceProvider
:To turn this sequence of bytes into a string you could use hex (
BitConverter.ToString
) or Base64 (Convert.ToBase64String
).But there are some strange points here:
I understand perfectly what you want Instead of passing urls with htttp: //mydomain.com? Id = 123, you want to pass with an encrypted value. And when someone clicks on this url with encrypted id, you want to decrypt the value of the url.
There are 2 procedures for this:
In encryption: 1 - Convert the "ID" (usually an integer) to a string. Example var NewId = Convert.ToString ((ID); 2 - Use a phrase to shuffle the encryption. Example: "I love chocolate" (this phrase can be fished from your parameter database if you have one ...)
In decryption: 1 - Use the same blocking phrase to unscramble. 2 - Convert what was decrypted to an integer again, using Convert.ToInt32 (Shuffled Variable Above)
You will have to implement 2 functions:
Function for encrypt:
Function for Decryption:
When encrypting an ID as ID = "123", you will have a value for the ID as ID = "B8 + iXv5 / 8BUQEbHt8 // fGA ==".
When you decrypt the ID value "B8 + iXv5 / 8BUQEbHt8 // fGA ==" you will get "123" again.
A sample in C#:
I hope this can help you.