Encrypted/Decrypted string : ProtectedData « Security « C# / CSharp Tutorial






using System;
using System.Text;
using System.Security.Cryptography;

class MainClass
{
    public static void Main()
    {
        string str = "string";

        byte[] entropy = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };

        byte[] enc = ProtectedData.Protect(Encoding.Unicode.GetBytes(str), entropy, DataProtectionScope.LocalMachine);

        Console.WriteLine("\nEncrypted string = {0}", BitConverter.ToString(enc));

        byte[] dec = ProtectedData.Unprotect(enc, entropy, DataProtectionScope.CurrentUser);

        Console.WriteLine("\nDecrypted data using CurrentUser scope = {0}", Encoding.Unicode.GetString(dec));
    }
}
Encrypted string = 01-00-00-00-D0-8C-9D-DF-01-15-D1-11-8C-7A-00-C0-4F-C2-97-EB-01-00-00-00-10-5D-50-
C0-33-89-20-49-A3-DD-99-EC-D2-99-48-83-04-00-00-00-02-00-00-00-00-00-03-66-00-00-A8-00-00-00-10-00-0
0-00-46-11-DD-4B-03-12-CA-0B-26-DF-A0-52-18-FC-8C-8A-00-00-00-00-04-80-00-00-A0-00-00-00-10-00-00-00
-A2-2F-CF-2A-22-A9-77-00-D1-CC-73-FD-D7-ED-74-5E-10-00-00-00-10-DA-49-47-7D-C9-55-3F-05-26-F7-D2-1A-
80-1A-AD-14-00-00-00-6E-D7-CC-91-BA-6B-64-54-DC-12-A7-CA-F2-08-A2-B6-19-02-E4-B6

Decrypted data using CurrentUser scope = string








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