By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,800 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,800 IT Pros & Developers. It's quick & easy.

Java DES encryption

P: 3
I'm trying to do a simple encryption/decryption program. The code below works fine if I never convert the encrypted text into a string. My problem occurs when i convert it to a string and then I convert it back into bytes. I want to eventually create a GUI, so I figured that if there is a encrypted text it will be assigned to a variable from a text field via a string. Please help if you can. Sorry for any confusion.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.security.InvalidKeyException;
  3. import java.security.NoSuchAlgorithmException;
  4.  
  5. import javax.crypto.BadPaddingException;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.IllegalBlockSizeException;
  8. import javax.crypto.KeyGenerator;
  9. import javax.crypto.NoSuchPaddingException;
  10. import javax.crypto.SecretKey;
  11.  
  12.  
  13. public class Main {
  14.  
  15.     public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  16.         KeyGenerator keyGen = KeyGenerator.getInstance("DES");
  17.         SecretKey myDesKey = keyGen.generateKey();
  18.  
  19.         Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  20.         cipher.init(Cipher.ENCRYPT_MODE, myDesKey);
  21.         String message = "TEST";
  22.         byte[] input = message.getBytes();
  23.         System.out.println(input);
  24.  
  25.  
  26.         // Encrypt
  27.         byte[] messageEncrypted = cipher.doFinal(input);
  28.  
  29.         // Decrypt
  30.         String text = messageEncrypted.toString();
  31.         System.out.println(text);
  32.         System.out.println(messageEncrypted);
  33.         byte[] textMessageEncrypted;
  34.         textMessageEncrypted = text.getBytes();
  35.         System.out.println(textMessageEncrypted);
  36.         cipher.init(Cipher.DECRYPT_MODE, myDesKey);
  37.         byte[] messageDecrypted = cipher.doFinal(textMessageEncrypted);
  38.         System.out.println("Text Decrypted: " + new String(messageDecrypted));
  39.     }
  40.  
  41. }
  42.  
  43.  
Here is the error:
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
  2.     at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
  3.     at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
  4.     at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA13*..)[B@4bdb699b
  5.  
Feb 25 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.