title: Why the same result both DES Encrypt and DESede Encrypt?
body: <p>I'm try to be compatible Encrypt/Decrypt both C# and Java.</p>
  
  <p>As I know the default mode is 'ecb/pkcs5' in Java, and 'cbc/pkcs7' in C#.</p>
  
  <p>So I match these things.</p>
  
  <p><strong>1st question is that PKCS7 and PKCS5 are compatible each other??</strong>, </p>
  
  <p>there is no PKCS7 in Java so I use PKCS5. but I can get same encrypted data [even the padding-way is different ,pkcs7/pkcs5,] Is it possible? or these are compatible?</p>
  
  <p><strong>2nd question is that Why I get same result even though the mode, way are all different?</strong></p>
  
  <p>I compare 'DES-ECB / DES-CBC / TripleDES-ECB' these things. and C# is working well, results are all different.</p>
  
  <p>Input > HELLO Output > (ECB)/dZf3gUY150=  (CBC) V17s5QLzynM= (Triple)sWGS0GMe1jE</p>
  
  <p>but I get same reulst in Java ..</p>
  
  <p>Input > HELLO Output > (ECB)/dZf3gUY150= (CBC)/dZf3gUY150= (Triple)/dZf3gUY150=</p>
  
  <p>When debugging the flow is right.</p>
  
  <p>Here is my code.</p>
  
  <p><strong>C#</strong></p>
  
  <pre><code>public static string Encrypt_DES(string originalString, byte[] key, string mode)
      {
          DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  
          if (mode.Equals("ECB"))
              cryptoProvider.Mode = CipherMode.ECB;
          else if (mode.Equals("CBC"))
          {
              cryptoProvider.Mode = CipherMode.CBC;
              cryptoProvider.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
          }
  
          cryptoProvider.Padding = PaddingMode.PKCS7;
          MemoryStream memoryStream = new MemoryStream();
          CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);
          StreamWriter writer = new StreamWriter(cryptoStream);
          writer.Write(originalString);
          writer.Flush();
          cryptoStream.FlushFinalBlock();
          writer.Flush();
          return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
      }
  
  public static string Encrypt_TripleDES(string source, string key)
      {
          TripleDESCryptoServiceProvider desCryptoProvider = new TripleDESCryptoServiceProvider();
          MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider();
  
          byte[] byteHash;
          byte[] byteBuff;
  
          byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
          desCryptoProvider.Key = byteHash;
          desCryptoProvider.Mode = CipherMode.ECB; //CBC, CFB
          desCryptoProvider.Padding = PaddingMode.PKCS7;
          byteBuff = Encoding.UTF8.GetBytes(source);
  
          string encoded = Convert.ToBase64String(desCryptoProvider.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
          return encoded;
      }
  </code></pre>
  
  <p><strong>Java(Android)</strong></p>
  
  <pre><code>public String Encrypt(String str, String desKey, String mode) {
      try {
          KeySpec keySpec = null;
          SecretKey key = null;
          Cipher ecipher = null;
          if (desKey.length() == 8) {
              keySpec = new DESKeySpec(desKey.getBytes("UTF8"));
              key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
              if(mode.equals(ECB)){
                  ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                  ecipher.init(Cipher.ENCRYPT_MODE, key);
              }else if (mode.equals(CBC)){
                  ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
                  AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
                  ecipher.init(Cipher.ENCRYPT_MODE, key,ivSpec);
              }
          } else if (desKey.length() == 24) {
              keySpec = new DESedeKeySpec(desKey.getBytes("UTF8"));
              key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
              ecipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
              ecipher.init(Cipher.ENCRYPT_MODE, key);
          }
  
          byte[] data = str.getBytes("UTF-8");
          byte[] crypt = ecipher.doFinal(data);
  
          return Base64.encodeToString(crypt, 0);
      } catch (Exception ex) {
          Log.d("ZVM", ex.getMessage());
      }
      return null;
  }
  </code></pre>
  
  <ul>
  <li>As I understand 'IV' is for CBC, When making password, it is mixed with IV(not the key but like key). Is it right? </li>
  </ul>
  
  <p>Thanks.</p>