7 Replies Latest reply: Jan 3, 2012 4:33 AM by rukbat RSS

    javax.crypto.BadPaddingException in DES encrypt/decrypt algorithm

    843849
      I am using DES algorithm, the default provided by J2ME to encrypt and decrypt some text. My problem is that I can encrypt the text but when I decrypt I get javax.crypto.BadPaddingException. I used a sample code from this forum I suppose and modified it to some extend.

      Here's the output -
      Plain Text :debayandas
      Cipher Text :Æ–2�Ü°*Yð´4}�f¥
      Recovered Plain Text :javax.crypto.BadPaddingException
      And here's the J2ME code -


      Declaration part:
      private boolean midletPaused = false;
      
              private static String algorithm = "DES";
           private static byte[] secretKey = {(byte) 0x2b, (byte) 0x7e, (byte) 0x15, (byte) 0x16,
                                                        (byte) 0x28, (byte) 0xae, (byte) 0xd2, (byte) 0xa6 };
           private static String secretKeyAlgorithm = "DES";
           private static byte[] iv = "DES".getBytes();
      
           private static byte[] plainText = null;
           private Key key = null;
           private static Cipher cipher = null;
      
           private static int ciphertextLength = 512;
              private static byte[] cipherText = new byte[ciphertextLength];
      
              private static int decryptedtextLength = 1024;
              private static byte[] decryptedText = new byte[decryptedtextLength];
      commandAction:
      public void commandAction(Command command, Displayable displayable) {                                               
              if (displayable == form) {                                           
                  if (command == exitCommand) {                                         
                      exitMIDlet();                                           
                  } else if (command == okCommand) { 
                                               
                      plainText=textField.getString().getBytes();
                      encrypt();
                      decrypt();                                                         
                  }                                                  
              }                                                        
          }
      Encrypt:
      public void encrypt()
          {
                  try
                  {
                      key = new SecretKeySpec(secretKey,0,secretKey.length,secretKeyAlgorithm);
                cipher = Cipher.getInstance(algorithm);
                      cipher.init(Cipher.ENCRYPT_MODE, key);
                      cipher.doFinal(plainText, 0, plainText.length, cipherText, 0);
                System.out.println("Plain Text :"+new String(plainText));
                System.out.println("Cipher Text :"+new String(cipherText));
                  }
                  catch(Exception e)
                  {
                      System.out.println(""+e);
                  }
          }
      Decrypt:
      public void decrypt()
          {
              try
              {
      //            cipher = Cipher.getInstance(algorithm);
                  cipher.init(Cipher.DECRYPT_MODE,key);
                  cipher.doFinal(cipherText,0,cipherText.length,decryptedText,0);
                  System.out.println("Recovered Plain Text :"+new String(decryptedText));
      
              }
              catch(Exception e)
              {
                  System.out.println(""+e);
              }
          }
      Where am I going wrong?