title: Encrypt with Double Length 3DES Key in Java and produce 16 bytes array cipher
body: <p>I need to create a double length 3DES key in Java using a 16 bytes array key material, encrypt with it a message and get back 16 bytes length array. I am able to get it working with 24 bytes but not 16 bytes... </p>
  
  <p>Because DESede expects 24 byte[], the only way to produce a double length 3DES Key with a 16 bytes long key material, I was able to by using a DESede instance and then increasing its length by appending first 8 bytes at the end to get 24 bytes. Like so: </p>
  
  <pre><code>    byte[] keyMaterial = // Assume this one is 16 bytes. 
  
      byte[] newKeyBytes = ByteBuffer.wrap( new byte[ 24 ] )
              .put( Arrays.copyOfRange( keyMaterial, 0, 16 ) )
              .put( Arrays.copyOfRange( keyMaterial, 0, 8 ) )
              .array();
  
      SecretKeyFactory mySecretKeyFactory = SecretKeyFactory.getInstance( "DESede" );
      KeySpec myKeySpec = new DESedeKeySpec( newKeyBytes );
      SecretKey encryptionKey = mySecretKeyFactory.generateSecret( myKeySpec );
  </code></pre>
  
  <p>Now when I have my DESede key I can use it to encrypt the message the following way. Also, I need to use "ECB" mode: </p>
  
  <pre><code>    Cipher cipher = Cipher.getInstance( "DESede/ECB/PKCS5Padding" );
      cipher.init( Cipher.ENCRYPT_MODE, encryptionKey );
      byte[] returnValue = cipher.doFinal( dataToEncrypt )
  </code></pre>
  
  <p>But as a result I get back 24 byte array rather than 16 byte array and I guess this is because I am encrypting with 24 byte array. </p>
  
  <p>Is there a way to create a 3DES SecretKey 16 bytes long rather than 24 bytes, OR when encrypting with 24 byte long SecretKey, get back a 16 bytes long ciphered message as I need rather than 24 bytes long?</p>
  
  <p>Thank you!</p>