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();
                }
            }
        }
Esempio n. 2
0
 public static extern bool CryptUnprotectData(SafeCryptoBlobIn dataIn, StringBuilder ppszDataDescr, IntPtr optionalEntropy, IntPtr pvReserved, IntPtr pPromptStruct, int dwFlags, SafeCryptoBlobOut pDataOut);