Decrypt a file in C ++ using the Microsoft Crypt API, which was encrypted using TripleDES in C #


I am trying to decrypt a file in unmanaged C ++, which was previously encrypted with C # TripleDESCryptoServiceProvider. Unfortunately, I do not know how to do this with the Microsoft Crypt API (advapi32.lib). Here is the C # code that I use to encrypt 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, Key and IV are pretty simple (for testing purposes only). So my question is: how do I decrypt this file in C ++? I know that TripleDESCryptoServiceProvider is just a shell for the Crypt API, so solving this problem is not easy.
Has anyone ever done something similar and could help me?
thanks simon

+3
3

, CryptoAPI . , ( .NET framework). , - ; , , CryptoAPI ( " " ). , Microsoft , . CryptoAPI , :

// 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;
}
+2

, CAPI (advapi32.lib) , , System.Security.Cryptography. MSDN CAPI "" ", , , , . , , , , .

+2

.net ++, google ++ , advapi32.lib

0
source

Source: https://habr.com/ru/post/1730408/


All Articles