title: How to encrypt and decrypt a file with 3DES (Triple DES) in Java
body: <p>This is what I have, but I can not decipher the file.
  I do not find a logic that allows me to decipher.
  I do not know if it is encrypting correctly, because I can not decipher it.</p>
  
  <pre><code> import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.security.GeneralSecurityException;
   import java.security.MessageDigest;
   import javax.crypto.Cipher;
   import javax.crypto.SecretKey;
   import javax.crypto.SecretKeyFactory;
   import javax.crypto.spec.DESedeKeySpec;
   import javax.crypto.spec.IvParameterSpec;
  
   public class TestEncryptor {
  
       public static void main(String... args) {
           try {
               String KEY_STRING = "asdasdasd";
               byte[] key = getEnKey(KEY_STRING);
               String pFilePath = "D:\\fileTest.png";
               String pFilePathEncryp = "D:\\fileTestEncryp.png";
               byte[] archivoDecrypt = encryptFile(pFilePath, key);
  
               try (FileOutputStream fos = new FileOutputStream(pFilePathEncryp)) {
                   fos.write(archivoDecrypt);
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
  
       }
  
       public static byte[] encryptFile(String pFilePath, byte[] pKey) throws GeneralSecurityException, IOException {
           File file = new File(pFilePath);
           long length = file.length();
           InputStream is = new FileInputStream(file);
  
           // You cannot create an array using a long type.
           // It needs to be an int type.
           // Before converting to an int type, check
           // to ensure that file is not larger than Integer.MAX_VALUE.
           if (length &gt; Integer.MAX_VALUE) {
               // File is too large
           }
  
           // Create the byte array to hold the data
           byte[] bytes = new byte[(int) length];
  
           // Read in the bytes
           int offset = 0;
           int numRead = 0;
           while (offset &lt; bytes.length
                   &amp;&amp; (numRead = is.read(bytes, offset, bytes.length - offset)) &gt;= 0) {
               offset += numRead;
           }
  
           // Close the input stream and return bytes
           is.close();
  
           // Ensure all the bytes have been read in
           if (offset &lt; bytes.length) {
  
               throw new IOException("Could not completely read file " + file.getName());
           }
  
           SecretKeyFactory lDESedeKeyFactory = SecretKeyFactory.getInstance("DESede");
           SecretKey kA = lDESedeKeyFactory.generateSecret(new DESedeKeySpec(pKey));
  
           IvParameterSpec lIVSpec = new IvParameterSpec(new byte[8]);
           Cipher desedeCBCCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
  
           desedeCBCCipher.init(Cipher.ENCRYPT_MODE, kA, lIVSpec);
   //        desedeCBCCipher.init(Cipher.DECRYPT_MODE, kA, lIVSpec);
           byte[] encrypted = desedeCBCCipher.doFinal(bytes);
  
           return encrypted;
       }
  
       private static byte[] getEnKey(String spKey) {
           byte[] desKey = null;
           try {
               byte[] desKey1 = md5(spKey);
               desKey = new byte[24];
               int i = 0;
               while (i &lt; desKey1.length &amp;&amp; i &lt; 24) {
                   desKey[i] = desKey1[i];
                   i++;
               }
               if (i &lt; 24) {
                   desKey[i] = 0;
                   i++;
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
  
           return desKey;
       }
  
       private static byte[] md5(String strSrc) {
           byte[] returnByte = null;
           try {
               MessageDigest md5 = MessageDigest.getInstance("MD5");
               returnByte = md5.digest(strSrc.getBytes("GBK"));
           } catch (Exception e) {
               e.printStackTrace();
           }
           return returnByte;
       }
   }
  </code></pre>