Java AES decryption problem : Input length must be multiple of 16 when decrypting with padded cipher











up vote
-1
down vote

favorite












Here's the thing, I'm able to encrypt text by calling these methods which I've written but I'm not able to decrypt the text that I'm receiving from the database.



After having looked up various solutions here, I did add the encoding type while forming the original string and I still am facing this problem.



Here are the methods for encrypting and decrypting used :



//fetching key from keystore
private static void init() {
KeyStore keyStore;
try {
keyStore = KeyStore.getInstance(KEYSTORE_FORMAT);
InputStream inputStream = new FileInputStream(PROPERTIES_FILE_NAME);
keyStore.load(inputStream, PASSWORD.toCharArray());
key = keyStore.getKey(KEY_ALIAS, PASSWORD.toCharArray());
cipher = Cipher.getInstance(CIPHER_ALGORITHM);
inputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}

//returning key either for encryption or decryption
public static Key getKey() {
return key;
}

public static String encrypt(String plainText) throws HeException {
try {
cipher.init(Cipher.ENCRYPT_MODE, getKey());
byte encryptedByte = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = Base64.getEncoder().encodeToString(encryptedByte);
return encryptedText;
} catch (Exception e) {
System.out.println(e);
}
}

public static String decrypt(String encryptedText) throws HeException {
try {
byte encryptedTextByte = Base64.getDecoder().decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, getKey());
String decryptedText = new String(cipher.doFinal(encryptedTextByte),"UTF-8");
return decryptedText;
} catch (Exception e) {
System.out.println(e);
}
}


And here's the exception I'm getting :



javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446) ~[sunjce_provider.jar:1.8.0_171]
at javax.crypto.Cipher.doFinal(Cipher.java:2164) ~[?:1.8.0_171]


Is there anything to be added apart from the encoding for the decryption or do I have to change the way of encryption itself ?
Would greatly appreciate your help and thanks a lot in advance.










share|improve this question






















  • There is something lost between the encryption and decryption. ECB mode - the default mode unfortunately - always outputs X times the block size. If the input size is not a multiple of the block size then you'll get this exception. Maybe different code was used to produce the ciphertext or too few characters are stored in the DB...
    – Maarten Bodewes
    Nov 22 at 12:45










  • To be able to decrypt you need to know algorithm, mode and padding used to encrypt. You need to use the same values for these 3 items while decrypting. Moreover, if an IV is used (as required by all secured modes), you need to know how to extract the IV from the cipher text
    – Saptarshi Basu
    Nov 22 at 14:30










  • Please clarify CIPHER_ALGORITHM
    – kelalaka
    Nov 22 at 19:43










  • Arafind, you should definitely print out and provide us the ouput of the encryption process and input of the decryption process so we can see what goes wrong. This is more debugging than a concise question, and those are impossible to answer without test data - including test keys and such - as well as an MCVE.
    – Maarten Bodewes
    Nov 25 at 15:26










  • Thanks for all the comments guys and apologies, I figured out that during the decryption process, my code was trying to read some texts that were plain texts as well as other texts that were already encrypted with another key. Apologies once, again.
    – Aravind S
    Nov 27 at 4:35















up vote
-1
down vote

favorite












Here's the thing, I'm able to encrypt text by calling these methods which I've written but I'm not able to decrypt the text that I'm receiving from the database.



After having looked up various solutions here, I did add the encoding type while forming the original string and I still am facing this problem.



Here are the methods for encrypting and decrypting used :



//fetching key from keystore
private static void init() {
KeyStore keyStore;
try {
keyStore = KeyStore.getInstance(KEYSTORE_FORMAT);
InputStream inputStream = new FileInputStream(PROPERTIES_FILE_NAME);
keyStore.load(inputStream, PASSWORD.toCharArray());
key = keyStore.getKey(KEY_ALIAS, PASSWORD.toCharArray());
cipher = Cipher.getInstance(CIPHER_ALGORITHM);
inputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}

//returning key either for encryption or decryption
public static Key getKey() {
return key;
}

public static String encrypt(String plainText) throws HeException {
try {
cipher.init(Cipher.ENCRYPT_MODE, getKey());
byte encryptedByte = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = Base64.getEncoder().encodeToString(encryptedByte);
return encryptedText;
} catch (Exception e) {
System.out.println(e);
}
}

public static String decrypt(String encryptedText) throws HeException {
try {
byte encryptedTextByte = Base64.getDecoder().decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, getKey());
String decryptedText = new String(cipher.doFinal(encryptedTextByte),"UTF-8");
return decryptedText;
} catch (Exception e) {
System.out.println(e);
}
}


And here's the exception I'm getting :



javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446) ~[sunjce_provider.jar:1.8.0_171]
at javax.crypto.Cipher.doFinal(Cipher.java:2164) ~[?:1.8.0_171]


Is there anything to be added apart from the encoding for the decryption or do I have to change the way of encryption itself ?
Would greatly appreciate your help and thanks a lot in advance.










