CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Using managed Data Protection to encrypt and decrypt data

247 Views
Copy Code Show/Hide Line Numbers
   1:  using System;
   2:  using System.Security.Cryptography;
   3:   
   4:  namespace ConsoleApplication1
   5:  {
   6:      class Program
   7:      {
   8:          static void Main(string[] args)
   9:          {
  10:              string PlainText = "Luciano Evaristo Guerche";
  11:              byte[] PlainData = (new System.Text.ASCIIEncoding()).GetBytes(PlainText);
  12:              byte[] Entropy = new byte[] { 29, 63, 63, 48, 36, 41, 89, 81, 38, 18, 47, 51, 8, 91, 32, 21 };
  13:              byte[] ProtectedData = System.Security.Cryptography.ProtectedData.Protect(PlainData, Entropy, DataProtectionScope.LocalMachine);
  14:              byte[] UnprotectedData = System.Security.Cryptography.ProtectedData.Unprotect(ProtectedData, Entropy, DataProtectionScope.LocalMachine);
  15:   
  16:              Console.WriteLine("Plain Text: {0}", PlainText);
  17:              Console.WriteLine();
  18:              Console.WriteLine("Plain Data: 0x{0}", BitConverter.ToString(PlainData).Replace("-", ""));
  19:              Console.WriteLine();
  20:              Console.WriteLine("Protected Data: 0x{0}", BitConverter.ToString(ProtectedData).Replace("-", ""));
  21:              Console.WriteLine();
  22:              Console.WriteLine("Unprotected Data: 0x{0}", BitConverter.ToString(UnprotectedData).Replace("-", ""));
  23:              Console.WriteLine();
  24:              Console.ReadKey(false);
  25:          }
  26:      }
  27:  }
by Luciano Evaristo Guerche (Gorše)
  January 04, 2010 @ 7:54am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate