Csharp/C Sharp by API/System.Security.Cryptography/ProtectedData

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

ProtectedData.Protect

  

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


ProtectedData.Unprotect

  

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