Getting javax.crypto.IllegalBlockSizeException: last block incomplete in decryption error

437 Views Asked by At

I am new to java, so please forgive me for silly questions

1) my project moved from netbeans 6 to 8

2) jdk moved from 6u45 to 8u11

3) After successful setup to new environment when I debug

got error: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

Stacktrace:

com.alstom.tsoft.controller.system.ParameterTablesController$LoadOfflineWorker - [javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:121), javax.crypto.CipherInputStream.read(CipherInputStream.java:192), com.alstom.tsoft.crypto.CryptoManager.decryptString(CryptoManager.java:146), com.alstom.tsoft.io.CryptedFieldReader.readField(CryptedFieldReader.java:58),

4) It never happened in previous environment(jdk6 and netbeans 6), that is the only change in project.

We are using following input:

private static final String ALGO_TYPE = "AES";
private static final String ALGO_FULL_NAME = "AES/ECB/PKCS5Padding";
private static final String ALGO_PROVIDER = "BC";

public String encryptString(String input) throws NoSuchAlgorithmException,NoSuchProviderException,
       NoSuchPaddingException,InvalidKeyException,IOException {
    StringBuffer output = new StringBuffer();
    CipherInputStream cis = new CipherInputStream(
           new ByteArrayInputStream(input.getBytes()),
           getEncryptionCipher());                
    int b;
    while ( ( b = cis.read() ) != -1 ) {           
       String twoHexDigit = String.format("%1$02x",b);           
       //m_logger.info(":"+b+":"+twoHexDigit.toUpperCase());
       output.append(twoHexDigit.toUpperCase());
    }
    return output.toString();
}
//Dencryption
public String decryptString(String input) throws NoSuchAlgorithmException,NoSuchProviderException,
       NoSuchPaddingException,InvalidKeyException,IOException {       
    StringBuffer output = new StringBuffer();                        
    byte[] inputBytes = convertHexStringToByte(input);        

    CipherInputStream cis = new CipherInputStream(
           new ByteArrayInputStream(inputBytes), 
           getDecryptionCipher());                      
    int b;
    while ( ( b = cis.read() ) != -1 ) {
       output.append(new Character((char)b));
    }                                              
    return output.toString();
}

Please suggest where I am doing wrong and how to debug

Thanks in advance

0

There are 0 best solutions below