Exemplo n.º 1
0
 static internal void InitPromptstruct(
     ref NativeMethods.CRYPTPROTECT_PROMPTSTRUCT ps)
 {
     ps.cbSize = Marshal.SizeOf(
         typeof(NativeMethods.CRYPTPROTECT_PROMPTSTRUCT));
     ps.dwPromptFlags = 0;
     ps.hwndApp       = NativeMethods.NullPtr;
     ps.szPrompt      = null;
 }
Exemplo n.º 2
0
        internal static byte[] UnprotectData(byte[] data,
                                             int dwFlags)
        {
            byte[] clearText = null;

            // Copy data into unmanaged memory.
            NativeMethods.DATA_BLOB din =
                new NativeMethods.DATA_BLOB();
            din.cbData = data.Length;
            din.pbData = Marshal.AllocHGlobal(din.cbData);
            Marshal.Copy(data, 0, din.pbData, din.cbData);

            NativeMethods.CRYPTPROTECT_PROMPTSTRUCT ps =
                new NativeMethods.CRYPTPROTECT_PROMPTSTRUCT();

            InitPromptstruct(ref ps);

            NativeMethods.DATA_BLOB dout =
                new NativeMethods.DATA_BLOB();

            try {
                bool ret =
                    NativeMethods.CryptUnprotectData(
                        ref din,
                        null,
                        NativeMethods.NullPtr,
                        NativeMethods.NullPtr,
                        ref ps,
                        dwFlags,
                        ref dout);

                if (ret)
                {
                    clearText = new byte[dout.cbData];
                    Marshal.Copy(dout.pbData,
                                 clearText, 0, dout.cbData);
                    NativeMethods.LocalFree(dout.pbData);
                }
                else
                {
                    #if (DEBUG)
                    Console.WriteLine("Decryption failed: " +
                                      Marshal.GetLastWin32Error().ToString());
                    #endif
                }
            }

            finally {
                if (din.pbData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(din.pbData);
                }
            }

            return(clearText);
        }