Results 1 to 6 of 6
  1. #1
    LStrike is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default [SOLVED]Cryptography Problem (BadPaddingException)

    Hello there,

    Ive got a prblem to encrypt and decrypt a String.
    I always get a BadPaddingException.

    Where is the failure I probably made?

    Thanks for your help.

    Java Code:
     
    public class KryptTest2 {
     
        byte[] salt = {
                  (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
                  (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
                  };
        int count=20;
     
     
    //....main and some stuff
     
    // Schlssel erzeugen (create key from password)
        private SecretKey makeKeyFromPassword(String passwort) {
             
            PBEKeySpec pbeKeySpec = new PBEKeySpec(passwort.toCharArray());
            try{
                SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
                SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
                return pbeKey;
            }
            catch(NoSuchAlgorithmException e){ 
                return null;}
            catch(InvalidKeySpecException e1){
                return null;
                        }
        }
     
        // Verschluesselung und Entschluesselung (encrypt and decrypt)
        private String verschlsseln(String nachricht, SecretKey secretKey) {
            try{
                PBEParameterSpec pbeParamSpec= new PBEParameterSpec(salt, count);
                Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
                pbeCipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParamSpec);
                byte[] ciphertext= pbeCipher.doFinal(nachricht.getBytes());
                return new String(ciphertext);  
            }
            catch (Exception e){
                return null;
            }
                
        }
        private String entschlsseln(String nachricht, SecretKey secretKey) {
            try{
                PBEParameterSpec pbeParamSpec= new PBEParameterSpec(salt, count);
                Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
                pbeCipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParamSpec);
                byte[] ciphertext= pbeCipher.doFinal(nachricht.getBytes());
              return new String(ciphertext);
        } 
        catch (Exception e){
            return null;
            }
        }
    Last edited by LStrike; 03-09-2010 at 07:54 PM. Reason: solved

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Well you have to dig into security API if you don't want to change your code
    but if you can do it i can recommend using sample code from this link :

    Source Code File Structure

    and maybe you need this:

    http://sce.uhcl.edu/yang/teaching/pr...leExample.java

    hope that will help

    cheers

  3. #3
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Make sure your key is 24 characters long.

  4. #4
    LStrike is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you FON and .
    I will take a closer look at my code.

    Make sure your key is 24 characters long.
    I will try this next.

  5. #5
    LStrike is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry, but this does not work properly.
    I think it has somesthing with the input string to do.

    If I put one more character to the input string I have a not correct decrypted message, if I remove it, I get a BadPaddingException.

    I show you what I mean:
    Look at the white-space between the full stop sign (.) and the word "Hallo" .
    1. Two white-space
    2. One white-spaces
    3. zero white-spaces

    1.
    message: Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein. Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.

    encrypted: kuS[OU`ezII`D:դû=q +Wdžb *:!|O̒P`i1*Y?wߙ ԸRkAJ?fY* *>mq8r5,5sTdH1!y@CjMbri

    decrypted: Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein. Hallo,վHsi
    eine TstnachriY4vn? sNachricht darf auch lnger sein.
    But:
    2.

    message: Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein. Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.

    -->
    javax.crypto.BadPaddingException: Given final block not properly padded
    3.
    message: Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.

    encrpted: kuS[OU`ezII`D:դû=q +Wdžb *
    :!|kzj('OdZ!d|b8j}ZkZ
    (04#8\穷qƼon5",

    decrypted: Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.
    I guess my problem remains to the input string.

    I think I have to use pbeCipher.update() and then pbeCipher.doFinal().
    But I don't know how to use the update function properly. I haven't found any example how to use it.

  6. #6
    LStrike is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    Good evening,

    just for completion. A member of the German Java-Forum found a solution.

    Thx for your help.

    Java Code:
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.spec.AlgorithmParameterSpec;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.KeySpec;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.PBEParameterSpec;
    
    import org.marre.sms.SmsPduUtil;
     
    public class Encrypt {
     
        final String PBEWithMD5AndDES = "PBEWithMD5AndDES";
        final int iterations = 8;
        final byte[] salt = {
            (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
            (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
        };
     
        public SecretKey makeKeyFromPassword(String passwort) throws NoSuchAlgorithmException, InvalidKeySpecException {
     
            final KeySpec pbeKeySpec = new PBEKeySpec(passwort.toCharArray());
            return SecretKeyFactory.getInstance(PBEWithMD5AndDES).generateSecret(pbeKeySpec);
        }
     
        private byte[] doCrypt(final byte[] cipher, final SecretKey secretKey, int mode) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
     
            final AlgorithmParameterSpec pbeParamSpec = new PBEParameterSpec(salt, iterations);
            final Cipher pbeCipher = Cipher.getInstance(PBEWithMD5AndDES);
     
            pbeCipher.init(mode, secretKey, pbeParamSpec);
            return pbeCipher.doFinal(cipher);
        }
     
        public byte[] encrypt(final String nachricht, final SecretKey secretKey) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
            
        	
        	return doCrypt(nachricht.getBytes(), secretKey, Cipher.ENCRYPT_MODE);
        }
     
        public String decrypt(final byte[] nachricht, final SecretKey secretKey) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
            return new String(doCrypt(nachricht, secretKey, Cipher.DECRYPT_MODE));
        }
     
    //    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
    // 
    //        //final String plainText = "Hallo Welt1234xyz578zyx";
    //        final String plainText = "Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.  Hallo, ich bin eine Testnachricht. Dies Nachricht darf auch lnger sein.";
    //        final String password = "secret";
    // 
    //        final Encrypt encrypt = new Encrypt();
    //        final SecretKey secretKey = encrypt.makeKeyFromPassword(password);
    // 
    //        final byte[] cipher = encrypt.encrypt(plainText, secretKey);
    //        final String decrypted = encrypt.decrypt(cipher, secretKey);
    // 
    //        System.out.println(plainText.equals(decrypted) ? decrypted : "failed!");
    //        
    //        System.out.println();
    //        System.out.println(new String(cipher));
    //        System.out.println();
    //        System.out.println(decrypted);
    //    }
    }

Similar Threads

  1. cryptography
    By swathi palla in forum AWT / Swing
    Replies: 2
    Last Post: 02-19-2009, 01:51 AM
  2. Legion of the Bouncy Castle Java Cryptography API 1.39
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-03-2008, 04:27 PM
  3. BadPaddingException
    By swmk in forum Advanced Java
    Replies: 0
    Last Post: 12-23-2007, 04:00 PM
  4. Legion of the Bouncy Castle Java Cryptography API 1.38
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-12-2007, 05:53 PM
  5. Replies: 0
    Last Post: 06-16-2007, 09:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •