title: AES encryption in VB6 and Java
body: <p>I am trying to encrypt data using AES in VB6, and match the same encryption in Java.  I took the basic VB6 code from Microsoft here: <a href="http://support.microsoft.com/kb/821762" rel="nofollow">http://support.microsoft.com/kb/821762</a></p>
  
  <p><strong>VB6:</strong>  </p>
  
  <pre><code>strProvider = "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)" &amp;     vbNullChar  
  lngHashType = CALG_MD5  
  lngAlgoType = CALG_AES_128 
  strPassword = "secretPassphrase"
  
  If CBool(CryptAcquireContext(g_lngCryptoContext, ByVal strTemp, _
           ByVal strProvider, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) Then           
      GetProvider = True
  else 
      GoTo CleanUp
  End If
  
  If Not CBool(CryptCreateHash(g_lngCryptoContext, lngHashType, ByVal 0&amp;, _
               ByVal 0&amp;, lngHashHnd)) Then
  
      MsgBox "Error: " &amp; CStr(GetLastError) &amp; " during CryptCreateHash!", _
             vbExclamation Or vbOKOnly, "Encryption Errors"
      GoTo CleanUp
  End If
  
  If Not CBool(CryptHashData(lngHashHnd, strPassword, Len(strPassword), ByVal 0&amp;)) Then
      MsgBox "Error: " &amp; CStr(GetLastError) &amp; " during CryptHashData!", _
             vbExclamation Or vbOKOnly, "Encryption Errors"
      GoTo CleanUp
  End If
  
  If Not CBool(CryptDeriveKey(g_lngCryptoContext, lngAlgoType, _
               lngHashHnd, ByVal CRYPT_NO_SALT, lngkey)) Then
  
      MsgBox "Error: " &amp; CStr(GetLastError) &amp; " during CryptDeriveKey!", _
             vbExclamation Or vbOKOnly, "Encryption Errors"
      GoTo CleanUp
  End If
  
  If lngHashHnd &lt;&gt; 0 Then
      lngRetCode = CryptDestroyHash(lngHashHnd)
  End If
  lngHashHnd = 0
  
  lngEncDataLength = Len(g_strInData)         
  lngEnctBuffLen = lngEncDataLength * 2
  strEncBuffer = String$(lngEnctBuffLen, vbNullChar)
  LSet strEncBuffer = g_strInData
  
  If Not CBool(CryptEncrypt(lngkey, ByVal 0&amp;, ByVal 1&amp;, ByVal 0&amp;, _
                            strEncBuffer, lngEncDataLength, lngEnctBuffLen)) Then
  
      MsgBox "Bytes required:" &amp; CStr(lngEnctBuffLen) &amp; vbCrLf &amp; vbCrLf &amp; _
             "Error: " &amp; CStr(GetLastError) &amp; " during CryptEncrypt!", _
             vbExclamation Or vbOKOnly, "Encryption Errors"
      GoTo CleanUp
  End If
  
  strOutData = Mid$(strEncBuffer, 1, lngEncDataLength)
  g_abytOutData = StringToByteArray(strOutData)
  </code></pre>
  
  <p>Mode, padding, iv, and block length are not explicitly set, but are CBC, PKCS5, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, and 128 respectively as are defaults and I have verified using the GetKeyParam function. </p>
  
  <p><strong>JAVA:</strong> </p>
  
  <pre><code>String password = "secretPassphrase";  
  String text = "text to encrypt";  
  MessageDigest md = MessageDigest.getInstance("MD5");  
  md.update(password.getBytes("UTF-8"), 0, password.length());  
  byte[] rawKey = md.digest();  
  byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  
  IvParameterSpec ivSpec = new IvParameterSpec(iv);  
  SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");  
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");  
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);  
  byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8"));  
  System.out.println(toHex(encrypted));
  </code></pre>
  
  <p>Even though I've set the encryption settings the same on both sides, I get different results when I output the encryptions as hex. I am wondering if the 2 different CSPs are compatible using AES? </p>
  
  <p>If I alter the code slightly on both ends to use RC2 instead of AES, the encryptions match.</p>
  
  <p>Any thoughts would be greatly appreciated. I am pulling my hair out on this one.  </p>