Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3046

::cryptDecrypt Fails with Unicode

$
0
0
Hey Gurus,
I'm frustrated by an issue I have when trying to decrypt some data.
I have two projects, both decrypting the exactly the same data. One project uses unicode and the other doesn't. The project not using unicode, decrypts the data without a problem. With the project that does use unicode ::cryptDecrypt fails with "bad data". I understand the difference between unicode and non unicode builds. When I look at the data being sent to ::cryptDecrypt it is identical on both builds! This really has me stumped. Here is some of my code from the unicode version. I'm using the class CCryptography in my software.

Code:

int result = true;

        CByteArray arData;

        int count;

        BYTE *data;

        CString passwordDecoded;

        // This is for the T2A macro.
        USES_CONVERSION;

        // Decode the Base64 encoding of the password.
        count = Base64DecodeGetRequiredLength( password.GetLength() );

        data = ( BYTE * ) malloc( count );

        Base64Decode( T2A(password), password.GetLength(), data, &count );

        for ( int i = 0; i < count; i++ ) arData.Add( data[ i ] );

        arData.Add( 0 );

        passwordDecoded = arData.GetData();

        free( data );

        arData.RemoveAll();

        if ( m_crypto.DeriveKey( passwordDecoded ) )
        {
                // Decode the Base64 encoding of the registration key.
                count = Base64DecodeGetRequiredLength( registrationKey.GetLength() );

                data = ( BYTE * ) malloc( count );

                Base64Decode( T2A( registrationKey), registrationKey.GetLength(), data, &count );

                int i;

                for ( i = 0; i < count; i++ )
                {
                        arData.Add( data[ i ] );
                }

                free( data );

                //        Try and deserialize the data.
                if ( m_crypto.Decrypt( arData, m_arStrings ) == true ) // This returns false!!

m_crypto.Decrypt function:
Code:

bool CCrypto::Decrypt(const CByteArray& arData, CObject& serializable)
{
        //        Return failure if we don't have a context or key.
        if(m_hCryptProv == NULL || m_hKey == NULL)
                return false;

        //        Return failure if the object is not serializable.
        if(serializable.IsSerializable() == FALSE)
                return false;

        //        Decrypt the contents of the array to the memory file.
        if(InternalDecrypt(arData) == false)
                return false;

        //        Create a reading archive from the memory file.
        CArchive ar(&m_file, CArchive::load);

        //        Read the data from the memory file.
        serializable.Serialize(ar);
       
        //        Close the archive.
        ar.Close();

        //        And we're done.
        return true;
}

The InternalDecrypt function:
Code:

bool CCrypto::InternalDecrypt(const CByteArray& arSource)
{
        //        Trash the file.
        m_file.SetLength(0);

        //        Write the contents of the byte array to the memory file.
        m_file.Write(arSource.GetData(), static_cast<UINT>(arSource.GetCount()));
        m_file.Flush();

        //        Acquire direct access to the memory file buffer.
        BYTE* pData = m_file.Detach();

        //        We need a DWORD to tell decrpyt how much data we're encrypting.
        DWORD dwDataLength = static_cast<DWORD>(arSource.GetCount());
        DWORD dwOldDataLength = dwDataLength;

        //        Now decrypt the data.
        if(!::CryptDecrypt(m_hKey, NULL, TRUE, 0, pData, &dwDataLength))
        {
                //        Free the memory we release from the memory file.
                delete [] pData;

                return false;
        }

::CryptDecrypt(m_hKey, NULL, TRUE, 0, pData, &dwDataLength) fails with Bad Data on a unicode build, but not on a non unicode build.

Could someone please give me some direction as to how to fix this?

Many thanks,

Steve Q.

Viewing all articles
Browse latest Browse all 3046

Trending Articles