title: What is the java equivalent of 3DES encryption in C#
body: <p>I have a snippet of C# code for encryption :</p>
  
  <pre><code> private static byte[] sharedkey = {0x01, 0x02, 0x03, 0x05, 0x03, 0x0B, 0x0D, 0x11, 0x12, 0x11, 0x0D, 0x0B, 0x07, 0x07, 0x04, 0x08,
  0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11};
  private static byte[] sharedvector = {0x01, 0x07, 0x01, 0x05, 0x07, 0x0B, 0x0C, 0x11};
  
  
  
   public static String Encrypt(String val) {
      TripleDESCryptoServiceProvider tdes = newTripleDESCryptoServiceProvider(); 
      byte[] toEncrypt = Encoding.UTF8.GetBytes(val);
      MemoryStream ms = new MemoryStream();
      CryptoStream cs = new CryptoStream(ms, tdes.CreateEncryptor(sharedkey, sharedvector ), CryptoStreamMode.Write);
      cs.Write(toEncrypt, 0, toEncrypt.Length); cs.FlushFinalBlock();
      return Convert.ToBase64String(ms.ToArray());
  
   }
  </code></pre>
  
  <p>I tried to make a java version of this, but it does not work. this is my java version:</p>
  
  <pre><code>public String doTripleDESEncryption(String rawData) {
  
      byte[] cipher;
      int length;
      int inputLength = sharedvector.length;
      int iterations = inputLength / 8;
      if (inputLength % 8 != 0) {
          iterations++;
      }
  
      cipher = new byte[8 * iterations];
  
      for (int i = 0; i &lt; iterations; i++) {
          DESedeEngine tdes = new DESedeEngine();
          tdes.init(true, new DESedeParameters(sharedkey));
          byte[] data = new byte[8];
  
          if (i == (iterations - 1)) {
              length = sharedvector.length - i * 8;
          } else {
              length = 8;
          }
  
          System.arraycopy(sharedvector, i * 8, data, 0, length);
          tdes.processBlock(data, 0, cipher, i * 8);
      }
      return new String(org.bouncycastle.util.encoders.Hex.encode(cipher));
  }
  </code></pre>
  
  <p>But it does not work.I feel i am doing the conversion in the wrong way, as i am new to 3DES.</p>
  
  <p>In the C# snippet, CryptoStream takes in the sharedKey and sharedVector to create CryptoStream, but in Java I can't seem to find the equivalent.</p>