Csharp/CSharp Tutorial/Security/ProtectedData — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:17, 26 мая 2010

Encrypted/Decrypted string

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

Using ProtectedData to protect string value

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

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