Compartilhar via


IPassportCrypt::Decrypt

IPassportCrypt::Decrypt

Decrypts data encrypted with IPassportCrypt::Encrypt, using the same key.

Syntax

HRESULT Decrypt(
    BSTR rawData,
    BSTR *pUnencrypted
);

Parameters

  • rawData
    [in] A BSTR containing the data to be decrypted.
  • pUnencrypted
    [out, retval] A pointer to the BSTR containing the decrypted data.

Return values

Returns one of the following:

S_OK Success.
E_FAIL Failure. The amount of data to be decrypted was greater than 5498 bytes or the Passport Manager object was not correctly configured.

Example

In this example, a string is compressed, encrypted, decrypted, and finally decompressed. The displayed string is the same as the original string.

#include "stdafx.h"
#include <atlbase.h>
#using <mscorlib.dll>
#import "C:/WINNT/system32/MicrosoftPassport/msppmgr.dll" named_guids raw_interfaces_only no_namespace
using namespace System;

// This is the entry point for this application

int _tmain(void)
{
 HRESULT hr;
 hr = CoInitialize(NULL);

 IPassportCrypt * piCrypt;
 hr = CoCreateInstance(CLSID_Crypt, NULL, CLSCTX_INPROC_SERVER, IID_IPassportCrypt, (void**)&piCrypt);
 IUnknown* pII_IPassportCrypt;
 piCrypt->QueryInterface(IID_IPassportCrypt, (void**)&pII_IPassportCrypt);

 //Define and display original string
 BSTR bstrIn = ::SysAllocString(L"This is a string to be compressed, encrypted, transmitted, decrypted, and finally decompressed.");
 BSTR pbstrOut;
 Console::Write("Original String: ");
 Console::WriteLine(bstrIn);

 //Compress and encrypt the string
 hr = piCrypt->Compress(bstrIn,&pbstrOut);
 hr = piCrypt->Encrypt(pbstrOut,&bstrIn);

 //Send the encrypted and compressed string over the wire here

 //Decrypt, decompress, and display the string
 hr = piCrypt->Decrypt(bstrIn,&pbstrOut);
 hr = piCrypt->Decompress(pbstrOut, &bstrIn);
 Console::Write("Reconstructed string: ");
 Console::WriteLine(bstrIn);

//cleanup
hr = piCrypt->Release();
CoUninitialize();
}

Remarks

Typically, this method is used in conjunction with the Encrypt, Compress, and Decompress methods. The preceding example uses these four methods together to simulate transmitting a compressed and encrypted string over the Internet. When the transmitted string is received, it is decrypted and decompressed and the original string is displayed.

After the Encrypt method is executed, the Decrypt method must be performed to make the data readable. The Encrypt and Decrypt methods are used to help maintain the data's security.

Empty (NULL) input returns a NULL. Empty or NULL input also returns S_OK, but NULL is passed to the pUnencrypted parameter. Attempting to decrypt a blob larger than 5498 bytes will fail. However, the Encrypt method is limited to a blob size of 2045 bytes and would have also failed attempting to encrypt that amount of data.

See Also

IPassportCrypt Interface | IPassportCrypt::Compress | IPassportCrypt::Decompress | IPassportCrypt::Encrypt