Using ProtectedData to protect string value : 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 fileMessage = "this is another test";
            byte[] fileMsgArray = System.Text.ASCIIEncoding.ASCII.GetBytes(fileMessage);

            byte[] entropy = { 0, 1, 3, 5, 7, 9 };
            byte[] protectedMessage = ProtectedData.Protect(fileMsgArray, entropy, DataProtectionScope.CurrentUser);

            Console.WriteLine("Protected byte array:");
            Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(protectedMessage));

            byte[] clearMessage = ProtectedData.Unprotect(protectedMessage, entropy, DataProtectionScope.CurrentUser);
            Console.WriteLine("Unprotected/Decrypted Data:");
            Console.WriteLine(ASCIIEncoding.ASCII.GetString(clearMessage));

        }
    }








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