RE: cryptdecrypt failed with an error 0x80090020 when using with an ke



You need to ENCRYPT data with the PUBLIC key and DECRYPT it with the PRIVATE
key - the opposite what your code is doing.
The reason CryptEncrypt doesn't fail first is because hKey is actually a
handle to the key PAIR, ie. it happily encrypts your data with the PUBLIC key
part of they key pair.

Laszlo Elteto
SafeNet, Inc.

"imran" wrote:

hai,

iam imran, when ever iam using the function it is failing with an error
0x80090020,i.e an internal error occurred.plz help me to rectify this error.


#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#include <string.h>

//
// Preprocesssor directives
//

#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define MAX 128

int main(int argv,char* argc[])
{
//
// Local Variables
//
HCERTSTORE phStore = NULL;
HCRYPTPROV phProv = NULL; // handle
to csp
LPTSTR pszProvider; // Provider
name
BOOL bValue; //
HCRYPTKEY hKey = NULL; // handle
of private key
HCRYPTKEY phKey = NULL; // handle
of public key
PCERT_PUBLIC_KEY_INFO pInfo; // pointer
to public key info structure
char cBuff[MAX]; // array of
characters
DWORD pdwDataLen = 5; // data
length
DWORD pdwDataLen1 = 0;
char vBuff[MAX];
int bCmp; // reurn
value of cmp function
DWORD dwIndex = 0;
DWORD pdwProvType; // provider
Type
DWORD pcbProvName; //
ProviderName
PCCERT_CONTEXT pCertContext;
DWORD pdwKeySpec;
BOOL pfCallerFreeProv =FALSE;
for (;;)
{
//
// Obtaining the user data
//
printf("ENTER THE MESSAGE\n");
scanf("%s",cBuff);
strcpy(vBuff,cBuff);


while(bValue = CryptEnumProviders(dwIndex,
NULL,
0,
&pdwProvType,
NULL,//pszProvider,
&pcbProvName))
{

if (!(pszProvider = (LPTSTR)LocalAlloc(LMEM_ZEROINIT,
pcbProvName)))
{
printf("ERROR - LocalAlloc failed!");
}//if

if (CryptEnumProviders( dwIndex++,
NULL,
0,
&pdwProvType,
pszProvider,
&pcbProvName )) // pcbProvName --
size of pszNam))
{
printf (" %4.0d %s\n",pdwProvType,
pszProvider);
}//if

LocalFree(pszProvider);

}//while

if (TRUE!=bValue)
{
printf("Failed to know the provider name %d\n",GetLastError());

} //if

//
// Acquire the handle of ikey
//
bValue = CryptAcquireContext(&phProv,
// handle to the CSP
TEXT("n-yogesh.pfx"),
// container name
TEXT("Rainbow iKey 1000 RSA Cryptographic Service Provider") ,
// use the default provider
1,
// provider type
0);
if (TRUE!=bValue)
{
printf("Failed to Acquire Context1 %0x\n",GetLastError());
break;
} //if

//
// Opening the system store
//

//
// Fuction to Obtaine the Private key Handle
//

bValue = CryptGetUserKey(phProv, // handle to csp
AT_KEYEXCHANGE, // identifies private key
&hKey); // handle to private key
if (TRUE!=bValue)
{
printf("Failed to get Handle of Private Key
%d\n",GetLastError());
break;
} //if

//
// obtaining the Handle of Public Key
//
bValue = CryptImportPublicKeyInfo(phProv, // handle
to csp
MY_ENCODING_TYPE, // encoding
style
&(pCertContext->pCertInfo->SubjectPublicKeyInfo), // pointer
to public key info
&phKey); // pointer
to public key handle
if (TRUE!=bValue)
{
printf("Failed to get Handle of Public Key
%d\n",GetLastError());
break;
} //if

//
// Encrypting data with Private Key Handle
//
bValue = CryptEncrypt(hKey, // handle of private key
0, // to donot obtain Hash
TRUE, // only sentance to encrypt
0, // reserved for future use
(BYTE *)cBuff, // pointer to buff to b encrypted
&pdwDataLen,
sizeof(cBuff)); //size of data
if (TRUE!=bValue)
{
printf("Failed to Encrypt %d\n",GetLastError());
break;
} // if

//
//
//
printf("%s\n",cBuff);
pdwDataLen1=strlen(cBuff);
printf("%d\n",pdwDataLen1);

//
// Decrypting data using public key
//
bValue = CryptDecrypt(phKey, // handle of public key
0, // to donot obtain Hash
TRUE, // only sentance to decrypt
0, // reserved for future use
(BYTE *)cBuff, // pointer to buff to b decrypted
&pdwDataLen1);
if (TRUE!=bValue)
{
printf("Failed to decrypt %d\n",GetLastError());
break;
} //if


//
// Comparing the Strings
//
bCmp = strcmp(vBuff,cBuff);
if (0!=bCmp)
{
printf("Code is wRONG %d\n",GetLastError());
return 1;
}//if

printf("IKEY IS VERIFIED\n");

//
//final break
//
break;
} //for
bValue = CryptReleaseContext(phProv,
0);
if (TRUE!=bValue)
{
printf("Failed to release %d",GetLastError());

} //if

return 0;
} //main

.



Relevant Pages