Hi Gaurav Sharma here with more information about SecureStrings. This time I'll cover following topics:
Let us start with our first topic,
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
ProtectMemory()
{
IF ((Length OF SecureStringObj IS NOT 0) AND (SecureStringObj IS NOT Encrypted))
BEGIN Constrained Execution Region
CALL Win32Native.RtlEncryptMemory method AND Store Result IN @RES
IF (@RES Shows Error)
THROW CRYPTOGRAPHIC_EXCEPTION
ELSE
SET SecureStringObj.IsEncrypted to TRUE;
}
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
UnProtectMemory()
IF ((Length OF SecureStringObj IS NOT 0) AND (SecureStringObj IS Encrypted))
CALL Win32Native.RtlDecryptMemory method AND Store Result IN @RES
SET SecureStringObj.IsEncrypted to FALSE;
I think, comparing String and SecureString is not completely justified. SecureString was created taking in mind the shortcomings of String class. The way in which SecureString works like managing pointers, creating constrained execution regions, encryption and decryption, it is bound to be on the slower side of performance. In this section I'll try to show you exactly how much slower SecureString is as compared to tradition String class.
1: Int32 loopCounter = 0;
2: Int32 loopMaxCounter = 100000;
3: List<SecureString> secStrList = new List<SecureString>();
4: Stopwatch sw = new Stopwatch();
5: sw.Start();
6:
7: sw.Reset();
8: List<String> StrList = new List<String>();
9: sw.Start();
10: Console.WriteLine("-----------------------------------------------------------");
11: Console.WriteLine("Creating 100000 instances of strings");
12: for (loopCounter = 0; loopCounter < loopMaxCounter; loopCounter++)
13: {
14: String str = new string(new Char[] { 'a' });
15: StrList.Add(str);
16: }
17: Console.WriteLine("Created 100000 instances of string. Elapsed time (in milliseconds) " +
18: sw.Elapsed.Milliseconds.ToString());
19: Console.WriteLine("-----------------------------------------------------------");
20: sw.Stop();
21: sw.Reset();
22: sw.Start();
23: Console.WriteLine("-----------------------------------------------------------");
24: Console.WriteLine("Creating 100000 instances of secure strings");
25: for (loopCounter = 0; loopCounter < loopMaxCounter; loopCounter++)
26: {
27: SecureString obj = new SecureString();
28: obj.AppendChar('a');
29: secStrList.Add(obj);
30: }
31: Console.WriteLine("Created 100000 instances of secure string. Elapsed time (in milliseconds)" +
32: sw.Elapsed.Milliseconds.ToString());
33: sw.Stop();
34: Console.WriteLine("-----------------------------------------------------------");
1: SecureString secString = new SecureString();
2:
3: String str=String.Empty;
5:
6: Int32 loopCounter = 0;
7: Int32 loopMaxCounter = 10000;
8: Console.WriteLine("---------------------------------------------------------");
9: Console.WriteLine("Loop will run {0} times",loopMaxCounter);
10: Console.WriteLine("Current String Length is {0}", str.Length.ToString());
11: sw.Start();
14: str = str + "a";
15: }
16: Console.WriteLine("String insertion completed in {0} milliseconds",sw.Elapsed.Milliseconds.ToString());
17: Console.WriteLine("Current length of String is {0}.", str.Length.ToString());
18: Console.WriteLine("----------------------------------------------------------");
19: sw.Stop();
20: sw.Reset();
21: sw.Start();
22: Console.WriteLine("----------------------------------------------------------");
23: Console.WriteLine("Loop will run {0} times", loopMaxCounter);
24: Console.WriteLine("Current Secure String Length is {0}", str.Length.ToString());
27: try
28: {
29: secString.AppendChar('a');
31: catch (Exception ex)
32: {
33: Console.WriteLine(ex.ToString());
34: }
35: }
36: Console.WriteLine("Secure String insertion completed in {0} milliseconds", sw.Elapsed.Milliseconds.ToString());
37: Console.WriteLine("Current length of Secure String is {0}.", secString.Length.ToString());
38: Console.WriteLine("-----------------------------------------------------------");
39: sw.Stop();
40: Console.Read();
If we search for SecureString on web we can find a lot of questions going around related to pros and cons of SecureString usage. I'll compile a list of all of such type of questions in my next post. Below are some reference sources that I used to get information related to secure string internals.
If we search for SecureString on web we can find a lot of questions going around related to pros and cons of SecureString usage. I'll compile a list of all of such type of questions in my next post.
Below are some reference sources that I used to get information related to secure string internals.