share|improve this question






















  • There is something lost between the encryption and decryption. ECB mode - the default mode unfortunately - always outputs X times the block size. If the input size is not a multiple of the block size then you'll get this exception. Maybe different code was used to produce the ciphertext or too few characters are stored in the DB...
    – Maarten Bodewes
    Nov 22 at 12:45










  • To be able to decrypt you need to know algorithm, mode and padding used to encrypt. You need to use the same values for these 3 items while decrypting. Moreover, if an IV is used (as required by all secured modes), you need to know how to extract the IV from the cipher text
    – Saptarshi Basu
    Nov 22 at 14:30










  • Please clarify CIPHER_ALGORITHM
    – kelalaka
    Nov 22 at 19:43










  • Arafind, you should definitely print out and provide us the ouput of the encryption process and input of the decryption process so we can see what goes wrong. This is more debugging than a concise question, and those are impossible to answer without test data - including test keys and such - as well as an MCVE.
    – Maarten Bodewes
    Nov 25 at 15:26










  • Thanks for all the comments guys and apologies, I figured out that during the decryption process, my code was trying to read some texts that were plain texts as well as other texts that were already encrypted with another key. Apologies once, again.
    – Aravind S
    Nov 27 at 4:35













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Here's the thing, I'm able to encrypt text by calling these methods which I've written but I'm not able to decrypt the text that I'm receiving from the database.



After having looked up various solutions here, I did add the encoding type while forming the original string and I still am facing this problem.



Here are the methods for encrypting and decrypting used :



//fetching key from keystore
private static void init() {
KeyStore keyStore;
try {
keyStore = KeyStore.getInstance(KEYSTORE_FORMAT);
InputStream inputStream = new FileInputStream(PROPERTIES_FILE_NAME);
keyStore.load(inputStream, PASSWORD.toCharArray());
key = keyStore.getKey(KEY_ALIAS, PASSWORD.toCharArray());
cipher = Cipher.getInstance(CIPHER_ALGORITHM);
inputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}

//returning key either for encryption or decryption
public static Key getKey() {
return key;
}

public static String encrypt(String plainText) throws HeException {
try {
cipher.init(Cipher.ENCRYPT_MODE, getKey());
byte encryptedByte = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = Base64.getEncoder().encodeToString(encryptedByte);
return encryptedText;
} catch (Exception e) {
System.out.println(e);
}
}

public static String decrypt(String encryptedText) throws HeException {
try {
byte encryptedTextByte = Base64.getDecoder().decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, getKey());
String decryptedText = new String(cipher.doFinal(encryptedTextByte),"UTF-8");
return decryptedText;
} catch (Exception e) {
System.out.println(e);
}
}


And here's the exception I'm getting :



javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446) ~[sunjce_provider.jar:1.8.0_171]
at javax.crypto.Cipher.doFinal(Cipher.java:2164) ~[?:1.8.0_171]


Is there anything to be added apart from the encoding for the decryption or do I have to change the way of encryption itself ?
Would greatly appreciate your help and thanks a lot in advance.










share|improve this question













Here's the thing, I'm able to encrypt text by calling these methods which I've written but I'm not able to decrypt the text that I'm receiving from the database.



After having looked up various solutions here, I did add the encoding type while forming the original string and I still am facing this problem.



Here are the methods for encrypting and decrypting used :



//fetching key from keystore
private static void init() {
KeyStore keyStore;
try {
keyStore = KeyStore.getInstance(KEYSTORE_FORMAT);
InputStream inputStream = new FileInputStream(PROPERTIES_FILE_NAME);
keyStore.load(inputStream, PASSWORD.toCharArray());
key = keyStore.getKey(KEY_ALIAS, PASSWORD.toCharArray());
cipher = Cipher.getInstance(CIPHER_ALGORITHM);
inputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}

//returning key either for encryption or decryption
public static Key getKey() {
return key;
}

public static String encrypt(String plainText) throws HeException {
try {
cipher.init(Cipher.ENCRYPT_MODE, getKey());
byte encryptedByte = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = Base64.getEncoder().encodeToString(encryptedByte);
return encryptedText;
} catch (Exception e) {
System.out.println(e);
}
}

public static String decrypt(String encryptedText) throws HeException {
try {
byte encryptedTextByte = Base64.getDecoder().decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, getKey());
String decryptedText = new String(cipher.doFinal(encryptedTextByte),"UTF-8");
return decryptedText;
} catch (Exception e) {
System.out.println(e);
}
}


And here's the exception I'm getting :



javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847) ~[sunjce_provider.jar:1.8.0_171]
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446) ~[sunjce_provider.jar:1.8.0_171]
at javax.crypto.Cipher.doFinal(Cipher.java:2164) ~[?:1.8.0_171]


Is there anything to be added apart from the encoding for the decryption or do I have to change the way of encryption itself ?
Would greatly appreciate your help and thanks a lot in advance.







java cryptography aes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 11:34









Aravind S

10112




