Using ProtectedMemory to protect a string : ProtectedData « Security « C# / CSharp Tutorial






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));

        }
    }








35.7.ProtectedData
35.7.1.Encrypted/Decrypted string
35.7.2.Using ProtectedData to protect string value
35.7.3.Using ProtectedMemory to protect a string