Using CryptoAPI in C++ in an MFC unicode application

I read Dan Rollins excellent article "Easy String Encryption Using CryptoAPI in C++":

http://www.experts-exchange.com/articles/1193/Easy-String-Encryption-Using-CryptoAPI-in-C.html

I got the class to compile and run no problem. But in my application it encrypts the string, but will not decrypt. The CryptoAPI  function CryptDecrypt fails with error NTE_BAD_LEN. I tried increasing the output buffer size to a much larger value, but then it simply returns an incorrect decryption of the input string. I have even tried just passing simple char and LPCSTR values to it, and it is no-go.

I am thinking this might be an issue with unicode, but I admit that I am a bit lost. Any help would be appreciated.
TheIronDukeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
This class is written for ASCII strings. In order to compile it for UNICODE you need to replace all:
LPSTR -> LPTSTR
LPCSTR -> LPCTSTR
char -> TCHAR
strlen -> _tcslen
strcpy -> _tcscpy
"literal" -> _T("literal")

In addition to this, when there is a parameter that takes the number of bytes (i.e. here: m_fOK= CryptEncrypt( m_hKey, 0, TRUE, 0, (BYTE*)pTmp, &nDataLen, nDataLen );) the number must be multiplied to sizeof(TCHAR), like this:
CString CCrypt::EncryptStrToHex( LPCTSTR szText, LPCTSTR pszKey/*= 0*/, LPCTSTR pszSalt/*= 0*/ )
{
    m_fOK= TRUE;
    CString sRet= "";
    len =  _tcslen( szText );
    DWORD nDataLen= len * sizeof(TCHAR);
    if ( pszSalt || pszKey || (0 == m_hKey) ) {
        m_fOK= SetKey( (LPCTSTR)pszKey, pszSalt );	
    }
    if ( m_fOK ) {
        TCHAR* pTmp= new TCHAR[len+1] ;
        _tcsncpy_s( pTmp, len+1, szText, len+1 );
        m_fOK= CryptEncrypt( m_hKey, 0, TRUE, 0, (BYTE*)pTmp, &nDataLen, nDataLen );
        if (m_fOK ) {
            sRet= EncodeToHex( (BYTE*)pTmp, nDataLen );
        }
        delete pTmp;
    }
    return( sRet );
}

Open in new window

Basically, you need to rewrite the whole thing.
If it is a task too difficult for you you can use the conversion. E.g this ASCII example:
   char szSrc[]="Secret Password";

   //------------ use default rawKey
   CString sEncrypted= crypt.EncryptStrToHex( szSrc );  
   CString sDecrypted= crypt.DecryptStrFromHex( sEncrypted );
   MessageBox( sEncrypted,sDecrypted );

Open in new window

Will be written as UNICODE like this:
   USES_CONVERSION;
   TCHAR szSrc[]=_T("Secret Password");

   //------------ use default rawKey
   CString sEncrypted= A2T(crypt.EncryptStrToHex( T2A(szSrc) ));  
   CString sDecrypted= A2T(crypt.DecryptStrFromHex( T2A(sEncrypted) ));
   MessageBox( sEncrypted,sDecrypted );

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.