CryptUnprotectData failing when code compiled on Windows 7
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: CryptUnprotectData failing when code compiled on Windows 7

Hybrid View

  1. #1
    Join Date
    Sep 2012
    Posts
    6

    CryptUnprotectData failing when code compiled on Windows 7

    Dear All,

    I need some help regarding a strange issue with CryptUnprotectData function.

    I have a C++ code that reads the database password from the registry and decrypts it using CryptUnprotectData. We have to deploy this application on a Windows 7 machine.

    When I compile my code on Windows XP and run it on the test Windows 7 machine, it works absolutely fine. When I compile the same code on my laptop having Windows 7 and run it on the test Windows 7 machine, CryptUnprotectData fails with GetLastError() return '87'. If I run this application on my own laptop with Windows 7 on it, it again works fine probably because my laptop has a lot of things installed including Visual Studio and all the service packs etc.

    I believe I have missed out installing some dependency on the test Windows 7 machine but I am unable to figure out what is that. What is it that's making the Windows XP compiled code running fine on the test machine and not the code that's compiled on Windows 7.

    Here is the call to CryptUnprotectData in the code:

    if (CryptUnprotectData(
    &cipherText,
    NULL,
    NULL, // Optional entropy
    NULL, // Reserved
    NULL, // Optional PromptStruct
    CRYPTPROTECT_UI_FORBIDDEN,
    &plainText))
    {
    password.Append((char*)plainText.pbData, plainText.cbData);
    }
    else
    {
    char buff[256];
    sprintf (buff, "CryptUnprotectData Error %d", GetLastError());
    AfxMessageBox(buff);
    }


    Please suggest. Thanks in advance.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CryptUnprotectData failing when code compiled on Windows 7

    You need Crypt32.dll for CryptoAPI, but that should be already available. You can check your application dependencies with depends.exe (www.dependencywalker.com).

    Error 87 means a parameter is incorrect.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Sep 2012
    Posts
    6

    Re: CryptUnprotectData failing when code compiled on Windows 7

    Thanks for replying.

    Yeah crypt32.dll is already there. Also, depends.exe doesn't show any missing dlls. '87' is hard to understand because the same code works fine on my laptop having Windows 7 on it and the same code compiled on Windows XP works fine on the test Windows 7 machine as well.

    It seems to be some permission issue even though I am running the application as administrator. One more piece of information, when I debug my code, it fails at the same place and gives the same error but when I run my exe directly, it works fine. May be this could help understanding why it's failing on the other test machine.

  4. #4
    Join Date
    Sep 2012
    Posts
    6

    Re: CryptUnprotectData failing when code compiled on Windows 7

    I found the fix to the issue. SP1 was the culprit. I had created a VM and SP1 wasn't installed by default. Installing it resolved the issue.

    Thanks.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CryptUnprotectData failing when code compiled on Windows 7

    It might not be just this piece of code that is the problem. Maybe it's something above it. The entire sequence of calls to the CryptAPIs. Maybe it's something with cipherText? Was the data encrypted on the same machine with the credentials of the same user? Otherwise this fails.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CryptUnprotectData failing when code compiled on Windows 7

    It might not be just this piece of code that is the problem. Maybe it's something above it. The entire sequence of calls to the CryptAPIs. Maybe it's something with cipherText? Was the data encrypted on the same machine with the credentials of the same user? Otherwise this fails.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Sep 2012
    Posts
    6

    Re: CryptUnprotectData failing when code compiled on Windows 7

    Cilu it was the Service Pack issue. I installed SP1 on the test Windows 7 machine and it started working. The surprising part is that the XP compiled version was working fine without SP1. Anyways, I hope this would be helpful for others trying out the same things.

    Thanks for the help.

    Cheers

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CryptUnprotectData failing when code compiled on Windows 7

    SP1 for what? Visual Studio? I guess you know that you need the appropriate VC++ redistributable package on the target machine. http://www.codeguru.com/forum/showthread.php?t=494136
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Sep 2012
    Posts
    6

    Re: CryptUnprotectData failing when code compiled on Windows 7

    SP1 for Windows 7. I had already installed VC++ redist. Somehow Windows didn't suggest me about the updates for SP1. This was the only difference in my laptop's and the test machine's configurations.

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CryptUnprotectData failing when code compiled on Windows 7

    Strange, but then this SP1 for Win7 is a prerequisite for your application. Make sure your setup handles that right. Probably you can narrow that to which component from the SP1 is important here and distribute it as a pre-requisite together with your app.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  11. #11
    Join Date
    Sep 2012
    Posts
    6

    Re: CryptUnprotectData failing when code compiled on Windows 7

    Yes I believe since the app was compiled with this SP1, it's probably using the crypto related components of SP1.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center