Java: Encrypting and Decrypting using DES
<p>I'm trying to create a simple encryption/decryption program, but i'm having problems when decrypting. The way the program works, I get string input from the user, then encrypt using DES, convert to Base64 and give the user the converted secret key. However, when I get the secret key from the user and I try to decrypt, I get either error:</p>

<p>java.security.InvalidKeyException: No installed provider supports this key: (null) </p>

<p>or</p>

<p>javax.crypto.BadPaddingException</p>

<p>I don't know if the fault is at the time of encryption or if it's the decryption. Here's the respective snippets of code:</p>

<pre><code>   import javax.xml.bind.DatatypeConverter;
   public static boolean encrypt(byte[] text){

    Boolean yorn = false;
    try{
        myDesKey = KeyGenerator.getInstance("DES").generateKey();

        //myDeskKey = myDesKey.toString();
        Cipher desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);

//I felt it would be better seeing the secret key as "woatDnBJLAg="  instead of  "com.sun.crypto.provider.DESKey@18765"

        if (myDesKey != null) {
           stringKey = DatatypeConverter.printBase64Binary(myDesKey.getEncoded());

            System.out.println("actual secret_key:" + myDesKey);

            byte[] encodedKey = DatatypeConverter.parseBase64Binary(stringKey);

            myDesKey = new SecretKeySpec(encodedKey, 0, encodedKey.length,
                    "DES");
            System.out.println("after encode &amp; decode secret_key:"
                    + DatatypeConverter.printBase64Binary(myDesKey.getEncoded()));
            }
        textEncrypted = desCipher.doFinal(text);

    yorn = true;
    JTextArea textArea = new JTextArea(2,50);
    textArea.setText("Your encryption key is:  " +  stringKey + " . Ensure you store it in a safe place" );// + DatatypeConverter.printBase64Binary(myDesKey.getEncoded()));
    textArea.setEditable(false);
    JOptionPane.showMessageDialog(null, new JScrollPane(textArea), "RESULT", JOptionPane.INFORMATION_MESSAGE);


    }catch(Exception e)
    {
        System.out.println("There has been an error encrypting the file");
        yorn = false;
    }
        return yorn;
</code></pre>

<p>Decryption</p>

<pre><code>  public static String decrypt(byte[] cipherText, SecretKey key,String key1)
  {
    String plainText = "";
    try{
        SecretKey myDesKey = key;
        if(key == null){

        JOptionPane.showMessageDialog(null, "We were unable to find your decryption key. Please enter your decryption key below: ");
        JTextArea textBox = new JTextArea(1,15);
        JOptionPane.showMessageDialog(null, new JScrollPane(textBox),"Enter your decryption key ",JOptionPane.PLAIN_MESSAGE);
        //myDesKey = textBox.toSecretKey;
        }

    Cipher desCipher;
    desCipher = Cipher.getInstance("DES");
    desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
    byte[] textDecrypted = desCipher.doFinal(cipherText);
    plainText = new String(textDecrypted);
    JOptionPane.showMessageDialog(null, plainText, "DECRYPTED MESSAGE", 0);

    }catch(Exception e)
    {
        System.out.println("There has been an error decrypting the file");
        System.out.println(e);
    }return plainText;
  }
}
</code></pre>

<p>I know that I'm probably getting the errors because i've combined so many jumbled bits of code from all over stack and I seem to have lost the plot, but any help will be greatly appreciated.
Thanks!!</p>