How to decrypt dotNet from ATL.

How to decrypt dotNet from ATL.

Secure Home | Search | About
 Microsoft Applications Security    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content add this group's latest topics to your Google content
Subject Author Date
How to decrypt dotNet from ATL. François 12-05-2005
Posted by François on December 5, 2005, 9:58 am
If you were  Registered and logged in, you could reply and use other advanced thread options
I have ciphered text from dotNet application.

I need to decrypt it from an ATL app.

The C# decrypting process I have is :

string p = b64 ciphered text 16 bytes len
string initVector = my b64 value value 8 bytes len
string strKey = my b64 value 16 bytes len

Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes);
dec.IV = Convert.FromBase64String(initVector);
byte[] plainText = dec.Decrypt(Convert.FromBase64String(p),
Convert.FromBase64String(strKey));
ClearText = Encoding.ASCII.GetString(plainText);




What I have tried in C++ ATL with no luck: 8009005 BAD DATA at key.decrypt.
(all other return OK)

CreatePrivateExponentOneKey2 and ImportPlainSessionBlob come from
http://support.microsoft.com/kb/q228786/ (Except I have stripped
CryptAcquireContext by CCryptProv::Initialize)




int passwordLen = 1000;
BYTE password[1000];
Base64Decode(strPasswordB64,strPasswordB64.GetLength(),password,
&passwordLen);

BYTE cipher[1000];
int cipherLen = 1000;
Base64Decode(strTexteB64,strTexteB64.GetLength(),cipher, &cipherLen);

CString iv = "my b64 value";
BYTE IV[8];
int lenIV = 8;
Base64Decode(iv,iv.GetLength(),IV, &lenIV);

CCryptProv prov;
HRESULT hr = prov.Initialize(PROV_RSA_FULL,NULL,MS_STRONG_PROV);
FAILED(hr)

HCRYPTKEY hPrivateKey;
HCRYPTPROV hProv = prov.GetHandle();
DWORD dwKeySpec = AT_KEYEXCHANGE;
BOOL bRet;
bRet = CreatePrivateExponentOneKey2(dwKeySpec,hProv,&hPrivateKey);
ASSERT(bRet)

HCRYPTKEY hSessionKey;
bRet =
ImportPlainSessionBlob(hProv,hPrivateKey,CALG_3DES,password,passwordLen,&hSe
ssionKey);
ASSERT(bRet);

CCryptKey key(hSessionKey);

hr = key.SetIV(IV);
FAILED(hr)

BYTE Final[1000];
DWORD lenF = 1000;
hr = key.Decrypt(cipher, cipherLen,Final,&lenF);
FAILED(hr)



Posted by François on December 5, 2005, 10:36 am
If you were  Registered and logged in, you could reply and use other advanced thread options
Got it.

It has to be CALG_3DES_112 , not CALG_3DES.


It was stupid. I should have checked the lentgh of the key fist.



> I have ciphered text from dotNet application.
>
> I need to decrypt it from an ATL app.
>
> The C# decrypting process I have is :
>
> string p = b64 ciphered text 16 bytes len
> string initVector = my b64 value value 8 bytes len
> string strKey = my b64 value 16 bytes len
>
> Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes);
> dec.IV = Convert.FromBase64String(initVector);
> byte[] plainText = dec.Decrypt(Convert.FromBase64String(p),
> Convert.FromBase64String(strKey));
> ClearText = Encoding.ASCII.GetString(plainText);
>
>
>
>
> What I have tried in C++ ATL with no luck: 8009005 BAD DATA at
key.decrypt.
> (all other return OK)
>
> CreatePrivateExponentOneKey2 and ImportPlainSessionBlob come from
> CryptAcquireContext by CCryptProv::Initialize)
>
>
>
>
> int passwordLen = 1000;
> BYTE password[1000];
> Base64Decode(strPasswordB64,strPasswordB64.GetLength(),password,
> &passwordLen);
>
> BYTE cipher[1000];
> int cipherLen = 1000;
> Base64Decode(strTexteB64,strTexteB64.GetLength(),cipher, &cipherLen);
>
> CString iv = "my b64 value";
> BYTE IV[8];
> int lenIV = 8;
> Base64Decode(iv,iv.GetLength(),IV, &lenIV);
>
> CCryptProv prov;
> HRESULT hr = prov.Initialize(PROV_RSA_FULL,NULL,MS_STRONG_PROV);
> FAILED(hr)
>
> HCRYPTKEY hPrivateKey;
> HCRYPTPROV hProv = prov.GetHandle();
> DWORD dwKeySpec = AT_KEYEXCHANGE;
> BOOL bRet;
> bRet = CreatePrivateExponentOneKey2(dwKeySpec,hProv,&hPrivateKey);
> ASSERT(bRet)
>
> HCRYPTKEY hSessionKey;
> bRet =
>
ImportPlainSessionBlob(hProv,hPrivateKey,CALG_3DES,password,passwordLen,&hSe
> ssionKey);
> ASSERT(bRet);
>
> CCryptKey key(hSessionKey);
>
> hr = key.SetIV(IV);
> FAILED(hr)
>
> BYTE Final[1000];
> DWORD lenF = 1000;
> hr = key.Decrypt(cipher, cipherLen,Final,&lenF);
> FAILED(hr)
>
>



Similar ThreadsPosted
Cannot decrypt about 5% of encrypted files March 29, 2007, 10:22 am
Decrypt windows files January 17, 2008, 5:14 am
Re: CAPICOM.EnvelopedData decrypt: where to put certificate? June 22, 2005, 3:24 pm
How can I decrypt a Microsoft Word document for which I have the key? March 19, 2006, 3:13 am
RSA frustrations - encrypt with private, decrypt with public - possible? October 24, 2005, 9:03 pm
Lose ability to decrypt EFS files after reboot February 27, 2007, 8:22 am
Unable to decrypt folders on windows vista September 1, 2008, 1:03 pm

The site map in XML format XML site map

Contact Us | Privacy Policy