title: TripleDES algorithm in C#
body: <p>I'm currently working on TripleDES encryption in C# for which I've received the code sample from JAVA. </p>
  
  <p>I have created an encryption function in C# with below code sample :</p>
  
  <p>Inputs : 
  key/ekay = "15ce89cd1a2a838f4f6d49d60438251915ce89cd1a2a838f"</p>
  
  <p>text/data = "0000000000000000"</p>
  
  <pre><code>public static string encryptionMethod(string Text, string key)
  {
            string encryptedText = string.Empty;
            try
              {                
                  MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();                          
                  byte[] md5Bytes = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(key));
                  md5Hash.Clear();
                  byte[] clearBytes = Encoding.UTF8.GetBytes(Text);  
                  TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();  
                  des.KeySize = 128;                          
                  des.Mode = CipherMode.CBC;    
                  des.Padding = PaddingMode.None;  
                  des.Key = md5Bytes;   //Passing key in byte array
                  //des.BlockSize = 64;
                  byte[] ivBytes = new byte[8] {0, 0, 0, 0, 0, 0, 0, 0 };
                  des.IV = ivBytes;                      
                  ICryptoTransform ct = des.CreateEncryptor();   //Interface with some result
                  byte[] resultArray = ct.TransformFinalBlock(clearBytes, 0, clearBytes.Length);               
                  encryptedText = ByteArrayToHexString(resultArray);                               
              }
              catch (Exception exception)
              {
                  return "";
              }
          return encryptedText;
  
  }
  
  public static string ByteArrayToHexString(byte[] ba)
          {
              StringBuilder hex = new StringBuilder(ba.Length * 2);
              foreach (byte b in ba)
                  hex.AppendFormat("{0:x2}", b);
              return hex.ToString();
          }
  
  </code></pre>
  
  <p>but after comparing the C# output with JAVA output, I got the different results.</p>
  
  <p>JAVA code</p>
  
  <pre><code>
  public  static String encrypt(String data, String ekey) {
          String encrypteddata = null;
     try{            
          String key = ekey;
          byte[] encryptKey = ISOUtil.hex2byte(key);        
          DESedeKeySpec spec = new DESedeKeySpec(encryptKey);
          SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
          SecretKey theKey = keyFactory.generateSecret(spec);        
          Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");       
          IvParameterSpec IvParameters = new IvParameterSpec( new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });        
          cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters);  
          String plain = data;       
          byte[] plaintext = ISOUtil.hex2byte(plain);
          byte[] encrypted = cipher.doFinal(plaintext);        
           encrypteddata= ISOUtil.byte2hex(encrypted);             
      }
      catch(Exception e){        
      }
          return encrypteddata;      
      }   
  
  </code></pre>
  
  <p>output :</p>
  
  <p>C# : eca27a1e639900f3298a5090cc34dd29</p>
  
  <p>JAVA : c0a946402dd20f5e</p>
  
  <hr>
  
  <p>Any help would be appreciated?</p>
  
  <p>Thanks.</p>