.NET Secure Memory Structures

后端 未结 6 958
面向向阳花
面向向阳花 2020-12-05 16:52

I know the .NET library offers a way of storing a string in a protected/secure manner = SecureString.

My question is, if I would like to store a byte array, what wou

6条回答
  •  悲&欢浪女
    2020-12-05 17:04

    as of .Net 2.0 use the ProtectedData.Protect Method, looks like setting the scope to DataProtectionScope.CurrentUser should give the same desired effect as secure string

    example usage taken from here

    http://msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata.protect.aspx

    using System;
    using System.Security.Cryptography;
    
    public class DataProtectionSample
    {
    // Create byte array for additional entropy when using Protect method. 
        static byte [] s_aditionalEntropy = { 9, 8, 7, 6, 5 };
    
        public static void Main()
        {
    // Create a simple byte array containing data to be encrypted. 
    
    byte [] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
    
    //Encrypt the data. 
            byte [] encryptedSecret = Protect( secret );
            Console.WriteLine("The encrypted byte array is:");
            PrintValues(encryptedSecret);
    
    // Decrypt the data and store in a byte array. 
            byte [] originalData = Unprotect( encryptedSecret );
            Console.WriteLine("{0}The original data is:", Environment.NewLine);
            PrintValues(originalData);
    
        }
    
        public static byte [] Protect( byte [] data )
        {
            try
            {
                // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted 
                //  only by the same current user. 
                return ProtectedData.Protect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
            } 
            catch (CryptographicException e)
            {
                Console.WriteLine("Data was not encrypted. An error occurred.");
                Console.WriteLine(e.ToString());
                return null;
            }
        }
    
        public static byte [] Unprotect( byte [] data )
        {
            try
            {
                //Decrypt the data using DataProtectionScope.CurrentUser. 
                return ProtectedData.Unprotect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
            } 
            catch (CryptographicException e)
            {
                Console.WriteLine("Data was not decrypted. An error occurred.");
                Console.WriteLine(e.ToString());
                return null;
            }
        }
    
        public static void PrintValues( Byte[] myArr )  
        {
              foreach ( Byte i in myArr )  
                {
                     Console.Write( "\t{0}", i );
                 }
          Console.WriteLine();
         }
    
    }
    

提交回复
热议问题