mardi 29 avril 2014

C# Same encryption algorithm, same input, different result.


Vote count:

0




I use this for security in a windows application, and it is important that if a user encrypts the same string they get the same result as any other user who encrypts the exact same string. I have been using this routine for 5 years in a windows application. Almost all the time, this works perfectly. However once in a great while, I will get a user who does not get the same result as everyone else.


In the code below, I changed the sEncriptKey and sInitVector values, however the values I am using are of the correct length and made up of simple just alpha numeric characters that never change. Also, sText is usually pretty small, like 8 to 30 characters.


Thanks for the help.


public static string EncryptString(string sText) {



string sEncriptKey = "012345678901234567890123456789AA";// 32 Chars
string sInitVector = "0123456789ABCDEF"; // 16 Chars;

byte[] oByteArrayText = Encoding.UTF8.GetBytes(sText);

System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();

MemoryStream ms = new MemoryStream();
byte[] rgbIV = Encoding.ASCII.GetBytes(sInitVector);
byte[] key = Encoding.ASCII.GetBytes(sEncriptKey);
CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);

cs.Write(oByteArrayText, 0, oByteArrayText.Length);

cs.Close();

return Convert.ToBase64String(ms.ToArray());
}


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire