title: 3DES Decrypt in java a token encrypted in C#
body: <p>I've searched a lot before asking, but none of the ideas I found work in my problem so here's my problem :</p>
  
  <ol>
  <li>In C# the code (which I cannot change because it is from another application) for encryption is detailed after.</li>
  <li>I have to decrypt the encrypted token in Java but nothing works so far, can anyone help ?</li>
  </ol>
  
  <p>For 1. C# code :</p>
  
  <pre><code>    static public string Encrypt3DES(string toEncrypt, string SecKey, string IV){
      byte[] keyArray;
      try
      {
          byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
  
      MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
          keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecKey));
          hashmd5.Clear();      
  
          TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
  
          tdes.Key = keyArray;      
          tdes.Mode = CipherMode.CBC;
      tdes.Padding = PaddingMode.PKCS7;
          tdes.IV = UTF8Encoding.UTF8.GetBytes(IV);
  
          ICryptoTransform cTransform = tdes.CreateEncryptor();
  
          //transform the specified region of bytes array to resultArray
          byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
      tdes.Clear();
  
      //Return the encrypted data into unreadable string format
          return Convert.ToBase64String(resultArray, 0, resultArray.Length);
      }
      catch (Exception e) { return string.Empty; }
      }
  </code></pre>
  
  <p>For 2 Java code that does not work :</p>
  
  <pre><code>    public class TripleDesTest {
  
  private KeySpec keySpec;
  private SecretKey key;
  private IvParameterSpec iv;
  
  public TripleDesTest() {
      String keyString = "THE_KEY";
      String ivString = "THE_IV";
  
      try {
          final MessageDigest md = MessageDigest.getInstance("md5");
          final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("UTF-8")));            
          final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
          for (int j = 0, k = 16; j &lt; 8;) {
              keyBytes[k++] = keyBytes[j++];
          }
  
          keySpec = new DESedeKeySpec(keyBytes);
  
          key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
  
          iv = new IvParameterSpec(ivString.getBytes("UTF-8"));
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  
  
  public String decrypt(String value) {
  
      try {
          Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "SunJCE");
          dcipher.init(Cipher.DECRYPT_MODE, key, iv);
  
          if (value == null)
              return null;
  
          // Decode base64 to get bytes
          byte[] dec = Base64.decodeBase64(value.getBytes("UTF-8"));
  
          // Decrypt
          byte[] utf8 = dcipher.doFinal(dec);
  
          // Decode using UTF-8
          return new String(utf8, "UTF-8");
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  
  }
      }
  </code></pre>