Key derivation in c++ and VB.NET

From: Howard Postley (HowardPostley_at_discussions.microsoft.com)
Date: 02/12/05

  • Next message: Joe Kaplan \(MVP - ADSI\): "Re: ASP.NET web app, Win2003, & Active Directory"
    Date: Fri, 11 Feb 2005 19:37:02 -0800
    
    

    I am trying to encrypt data in c++ and decrypt in VB.NET. I find that the
    System.Cryptography classes don't align exactly with the CryptoAPI
    functionality that is easily accessed from c++.

    I believe that the problem I'm having is deriving the same key on the .NET
    side as on the c++ side.

    The c++ code (which encrypts and decrypts fine) looks like:

    // Get handle to user default provider.
    if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
    {
      // Create hash object.
      if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
      {
        // Hash password string.
        if (CryptHashData(hHash, pbKey, cbKey, 0))
        {
          // Create block cipher session key based on hash of the password.
          if (CryptDeriveKey(hProv, CALG_3DES, hHash, CRYPT_EXPORTABLE, &hKey))
          {
            // Allocate memory.
            pbBuffer = (LPBYTE)malloc(*pcbCipherText);
            if (pbBuffer != NULL)
            {
              memcpy(pbBuffer, pbPlainText, cbPlainText);
              cbBuffer = *pcbCipherText;
              *pcbCipherText = cbPlainText;

              // Encrypt data
              if (CryptEncrypt(hKey, 0, TRUE, 0, pbBuffer, pcbCipherText,
    cbBuffer))
              {

    The VP.NET code (where the problem is):

    Dim ivNull() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
    Dim csp As New TripleDESCryptoServiceProvider ' CSP context
    Dim bHash As PasswordDeriveBytes
    Dim outData As New MemoryStream
    Dim Cipher As CryptoStream
    Dim transform As ICryptoTransform

    bHash = New PasswordDeriveBytes(ToString(Key), ivNull)
    csp.Key = bHash.CryptDeriveKey("TripleDES", "SHA1", 0, ivNull)
    csp.IV = ivNull

    Cipher = New CryptoStream(outData, _
                                              csp.CreateDecryptor(), _
                                              CryptoStreamMode.Write)
    Cipher.Write(inData, 0, inData.Length)
    Cipher.Close()

    The VB code throws an exception on the last (close) statement. I believe
    that the reason is that the derived key for decryption doesn't match the one
    that was used for encryption.

    Any suggestions?

    Thanks,

    </hjp>


  • Next message: Joe Kaplan \(MVP - ADSI\): "Re: ASP.NET web app, Win2003, & Active Directory"

    Relevant Pages

    • Ugh, at my whits end and in desperate need of help.
      ... I can create and encrypt a message ... with the public key without a problem, but when I try to decrypt the message ... Dim privatecert As New X509Certificate2 ...
      (microsoft.public.security)
    • Re: Need help decrypting
      ... Just a brief look: in your encrypt function you ... > I'm wondering if somebody can help me figure out how to decrypt data. ... > Private Sub Encryptpwd() ... > Dim ms As New MemoryStream ...
      (microsoft.public.dotnet.security)
    • RSACryptoServiceProvider problem with bad Key
      ... I encrypt using the public key, but when I decrypt using the private key I ... Dim privatecert As New X509Certificates.X509Certificate2 ...
      (microsoft.public.dotnet.security)
    • RE: Cryptography, Encryption, Decryption Help
      ... I changed the Encrypt and Decrypt functions in the Crypto.vb class to take in strings instead of Byte arrays. ... Dim EncryptString As String ...
      (microsoft.public.pocketpc.developer)
    • Problem with CryptoAPI
      ... I have read the documentation about CryptoAPI and i test the samples but ... I would like create a key, encrypt and decrypt a file with this key. ... Provider v1.0"); ...
      (microsoft.public.windowsce.embedded)