0

Im developing android program which in i want to encrypt an string and then decrypt it as well. but i recieve this exception: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

I searched a lot but i couldn't find the answer.

this is my encrypt class which contains two methods Encrypt() and Decrypt():

    public class EncryptionClass {

public static SecretKey mainKey=null;



public static SecretKey GenerateKey() throws NoSuchAlgorithmException
{

    KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
    SecretKey myDesKey = keygenerator.generateKey();
    return myDesKey;

    }


public static String Encrypt(String plainText) {

    String encryptedText = "";

    try {
        mainKey=GenerateKey();
            Cipher desCipher;

            // Create the cipher 
            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            // Initialize the cipher for encryption
            desCipher.init(Cipher.ENCRYPT_MODE,mainKey);

            //sensitive information
            byte[] plainTextAsBytes =plainText.getBytes();
            Log.d("Text [Byte Format] : " , plainTextAsBytes.toString());
            Log.d("Text : " ,new String(plainTextAsBytes));

           // Encrypt the text
            byte[] cipherText = desCipher.doFinal(plainTextAsBytes);

            Log.d("Text Encryted : " ,cipherText.toString());

            encryptedText=cipherText.toString();

    }catch(NoSuchAlgorithmException e){
        Log.d("NoSuchAlgorithmException :", e.toString());
    }catch(NoSuchPaddingException e){
        Log.d("NoSuchPaddingException  :", e.toString());
    }catch(InvalidKeyException e){
        Log.d("InvalidKeyException:", e.toString());
    }catch(IllegalBlockSizeException e){
        Log.d("IllegalBlockSizeException:", e.toString());
    }catch(BadPaddingException e){
        Log.d("BadPaddingException:", e.toString());
    } 
    finally{

    }

    return encryptedText;

}

public static String Decrypt(String cipherText) {

    String decryptedText = "";

    try {

            Log.d("Decrypt MAin Key:",mainKey.getEncoded().toString());
            Cipher desCipher;

            // Create the cipher 
            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            // Initialize the cipher for encryption
            desCipher.init(Cipher.DECRYPT_MODE,mainKey);


           // Encrypt the text

            byte[] cipherTextBytes=cipherText.getBytes();
            byte[]  plainText= desCipher.doFinal(cipherTextBytes);

            Log.d("Text Decryted : " ,plainText.toString());

            decryptedText=plainText.toString();

    }catch(NoSuchAlgorithmException e){
        Log.d("NoSuchAlgorithmException :", e.toString());
    }catch(NoSuchPaddingException e){
        Log.d("NoSuchPaddingException  :", e.toString());
    }catch(InvalidKeyException e){
        Log.d("InvalidKeyException:", e.toString());
    }catch(IllegalBlockSizeException e){
        Log.d("IllegalBlockSizeException:", e.toString());
    }catch(BadPaddingException e){
        Log.d("BadPaddingException:", e.toString());
    } 
    finally{

    }


    return decryptedText;
}

}

and this is my main activity where i call methods:

            public void onClick(View arg0) {

            String ct=EncryptionClass.Encrypt("the text to encrypt");
            Log.d("Cipher Text", ct);

            String pt=EncryptionClass.Decrypt(ct);
            Log.d("Plain Text", pt);

        }

any help really appreciate. Best Regards,

2
  • You searched a lot? I find that hard to believe. A simple Google search for exactly that exception returns hundreds of results for me. Commented Feb 11, 2014 at 20:31
  • yes that's right.there are alot of posts but i couldn't solve my problem reading them. Commented Feb 11, 2014 at 21:04

2 Answers 2

2

cipherText.toString() will not work as you think as the byte array can contain arbitrary bytes. Some of them will be lost or mangled in the conversion and the resulting ciphertext cannot be decrypted.

Keep the byte[] around and use that to feed the Decrypt, or convert the bytes to Base64.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot for your answer.How should i convert bytes to Base64?
cipherText.toString() won't work at all, regardless of the content of the array.
0
  1. Ciphertext is binary, and String isn't a container for binary data. Pass the ciphertext around as byte[], not String.
  2. cipherText.toString() is just byte[].toString(), and that doesn't give you a String containing its contents anyway, just a class name and object hashCode. But when you attend to (1) the problem will disappear anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.