title: DES implementation in Java. Can not get the plaintext
body: <p>I am working in a simple example for DES implementation using Java built-in libraries. This is my code:</p>
  
  <pre><code>import it.sauronsoftware.base64.Base64;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  import javax.crypto.BadPaddingException;
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.SecretKeySpec;
  
  
  public class DES {
  
      public static void main(String [] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
      {
          String msg="This is a secret message";
          byte [] msgBytes=msg.getBytes();        
          byte [] keyBytes  = {(byte)0xFE, (byte)0xDC, (byte)0xBA, (byte)0x98, (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10};
          SecretKeySpec myDesKey = new SecretKeySpec(keyBytes, "DES");
  
          //to encrypt a message
          String cipher=encryptMsg(msgBytes, myDesKey);
  
          //to decrypt a message
          String plain = decryptMsg(cipher.getBytes(), myDesKey);
  
          System.out.println("Original Message: "+ msg);
          System.out.println("Encrypted Message: "+ cipher);
          System.out.println("Decrypted Message: "+ plain);
  
      } //end main
  
      //encryption function
      public static String encryptMsg(byte [] msgBytes, SecretKey myDesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
      {
          Cipher desCipher;
          // Create the cipher 
          desCipher = Cipher.getInstance("DES/ECB/NoPadding");
          desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
          byte[] textEncrypted = desCipher.doFinal(msgBytes);
  
          // converts to base64 for easier display.
          byte[] base64Cipher = Base64.encode(textEncrypted);
          return new String(base64Cipher);
      } //end encryptMsg
  
      public static String decryptMsg(byte [] cipherBytes, SecretKey myDesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
      {
          Cipher desCipher; 
          desCipher = Cipher.getInstance("DES/ECB/NoPadding");
          desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
          byte[] textDecrypted=desCipher.doFinal(cipherBytes);
  
          // converts to base64 for easier display.
          byte[] base64Plain = Base64.encode(textDecrypted);
          return new String(base64Plain);
      } //end decryptMsg
  } //end class
  </code></pre>
  
  <p>The output I am getting is:</p>
  
  <pre><code>Original Message: This is a secret message
  Encrypted Message: hNFgTAoz2TN9f6FcLdbjnEBe5DrsU4sm
  Decrypted Message: RFdk1JK0gG0vv2zndHueS9rRe0Oux44ACGObsRHQ+4E=
  </code></pre>
  
  <p>I need my key to be a fixed (not random) value. That's why I defined it as array of bytes in the beginning. </p>
  
  <p>My problem is that my decryption function does not return the original message. This means there is a problem in the code and probably the encryption is not proper. I am suspicious about the coding issue as the encryption / decryption is quite straight forward. Can you point the problems in my code?</p>
  
  <p><strong>EDIT:</strong>
  In the decryption, I changed the <code>encode</code> to <code>decode</code> as suggested by one of the comments. That does not work. I get:</p>
  
  <pre><code>Exception in thread "main" java.lang.RuntimeException: Unexpected I/O error
      at it.sauronsoftware.base64.Base64.decode(Unknown Source)
      at DES.decryptMsg(DES.java:55)
      at DES.main(DES.java:25)
  Caused by: java.io.IOException: Bad base64 stream
      at it.sauronsoftware.base64.Base64InputStream.acquire(Unknown Source)
      at it.sauronsoftware.base64.Base64InputStream.read(Unknown Source)
      at java.io.InputStream.read(Unknown Source)
      at java.io.InputStream.read(Unknown Source)
      at it.sauronsoftware.base64.Base64.copy(Unknown Source)
      at it.sauronsoftware.base64.Base64.decode(Unknown Source)
      ... 3 more
  </code></pre>