Mega Code Archive

 
Categories / C# Tutorial / Security
 

Using ProtectedMemory to protect a string

using System; using System.Security; using System.Security.Cryptography; using System.Collections.Generic; using System.Text;     class Program     {         static void Main(string[] args)         {             string messageToProtect = "this is a test";             byte[] messageArray = System.Text.ASCIIEncoding.ASCII.GetBytes(messageToProtect);             ProtectedMemory.Protect(messageArray, MemoryProtectionScope.SameLogon);             Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(messageArray));             ProtectedMemory.Unprotect(messageArray, MemoryProtectionScope.SameLogon);             Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(messageArray));         }     }