title: 3DES , Java code result produces different result from 3des online tool and oracle database script
body: <p>when applying 3des encryption using java , java code generate wrong result compared to 3des online tool and oracle DB script</p>
  
  <p>3des encryption , Java</p>
  
  <pre><code>  SET SERVEROUTPUT ON;
  
      declare
      l_mod_ofb pls_integer;  
       l_mod_ecb pls_integer;
        v_key raw(32);   
        v_iv  raw(16);
        v_test_in raw(16);   v_ciphertext raw(16);
  
      begin
        l_mod_ecb := dbms_crypto.ENCRYPT_3DES_2KEY + dbms_crypto.CHAIN_CBC  + DBMS_CRYPTO.PAD_NONE ;
        v_key      :=   hextoraw  ('1FB3F48A6D51832CE91C1C734554086D');
        v_iv       :=   hextoraw  ('041234FFFFFFFFFF');                
        v_test_in  :=   hextoraw  ('0000211111111110');                
  
        v_ciphertext := dbms_crypto.encrypt(src =&gt; v_test_in,
                                            TYP =&gt; l_mod_ecb,
                                            key =&gt; v_key,
                                            iv =&gt; v_iv);
  
        dbms_output.put_line(' Ciphertext  : '||rawtohex(v_ciphertext));
  
      end;
  </code></pre>
  
  <p>-------------------Java Code</p>
  
  <pre><code>import javax.crypto.BadPaddingException;
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.SecretKey;
  import javax.crypto.SecretKeyFactory;
  import javax.crypto.spec.DESedeKeySpec;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  import javax.xml.bind.DatatypeConverter;
  import org.apache.commons.codec.DecoderException;
  import org.apache.commons.codec.binary.Hex;
  
  public class TripleDes {
  
  private static String SHARED_KEY = "1FB3F48A6D51832CE91C1C734554086D";
  private static String ivString = "041234FFFFFFFFFF";
  private static String valueStr = "0000211111111110";
  static final String HEXES = "041217DB6AEFF7CF";
  
  public static void main(String[] args) throws DecoderException, Exception {
  
      TripleDes tr = new TripleDes();
      byte[] plain = Hex.decodeHex(valueStr.toCharArray());
      byte[] codedtext = tr.encrypt(plain);
      System.out.println("Encrypted Result = " + getHex(codedtext));
  }
  
  
  
  public byte[] encrypt(byte[] plainTextBytes) throws Exception {
      TripleDes tr = new TripleDes();
      byte[] keyValue = getKey(Hex.decodeHex(SHARED_KEY.toCharArray()));
      System.out.println(Hex.encodeHex(keyValue));
      IvParameterSpec iv = new IvParameterSpec(Hex.decodeHex(ivString.toCharArray()));
      //DESede
      final SecretKey key = new SecretKeySpec(keyValue, "DESede");
      final Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
      cipher.init(Cipher.ENCRYPT_MODE, key, iv);
      final byte[] cipherText = cipher.doFinal(plainTextBytes);
      return cipherText;
  }
  
  public static byte[] getKey(byte[] keyBytes) {
      byte[] key;
      if (keyBytes.length == 16) {
          key = new byte[24];
          System.arraycopy(keyBytes, 0, key, 0, 16);
          System.arraycopy(keyBytes, 0, key, 16, 8);
      } else {
          key = keyBytes;
      }
  
      return key;
  }
  
  public static String getHex(byte[] raw) {
      if (raw == null) {
          return null;
      }
      final StringBuilder hex = new StringBuilder(2 * raw.length);
      for (final byte b : raw) {
          hex.append(HEXES.charAt((b &amp; 0xF0) &gt;&gt; 4))
                  .append(HEXES.charAt((b &amp; 0x0F)));
      }
      return hex.toString();
  }
  }
  </code></pre>
  
  <p>--------------- online tool
  use the following paramters</p>
  
  <pre><code>Input type: Text
  Input text:(hex)0000211111111110
  Function: 3DES  
  Mode: CBS   
  Key: (hex)1FB3F48A6D51832CE91C1C734554086D1FB3F48A6D51832C
  Init. vector:   041234FFFFFFFFFF
  </code></pre>
  
  <ul>
  <li>Expected result is 63CA0F38548CC8B5</li>
  <li>The result from online tool and oracle script is equal to expected result</li>
  <li>Java code result : D2FE0F26716FF6F7</li>
  </ul>