Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 601497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:43:18+00:00 2026-05-13T16:43:18+00:00

I’m trying to decrypt a file in unmanaged C++ that was previously encrypted with

  • 0

I’m trying to decrypt a file in unmanaged C++ that was previously encrypted with C# TripleDESCryptoServiceProvider. Unfortunately I do not have a clue how to do that with the Microsoft Crypt API (advapi32.lib). Here is the C# code that I use to encrypt the data:

private static void EncryptData(MemoryStream streamToEncrypt)
    {
        // initialize the encryption algorithm
        TripleDES algorithm = new TripleDESCryptoServiceProvider();

        byte[] desIV = new byte[8];
        byte[] desKey = new byte[16];

        for (int i = 0; i < 8; ++i)
        {
            desIV[i] = (byte)i;
        }

        for (int j = 0; j < 16; ++j)
        {
            desKey[j] = (byte)j;
        }

        FileStream outputStream = new FileStream(TheCryptedSettingsFilePath, FileMode.OpenOrCreate, FileAccess.Write);
        outputStream.SetLength(0);

        CryptoStream encStream = new CryptoStream(outputStream, algorithm.CreateEncryptor(desKey, desIV),
            CryptoStreamMode.Write);

        // write the encrypted data to the file
        encStream.Write(streamToEncrypt.ToArray(), 0, (int)streamToEncrypt.Length);

        encStream.Close();
        outputStream.Close();
    }

As you can see the Key and the IV is quite simple (just for testing purpose). So my question is, how do I decrypt that file in C++? I know that the TripleDESCryptoServiceProvider is just a wrapper for the Crypt API, so it cannot be that difficult to solve this problem.

Does anyone ever did something like that and can help me?

Thx Simon

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:43:19+00:00Added an answer on May 13, 2026 at 4:43 pm

    Once you get in the groove of things, CryptoAPI is relatively straightforward to use. The problem is doing it in a way that is compatible with other cryptography libraries (including .NET framework). I have successfully done this before, but it has been a while; the major sticking point is figuring out how to convert a plain text key into a format usable with CryptoAPI (which operates with “key blobs”). Luckily Microsoft has given us a working, if tedious, example. As for the CryptoAPI way of doing things, here is an example:

    // 1. acquire a provider context.
    // the Microsoft Enhanced provider offers the Triple DES algorithm.
    HCRYPTPROV hProv = NULL;
    if(CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    {
        // 2. generate the key; see Microsoft KB link above on how to do this.
        HKEY hKey = NULL;
        if(ImportPlainTextSessionKey(hProv, lpKeyData, cbKeyData, CALG_3DES, &hKey))
        {
            // 3. set the IV.
            if(CryptSetKeyParam(hKey, KP_IV, lpIVData, 0))
            {
                // 4. read the encrypted data from the source file.
                DWORD cbRead = 0;
                while(ReadFile(hSourceFile, buffer, 8192, &cbRead, NULL) && cbRead)
                {
                    // 5. decrypt the data (in-place).
                    BOOL bFinal = cbRead < 8192 ? TRUE : FALSE;
                    DWORD cbDecrypted = 0;
                    if(CryptDecrypt(hKey, NULL, bFinal, 0, buffer, &cbDecrypted))
                    {
                        // 6. write the decrypted data to the destination file.
                        DWORD cbWritten = 0;
                        WriteFile(hDestFile, buffer, cbDecrypted, &cbWritten, NULL);
                    }
                }
            }
            CryptDestroyKey(hKey);
            hKey = NULL;
        }
        CryptReleaseContext(hProv, 0);
        hProv = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.