Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:40:47+00:00 2026-05-15T05:40:47+00:00

I am writing an application which encrypts and decrypts the user notes based on

  • 0

I am writing an application which encrypts and decrypts the user notes based on the user set password. i used the following algorithms for encryption/decryption
1. PBEWithSHA256And256BitAES-CBC-BC
2. PBEWithMD5And128BitAES-CBC-OpenSSL

    e_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES-CBC-BC);
    d_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES-CBC-BC);
    e_Cipher.init()
    d_Cipher.init()

encryption is working well, but when trying to decrypt it gives

Exception – Illegal Block size

after encryption i am converting the cipherText to HEX and storing it in a sqlite database. i am retrieving correct values from the sqlite database during decyption but when calling d_Cipher.dofinal() it throws the Exception.

I thought i missed to specify the padding and tried to check what are the other available cipher algorithms but i was unable to found.

so request you to please give the some knowledge on what are the cipher algorithms and padding that are supported by Android? if the algorithm which i used can be used for padding, how should i specify the padding mechanism?
I am pretty new to Encryption so tried a couple of algorithms which are available in BouncyCastle.java but unsuccessful.

As requested here is the code

public class CryptoHelper {
private static final String TAG = "CryptoHelper";
//private static final String PBEWithSHA256And256BitAES = "PBEWithSHA256And256BitAES-CBC-BC";
//private static final String PBEWithSHA256And256BitAES = "PBEWithMD5And128BitAES-CBC-OpenSSL";
private static final String PBEWithSHA256And256BitAES = "PBEWithMD5And128BitAES-CBC-OpenSSLPBEWITHSHA1AND3-KEYTRIPLEDES-CB";
private static final String randomAlgorithm = "SHA1PRNG";
public static final int SALT_LENGTH = 8;
public static final int SALT_GEN_ITER_COUNT = 20;
private final static String HEX = "0123456789ABCDEF";

private Cipher e_Cipher; 
private Cipher d_Cipher;
private SecretKey secretKey;
private byte salt[];

public CryptoHelper(String password) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException {
    char[] cPassword = password.toCharArray();
    PBEKeySpec pbeKeySpec = new PBEKeySpec(cPassword);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, SALT_GEN_ITER_COUNT);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithSHA256And256BitAES);
    secretKey = keyFac.generateSecret(pbeKeySpec);

    SecureRandom saltGen = SecureRandom.getInstance(randomAlgorithm);
    this.salt = new byte[SALT_LENGTH];
    saltGen.nextBytes(this.salt);

    e_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES);
    d_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES);

    e_Cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParamSpec);
    d_Cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParamSpec);
}

public String encrypt(String cleartext) throws IllegalBlockSizeException, BadPaddingException {
    byte[] encrypted = e_Cipher.doFinal(cleartext.getBytes());

    return convertByteArrayToHex(encrypted);
}

public String decrypt(String cipherString) throws IllegalBlockSizeException {
    byte[] plainText = decrypt(convertStringtobyte(cipherString));

    return(new String(plainText));
}

public byte[] decrypt(byte[] ciphertext) throws IllegalBlockSizeException {        
    byte[] retVal = {(byte)0x00};
    try {
        retVal = d_Cipher.doFinal(ciphertext);
    } catch (BadPaddingException e) {
        Log.e(TAG, e.toString()); 
    }
    return retVal;
}


public String convertByteArrayToHex(byte[] buf) {
    if (buf == null)  
        return "";
    StringBuffer result = new StringBuffer(2*buf.length);  

    for (int i = 0; i < buf.length; i++) {
        appendHex(result, buf[i]);  
    }
    return result.toString();
}

private static void appendHex(StringBuffer sb, byte b) {
    sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}

private static byte[] convertStringtobyte(String hexString) {
    int len = hexString.length()/2;
    byte[] result = new byte[len];
    for (int i = 0; i < len; i++) {
        result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
    }
    return result;
}

public byte[] getSalt() {
    return salt;
}

public SecretKey getSecretKey() {
    return secretKey;
}

public static SecretKey createSecretKey(char[] password) throws NoSuchAlgorithmException, InvalidKeySpecException {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithSHA256And256BitAES);
    return keyFac.generateSecret(pbeKeySpec);
}

}

I will call mCryptoHelper.decrypt(String str) then this results in Illegal block size exception
My Env: Android 1.6 on Eclipse

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:40:48+00:00Added an answer on May 15, 2026 at 5:40 am

    In the code, each time i am generating the “salt”,

    SecureRandom saltGen = SecureRandom.getInstance(randomAlgorithm);
    this.salt = new byte[SALT_LENGTH];
    saltGen.nextBytes(this.salt);
    

    hence there is a difference between the encryption and decryption cipher. so it is giving the error Bad Pad block or Padding block corrupted.
    If i declare the Salt to some known value, it is working fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 429k
  • Answers 429k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Let's take the example of 36. 36 = 0010 0100… May 15, 2026 at 1:30 pm
  • Editorial Team
    Editorial Team added an answer Use GetFont() to get the font of the control. Strike-through… May 15, 2026 at 1:30 pm
  • Editorial Team
    Editorial Team added an answer XHTML is just another input language into the common DOM.… May 15, 2026 at 1:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.