Problem with CryptoAPI

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Seb (fiix_at_mime.univ-paris8.fr)
Date: 08/24/04


Date: Tue, 24 Aug 2004 17:29:57 +0200

Hello,

I have read the documentation about CryptoAPI and i test the samples but
it don't work.
I would like create a key, encrypt and decrypt a file with this key.
But it's impossible to me to create a key, my source code is following:

I call Init() and after GenerateRandomKey() but CryptGetUserKey return
failed 0x8009000d
Where is the pb ?

DWORD CMsCrypto::GenerateRandomKey()
{
     if (!CryptGenKey(m_hProv, CALG_RC2, CRYPT_EXPORTABLE, &m_hKey))
     {
                CString szError;
                szError.Format(_T("Error 0x%x during CryptGenKey!\n"), GetLastError ());
                AfxMessageBox(szError);
                
                return E_FAIL;
     }
        
        // Get the handle to the key exchange public key.
        if (!CryptGetUserKey(m_hProv, AT_KEYEXCHANGE, &m_hXchgKey))
        {
                CString szError;
                szError.Format(_T("Error 0x%x during CryptGetUserKey!\n"),
GetLastError());
                AfxMessageBox(szError);
                
                return E_FAIL;
        }
        
        return NO_ERROR;
}

void CMsCrypto::Init()
{
        HCRYPTPROV hProv;
        DWORD dwFlags = CRYPT_VERIFYCONTEXT;
        
        static TCHAR *szRealProvider=TEXT("Microsoft Enhanced Cryptographic
Provider v1.0");
        static TCHAR *szWeakProvider=TEXT("Microsoft Base Cryptographic
Provider v1.0");

        if (CryptAcquireContext(&m_hProv, NULL, szRealProvider, PROV_RSA_FULL,
dwFlags)
                || (GetLastError() == NTE_BAD_KEYSET
                && CryptAcquireContext(&m_hProv, NULL, szRealProvider,
PROV_RSA_FULL,CRYPT_NEWKEYSET)
                ))
        {
                m_lpszWhichCSP = szRealProvider;
        } else {
                m_lpszWhichCSP = szWeakProvider;
        }
}



Relevant Pages

  • Key derivation in c++ and VB.NET
    ... I am trying to encrypt data in c++ and decrypt in VB.NET. ... System.Cryptography classes don't align exactly with the CryptoAPI ... Dim bHash As PasswordDeriveBytes ...
    (microsoft.public.dotnet.security)
  • Re: Regarding encryption and Decryption
    ... CAPICOM (which leverages most of useful underlying CryptoAPI capability) ... If you encrypt arbitrary data, you will be using a secret *symmetric* key to ... access that encryption key so your users can decrypt your encrypted content. ... I will distribute the public key. ...
    (microsoft.public.security)
  • Re: interop: PaddingMode.None TripleDES with CrptoAPI
    ... > i'm trying to do TripleDES with no padding in the CryptoAPI ... > if i take the 1st block and encrypt it with the CryptoAPI. ... > but the problem is when i try to decrypt with the CryptoAPI. ...
    (microsoft.public.dotnet.security)
  • Re: interop: PaddingMode.None TripleDES with CrptoAPI
    ... > i'm trying to do TripleDES with no padding in the CryptoAPI ... > if i take the 1st block and encrypt it with the CryptoAPI. ... > but the problem is when i try to decrypt with the CryptoAPI. ...
    (microsoft.public.platformsdk.security)
  • Re: Cannot decrypt data on XP encrypted on NT
    ... CryptAcquireContext (default provider changed from MS_ENH_PROV to ... > I have an application that runs on Win NT system for encrypting/decrypting ... After upgrading the OS to Win XP I try to decrypt the password ... > HCRYPTKEY hXchgKey = NULL; ...
    (microsoft.public.platformsdk.security)