title: DES encrypt in C/C++ vs Java
body: <p>Just wondering why same plain text encrypted in Java and C doesn't generate same cipher text.
  For DES algo I noticed </p>
  
  <p>input <code>text = "text",  key = "test"</code></p>
  
  <p>C cipher text <code>len = 24</code>
  Java generates cipher text <code>len = 8</code></p>
  
  <p>For 256Bit AES I noticed similar difference<br>
  C cipher text <code>len = 32</code>
  Java generates cipher text <code>len = 16</code></p>
  
  <p>here is my "C" code </p>
  
  <pre class="lang-c prettyprint-override"><code>char* enc(const char* text, const char* keyStr)
  {
      EVP_CIPHER_CTX ctx;
      unsigned char key[32] = {0};
      unsigned char iv[16] = {0};
      unsigned char in[16] = {0};
      unsigned char out[32]; /* at least one block longer than in[] */
      memset(out, 0, 32);
      int outlen1, outlen2;
  
      memcpy(in, text, strlen(text));
      memcpy(key, keyStr, strlen(keyStr));
  
      EVP_EncryptInit(&amp;ctx, EVP_aes_256_cbc(), key, iv);    
      EVP_EncryptUpdate(&amp;ctx, out, &amp;outlen1, in, sizeof(in));
      EVP_EncryptFinal(&amp;ctx, out + outlen1, &amp;outlen2);
  
      char* ret  = (char*)malloc(outlen1 + outlen2+1);
      memset(ret, 0, outlen1 + outlen2+1);
      memcpy(ret, out, outlen1 + outlen2);
  
      EVP_CIPHER_CTX_cleanup(&amp;ctx);
  
      return ret;
  }
  </code></pre>
  
  <p>Here is the "Java" code </p>
  
  <pre class="lang-java prettyprint-override"><code>public static byte[] enc(byte[] input, byte[] keyStr){
          byte[] output = null;
  
          try {           
              byte[] newKey = getByteArrays(keyStr, 0, 32);
              SecretKey secretKey = new SecretKeySpec(newKey, "AES");
              Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
              //Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
              String ivStr = "";
              byte[] ivKey = getByteArrays(ivStr.getBytes("UTF-8"), 0, 16);
              IvParameterSpec ips = new IvParameterSpec(ivKey);
              cipher.init(Cipher.ENCRYPT_MODE, secretKey, ips, null);         
              output = cipher.doFinal(input);
  
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }       
          return output;
      }
  </code></pre>
  
  <p>I have list of entries encrypted using C , I want to decrypt those entries in Java.</p>
  
  <p><b> UPDATED </b> <br>
  following update helped to get same length , both in 'C' and 'JAVA'</p>
  
  <p>EVP_EncryptUpdate(&amp;ctx, out, &amp;outlen1, in, strlen(text));</p>
  
  <p>No Just wondering why ciphertext returned by "C" and "JAVA" are different for same text and key, I'm expecting them tobe same</p>