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



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

  • RE: PGP scripting...
    ... cryptosystems, ... In these systems divulging your private key compromises the public ... Here is a quick over view of the public key encryption routines (the ...
    (SecProg)
  • Re: Private & Public Key storage location
    ... with that you complete the 'certificate' to have both public and private key ... To view the complete cert, you access the cert mmc, ... its end & send only the public key to the CA along with the other websites ... The CA never know the private key of the website. ...
    (microsoft.public.inetserver.iis.security)
  • CryptImportKey NTE_BAD_KEY error.
    ... of the private key is just fine but when I try to import the public key I ... // This Asymetric key set will be used to create the Autherization Code. ... delete psBuffer; ...
    (microsoft.public.security)
  • Re: Private & Public Key storage location
    ... client use the public key to ... corresponds to this certiticate' when you view the cert. ... it will has the private key as well. ... installed for your website, it will be sent to all the clients who connect ...
    (microsoft.public.inetserver.iis.security)
  • CryptImportKey NTE_BAD_KEY error.
    ... of the private key is just fine but when I try to import the public key I ... // This Asymetric key set will be used to create the Autherization Code. ... delete psBuffer; ...
    (microsoft.public.platformsdk.security)