CryptUnprotect() func not working...


  • I am using cryptprotect and cryptunprotect functions in a single function...

    but cryptunprotect functions doesnot decrypts correctly, only first 3 characters are correctly decrypted, later are not correct... the code is:


    string passPhrase = "saneeha";
                string entropyPassPhrase = "khalid";

                DATA_BLOB cipher= new DATA_BLOB();
                DATA_BLOB pDataIn = new DATA_BLOB();
                DATA_BLOB pdataOut = new DATA_BLOB(); ;

                pDataIn.pbData = Marshal.StringToHGlobalUni(passPhrase);
                pDataIn.cbData = passPhrase.Length;

    DATA_BLOB entropy = new DATA_BLOB();
                entropy.pbData = Marshal.StringToHGlobalUni(entropyPassPhrase);
                entropy.cbData = entropyPassPhrase.Length;

                success = CryptProtectData(ref pDataIn, null, ref entropy, (IntPtr)null, ref prompt, CryptProtectFlags.CRYPTPROTECT_VERIFY_PROTECTION, ref pdataOut);
                success = CryptUnprotectData(ref pdataOut, null, ref entropy, (IntPtr)null, ref prompt, CryptProtectFlags.CRYPTPROTECT_VERIFY_PROTECTION, ref cipher);


    The string returned by the above statement is not the  same as passPhrase as defined earlier.. Please explain why this behavior is shown....

    Tuesday, February 28, 2012 8:52 AM

All replies

  • Hi,

    I have to confess, that I didn't do any tests on my own. But I had a look at the documentation and I saw that CRYPTPROTECT_VERIFY_PROTECTION is not supported for the CryptProtectData call. At least the documentation states that it should be a flag of CRYPTPROTECT_LOCAL_MACHINE, CRYPTPROTECT_UI_FORBIDDEN and CRYPTPROTECT_AUDIT. Not sure if that could have cause the problem or not.

    And what is prompt in your code?

    With kind regards,


    Tuesday, February 28, 2012 11:04 AM

    I have used CRYPTPROTECT_UI_FORBIDDEN flag in both functions and also tried RYPTPROTECT_LOCAL_MACHINE flag in cryptProtect function, keeping CRYPTPROTECT_UI_FORBIDDEN flag  in cryptUnProtect  function,  but same result... 

    The prompt is made null through this procedure...

     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            private struct CRYPTPROTECT_PROMPTSTRUCT
                public int cbSize;
                public int dwPromptFlags;
                public IntPtr hwndApp;
                public String szPrompt;

            static private IntPtr NullPtr = ((IntPtr)((int)(0)));

            private const int CRYPTPROTECT_UI_FORBIDDEN = 0x1;
            private const int CRYPTPROTECT_LOCAL_MACHINE = 0x4;

            private static void InitPrompt(ref CRYPTPROTECT_PROMPTSTRUCT ps)
                ps.cbSize = Marshal.SizeOf(
                ps.dwPromptFlags = 0;
                ps.hwndApp = NullPtr;
                ps.szPrompt = null;

                                      new CRYPTPROTECT_PROMPTSTRUCT();
                InitPrompt(ref prompt);

    please guide me where am i wrong...

    Wednesday, February 29, 2012 5:11 AM
  • Hi,

    Please check the following reference, hope could make some help 

    Also, you could  visit the below link to see the various paid support options that are available to better meet your needs if you requires a more in-depth level of support.;en-us;offerprophone 

    Friday, March 30, 2012 6:04 AM