using System.Security.Cryptography;
public class DataProtectionSample
static byte [] s_additionalEntropy = { 9, 8, 7, 6, 5 };
public static void Main()
byte [] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
byte [] encryptedSecret = Protect( secret );
Console.WriteLine("The encrypted byte array is:");
PrintValues(encryptedSecret);
byte [] originalData = Unprotect( encryptedSecret );
Console.WriteLine("{0}The original data is:", Environment.NewLine);
PrintValues(originalData);
public static byte [] Protect( byte [] data )
return ProtectedData.Protect( data, s_additionalEntropy, DataProtectionScope.CurrentUser );
catch (CryptographicException e)
Console.WriteLine("Data was not encrypted. An error occurred.");
Console.WriteLine(e.ToString());
public static byte [] Unprotect( byte [] data )
return ProtectedData.Unprotect( data, s_additionalEntropy, DataProtectionScope.CurrentUser );
catch (CryptographicException e)
Console.WriteLine("Data was not decrypted. An error occurred.");
Console.WriteLine(e.ToString());
public static void PrintValues( Byte[] myArr )
foreach ( Byte i in myArr )
Console.Write( "\t{0}", i );