I asked some questions before. Thanks for the help.
My need is to encrypt a URL. So I start off with a URL, I encrypt it, make a link on my page that uses the URL. When the user clicks the link it goes to a controller (MVC), I decrypt the URL, get my data and display it. It works very well so far.
What I am thinking to do is to make it so that the URL encryption is based on the users ID plus a chosen key value. Everything is on the server so the keys would just be in two scripts.
So how could I set my key. Say for example that I wanted the key to be equal to the user_name plus “hjshdasd” where the “hj..” is always same and secret to me. Currently the key is a byte array. How can I just make it the above string.
Am I correct in saying that I could set the IV to anything as everything is internal and known just to my own scripts. Any suggestions on how I could set this? Bear in mind that I want the encryption to always be the same so I guess my IV would always be the same as well as I would have the same key for a certain user every time.
Thanks,
private static byte[] key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
private static byte[] vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 221, 112, 79, 32, 114, 156 };
private ICryptoTransform encryptor, decryptor;
private UTF8Encoding encoder;
public SimplerAES()
{
RijndaelManaged rm = new RijndaelManaged();
encryptor = rm.CreateEncryptor(key, vector);
decryptor = rm.CreateDecryptor(key, vector);
encoder = new UTF8Encoding();
}
The
Rfc2898DeriveBytesclass is probably what you are looking for. Give it the user id, and your salt in the constructor. Your salt should not be based on text – it should be a completely random byte array.The purpose of this class is, “Given a string, such as a password, give me a byte[] suitable for use as a key.” The MSDN documentation has good examples on how to use it.
That being said; I’d be careful about putting sensitive information in a query string. If it is something sensitive, such as an SSN – don’t put it in the query string; even if you encrypt it.