RE: CryptDecrypt with error 0x80090005



Thanks for you answer.

I can simplify the program according to your suggestion but we did a lot of
debbuging and:

1) "For examples, instead of using an external file (texto.txt), use a
hard-coded
buffer to simplify the problem analyze. "

The buffer is correct. We tryed alsoo with a hard coded buffer and the
result is same (fails).

2) "Crypt function (not only the GetLastError) to be sure which function
fails. "

The CryptEncrypt functions always fail. We verified with the debbuger.

3) "You must also verify the output buffer used in CryptEncrypt is larger
enough to hold the result (don't forget that there will be paddings)."

The buffer is larger enough to hold the result.

To clarify a little more. All the other crypt functions works fine. The same
sequence of functions call are done in C++ and works OK.

I wonder if the Key Handler is in some kind of state that makes the function
CrypDecrypt Fails. See in the code that the Key Handler for Encrypt is the
same of the Decrypt.

Thanks for your help!

Cheers!!!

"Mounir IDRASSI" wrote:

Hi,

Please try to simplify your code and narrow the use case if you want someone
to help you. In the posted code, the problem can come from different causes.


Also, check the boolean return value
of the Crypt function (not only the GetLastError) to be sure which function
fails. You must also verify the output buffer used in CryptEncrypt is larger
enough to hold the result (don't forget that there will be paddings).

Cheers,
--
Mounir IDRASSI
IDRIX
http://www.idrix.fr

to reach : mounir_idrix_fr (replace the underscores with the at and dot
characters respectively)


"Marcio Campos" wrote:

I wrongly posted this question in another group.

I have a simple C# program to encrypt and decrypt a small file using a
smartcard. I can encrypt the file but when I try to decrypit, the
CryptDecrypt call returns with error code 80090005. The troubled part of the
program is below

Can anybody help me ?

private void btnGerarChaveTDES_Click(object sender, EventArgs e)

{

IntPtr hProv = new IntPtr();

string pszContainer = null;

string pszProvider = "SafeSign Standard Cryptographic Service
Provider";

uint dwProvType = 1; // "1 - PROV_RSA_FULL";

uint dwFlags = 32; // "8 - CRYPT_NEWKEYSET";



int erro = 0;



CryptAcquireContext(ref hProv, pszContainer, pszProvider,
dwProvType, dwFlags);



erro = Marshal.GetLastWin32Error();



// Gera a chave TDES

// =============================================================

//IntPtr pnKey = new IntPtr();

IntPtr pnKey = new IntPtr();

uint Aldid = ((3 << 13) | (3 << 9) | (3));

uint dwFlags2 = 1; //1 - CRYPT EXPORTABLE



CryptGenKey(hProv, Aldid, dwFlags2, ref pnKey);



erro = Marshal.GetLastWin32Error();







// Importa a chava para o cartão

// =============================================================

IntPtr hExpKey = new IntPtr();

IntPtr nPubKey = new IntPtr();

uint dwFlags3 = 0;



CryptImportKey(hProv, pbData, pbData.Length, nPubKey, dwFlags3,
ref hExpKey);



erro = Marshal.GetLastWin32Error();





// Exporta a chave

// =============================================================

Int32 dwBlobType = 1; //"1 - SIMPLEBLOB";

Int32 dwFlags4 = 0;

Byte[] pbData2 = null;

Int32 pdwDataLen = 0;



CryptExportKey(pnKey, hExpKey, dwBlobType, dwFlags4, null, ref
pdwDataLen);



erro = Marshal.GetLastWin32Error();



pbData2 = new byte[pdwDataLen];



CryptExportKey(pnKey, hExpKey, dwBlobType, dwFlags4, pbData2,
ref pdwDataLen);



erro = Marshal.GetLastWin32Error();



GravarArquivo(pbData2, "c:\\BlobTDes.blb");





// Ler arquivo texto e encriptar

// =============================================================

IntPtr hHash = new IntPtr(0);

int Final = 1; // ultimo bloco ou não

uint dwFlags5 = 0;





//byte[] bufferArquivo = new byte[11027 + 130];

byte[] bufferArquivo = new byte[128];

byte[] arquivo = LerArquivo("c:\\texto.txt");

int arquivoLen = arquivo.Length;



arquivo.CopyTo(bufferArquivo, 0);



int dwBufLen = bufferArquivo.Length;





CryptEncrypt(pnKey, hHash, Final, dwFlags5, bufferArquivo, ref
arquivoLen, dwBufLen);



erro = Marshal.GetLastWin32Error();



//byte[] aux = new byte[arquivoLen];

//for (int i = 0; i < aux.Length; i++)

// aux[i] = bufferArquivo[i];



////GravarArquivo(bufferArquivo, "c:\\texto_cripto.txt");

//GravarArquivo(aux, "c:\\texto_cripto.txt");





//// Zera o vetor de incializaçao

//// =============================================================

//uint dwParam = 1; // 1 - KP_IV

//byte[] pbData3 = new byte[8];

//uint dwFlags6 = 0;



//for (int i = 0; i < 8; i++)

// pbData3[i] = 0;



//bool bln1 = CryptSetKeyParam(pnKey, dwParam, pbData3, dwFlags6);



//erro = Marshal.GetLastWin32Error();



bool bln2 = CryptDecrypt(pnKey, hHash, Final, dwFlags5, ref
bufferArquivo, ref arquivoLen);



erro = Marshal.GetLastWin32Error();





}






.



Relevant Pages

  • RE: CryptDecrypt with error 0x80090005
    ... IntPtr hProv = new IntPtr; ... erro = Marshal.GetLastWin32Error; ... CryptGenKey(hProv, Aldid, dwFlags2, ref pnKey); ... CryptImportKey(hProv, pbData, pbData.Length, nPubKey, dwFlags3, ...
    (microsoft.public.platformsdk.security)
  • CryptDecrypt with error 0x80090005
    ... IntPtr hProv = new IntPtr; ... erro = Marshal.GetLastWin32Error; ... CryptGenKey(hProv, Aldid, dwFlags2, ref pnKey); ... CryptImportKey(hProv, pbData, pbData.Length, nPubKey, dwFlags3, ...
    (microsoft.public.platformsdk.security)
  • Re: Inline assembly and buffers
    ... mov bx, "the rest of the ref to buffer" ... are you saying I can't use ye olde BIOS calls from Windows ...
    (microsoft.public.vc.language)
  • Re: Inline assembly and buffers
    ... mov bx, "the rest of the ref to buffer" ... protected mode and is going to fail miserably on any version of Windows ...
    (microsoft.public.vc.language)
  • Re: Marshalling Question
    ... getting the char type to work. ... lpReserved, ref UInt32 lpType, ref charlpData, ref UInt32 lpcbData); ... out, passing in the first element of buffer - buffer, passing in the ... entire buffer, etc.). ...
    (microsoft.public.dotnet.framework.interop)