private static byte[] UnprotectData(byte[] data, int dwFlags)
        {
            byte[]            array             = null;
            SafeCryptoBlobIn  safeCryptoBlobIn  = null;
            SafeCryptoBlobOut safeCryptoBlobOut = null;

            try
            {
                safeCryptoBlobIn  = new SafeCryptoBlobIn(data);
                safeCryptoBlobOut = new SafeCryptoBlobOut();
                if (NativeMethods.CryptUnprotectData(safeCryptoBlobIn, null, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, dwFlags, safeCryptoBlobOut))
                {
                    NativeMethods.DATA_BLOB blob = safeCryptoBlobOut.Blob;
                    array = new byte[blob.cbData];
                    Marshal.Copy(blob.pbData, array, 0, blob.cbData);
                    safeCryptoBlobOut.ZeroBuffer();
                    return(array);
                }
                int    lastWin32Error         = Marshal.GetLastWin32Error();
                string additionalTraceMessage = string.Format(CultureInfo.InvariantCulture, "CryptUnprotectData: Win32 error:{0}", lastWin32Error);
                throw new ServerConfigurationErrorException(additionalTraceMessage);
            }
            finally
            {
                if (safeCryptoBlobIn != null)
                {
                    safeCryptoBlobIn.Close();
                }
                if (safeCryptoBlobOut != null)
                {
                    safeCryptoBlobOut.Close();
                }
            }
        }
Пример #2
0
 public void ZeroBuffer()
 {
     NativeMethods.DATA_BLOB blob = this.Blob;
     byte[] destination           = new byte[blob.cbData];
     Marshal.Copy(blob.pbData, destination, 0, blob.cbData);
 }