Win32 Application CryptoAPI

From: Darren Bennett (darren_at_work.com)
Date: 11/25/04

  • Next message: James McFarland: "RE: FormsAuthentication Roles Problem"
    Date: Wed, 24 Nov 2004 18:35:01 -0800
    
    

    Hi There,

    I have been scanning the newsgroups for a solution to my problem and have
    found that a few others are also experiencing the same problem but none of
    the solutions provided to them seem to work for me.

    I have a native Win32 application (written in C++) that needs to encrypt
    some data, pass that data to a web service (written in C#) for decryption. I
    just can't seem to decrypt the data correctly.

    To demonstrate the problem, I have written a native Win32 (C++) application
    to encrypt some data and write the encrypted data out to disk:

    HCRYPTPROV hProv;
    HCRYPTHASH hHash;
    HCRYPTKEY hKey;
    HANDLE hFile;
    DWORD dwNumBytes, dwBytesWritten;
    BYTE byPassword[] = {65, 66, 67, 68, 69, 70}; // equals "ABCDEF"
    BYTE byData[] = {10, 20, 30, 40, 50, 60, 70, 80, 0, 0, 0, 0, 0, 0, 0,
    0};
      
    // Create the Cryptograhic Provider Object
    if (CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
    CRYPT_VERIFYCONTEXT)) {
      // Create a hash object
      if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
        // Hash in the password
        if (CryptHashData(hHash, byPassword, sizeof(byPassword), 0)) {
          // Derive a session key from the hash object.
          if (CryptDeriveKey(hProv, CALG_RC2, hHash, 0, &hKey)) {
            dwNumBytes = 8;
            if (CryptEncrypt(hKey, NULL, TRUE, 0, byData, &dwNumBytes,
    sizeof(byData))) {
              // Create a file to store the encrypted data
              if ((hFile = CreateFile(_T("C:\\Encrypted.dat"), GENERIC_WRITE, 0,
    NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
                // Write the encrypted data out to file
                WriteFile(hFile, &byData, dwNumBytes, &dwBytesWritten, NULL);
                // Close the file handdle
                CloseHandle(hFile);
              }
            }
            // Destroy the session key
            CryptDestroyKey(hKey);
          }
        }
        // Destroy the hash object
        CryptDestroyHash(hHash);
      }
      // Release the cryptographic provider context
      CryptReleaseContext(hProv, 0);
    }

    I have also written a .NET C# application to read the data from file and try
    and decrypt the data:

    int iNumBytes;
    FileStream fsData;
    byte[] byData = new byte[16];
    byte[] bySalt = {0,0,0,0,0,0,0,0};
    byte[] byInitVect = {0,0,0,0,0,0,0,0};
                            
    // Open the file containing the encrypted data, read inb the data and close
    the file
    fsData = File.Open("C:\\Encrypted.dat", FileMode.Open);
    iNumBytes = fsData.Read(byData, 0, byData.Length);
    fsData.Close();
                            
    // Derive a session key from the password using an MD5 hash
    PasswordDeriveBytes SessionKey = new PasswordDeriveBytes("ABCDEF", bySalt,
    "MD5", 1);
                            
    // Set up an RC2 cryptographic object
    RC2CryptoServiceProvider Rc2 = new RC2CryptoServiceProvider();
    Rc2.Mode = CipherMode.CBC;
    Rc2.KeySize = 40;
    Rc2.EffectiveKeySize = 40;
    Rc2.BlockSize = 64;
    Rc2.Key = SessionKey.GetBytes(5);

    // Decrypt the data
    ICryptoTransform decryptor = Rc2.CreateDecryptor(SessionKey.GetBytes(8),
    byInitVect);
    byte[] myOutputBytes = new byte[decryptor.OutputBlockSize];
    iNumBytes = decryptor.TransformBlock(byData, 0, decryptor.InputBlockSize,
    myOutputBytes, 0);

    The problem is that the data just does not decrypt. I really don't care what
    type of hashing or encryption algorithms are used so long as they are
    supported on Win98/Me and NT4.0SP6/2000/XP/2003.

    Any help in solving this problem would be greatly appreciated.

    Thanks,

    -Darren-


  • Next message: James McFarland: "RE: FormsAuthentication Roles Problem"

    Relevant Pages

    • RE: NTE_BAD_DATA
      ... They are NOT used DIRECTLY to encrypt / decrypt data; ... you should generate a RANDOM SESSION KEY and select a SYMMETRIC ENCRYPTION ... // imported from a BLOB read in from the source file or having ...
      (microsoft.public.platformsdk.security)
    • Re: Back Doors
      ... >> Design into the system a master key. ... Encrypt that with public key. ... Decrypt random symmetric key with private key. ...
      (sci.crypt)
    • Re: CAPI and RC4: can not decrypt when Final parameter is set to F
      ... to store ASYMMETRIC key pairs - never symmetric keys like RC4, ... Now when you need to encrypt at one place and decrypt at the other normally ... Get a HCRYPTPROV handle to a key container with CryptAcquireContext ...
      (microsoft.public.platformsdk.security)
    • Re: RSA - Public vs. Private Keys
      ... This is a common pattern for license software ... your client will send a unique machine hash to the ... will let us decrypt with a Public Key (or simply not ... |> RSA is intended to encrypt messages with public keys only. ...
      (microsoft.public.dotnet.security)
    • Re: .NET Crypto Classes Interoperability with Win32 Crypto APIs
      ... when i encrypt a string using .NET classes and try to ... > decrypt it using Win32 APIs, ... > UnicodeEncoding(); ...
      (microsoft.public.dotnet.security)