10112












  • There is something lost between the encryption and decryption. ECB mode - the default mode unfortunately - always outputs X times the block size. If the input size is not a multiple of the block size then you'll get this exception. Maybe different code was used to produce the ciphertext or too few characters are stored in the DB...
    – Maarten Bodewes
    Nov 22 at 12:45










  • To be able to decrypt you need to know algorithm, mode and padding used to encrypt. You need to use the same values for these 3 items while decrypting. Moreover, if an IV is used (as required by all secured modes), you need to know how to extract the IV from the cipher text
    – Saptarshi Basu
    Nov 22 at 14:30










  • Please clarify CIPHER_ALGORITHM
    – kelalaka
    Nov 22 at 19:43










  • Arafind, you should definitely print out and provide us the ouput of the encryption process and input of the decryption process so we can see what goes wrong. This is more debugging than a concise question, and those are impossible to answer without test data - including test keys and such - as well as an MCVE.
    – Maarten Bodewes
    Nov 25 at 15:26










  • Thanks for all the comments guys and apologies, I figured out that during the decryption process, my code was trying to read some texts that were plain texts as well as other texts that were already encrypted with another key. Apologies once, again.
    – Aravind S
    Nov 27 at 4:35


















  • There is something lost between the encryption and decryption. ECB mode - the default mode unfortunately - always outputs X times the block size. If the input size is not a multiple of the block size then you'll get this exception. Maybe different code was used to produce the ciphertext or too few characters are stored in the DB...
    – Maarten Bodewes
    Nov 22 at 12:45










  • To be able to decrypt you need to know algorithm, mode and padding used to encrypt. You need to use the same values for these 3 items while decrypting. Moreover, if an IV is used (as required by all secured modes), you need to know how to extract the IV from the cipher text
    – Saptarshi Basu
    Nov 22 at 14:30










  • Please clarify CIPHER_ALGORITHM
    – kelalaka
    Nov 22 at 19:43










  • Arafind, you should definitely print out and provide us the ouput of the encryption process and input of the decryption process so we can see what goes wrong. This is more debugging than a concise question, and those are impossible to answer without test data - including test keys and such - as well as an MCVE.
    – Maarten Bodewes
    Nov 25 at 15:26










  • Thanks for all the comments guys and apologies, I figured out that during the decryption process, my code was trying to read some texts that were plain texts as well as other texts that were already encrypted with another key. Apologies once, again.
    – Aravind S
    Nov 27 at 4:35
















There is something lost between the encryption and decryption. ECB mode - the default mode unfortunately - always outputs X times the block size. If the input size is not a multiple of the block size then you'll get this exception. Maybe different code was used to produce the ciphertext or too few characters are stored in the DB...
– Maarten Bodewes
Nov 22 at 12:45




There is something lost between the encryption and decryption. ECB mode - the default mode unfortunately - always outputs X times the block size. If the input size is not a multiple of the block size then you'll get this exception. Maybe different code was used to produce the ciphertext or too few characters are stored in the DB...
– Maarten Bodewes
Nov 22 at 12:45












To be able to decrypt you need to know algorithm, mode and padding used to encrypt. You need to use the same values for these 3 items while decrypting. Moreover, if an IV is used (as required by all secured modes), you need to know how to extract the IV from the cipher text
– Saptarshi Basu
Nov 22 at 14:30




To be able to decrypt you need to know algorithm, mode and padding used to encrypt. You need to use the same values for these 3 items while decrypting. Moreover, if an IV is used (as required by all secured modes), you need to know how to extract the IV from the cipher text
– Saptarshi Basu
Nov 22 at 14:30












Please clarify CIPHER_ALGORITHM
– kelalaka
Nov 22 at 19:43




Please clarify CIPHER_ALGORITHM
– kelalaka
Nov 22 at 19:43












Arafind, you should definitely print out and provide us the ouput of the encryption process and input of the decryption process so we can see what goes wrong. This is more debugging than a concise question, and those are impossible to answer without test data - including test keys and such - as well as an MCVE.
– Maarten Bodewes
Nov 25 at 15:26




Arafind, you should definitely print out and provide us the ouput of the encryption process and input of the decryption process so we can see what goes wrong. This is more debugging than a concise question, and those are impossible to answer without test data - including test keys and such - as well as an MCVE.
– Maarten Bodewes
Nov 25 at 15:26












Thanks for all the comments guys and apologies, I figured out that during the decryption process, my code was trying to read some texts that were plain texts as well as other texts that were already encrypted with another key. Apologies once, again.
– Aravind S
Nov 27 at 4:35




Thanks for all the comments guys and apologies, I figured out that during the decryption process, my code was trying to read some texts that were plain texts as well as other texts that were already encrypted with another key. Apologies once, again.
– Aravind S
Nov 27 at 4:35

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430064%2fjava-aes-decryption-problem-input-length-must-be-multiple-of-16-when-decryptin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430064%2fjava-aes-decryption-problem-input-length-must-be-multiple-of-16-when-decryptin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks