Java – when decrypting with a padding password, the input length must be a multiple of 16

I have a server and client socket program. The server sends encrypted messages to the client, that is

cipher2 = Cipher.getInstance("AES"); 
secretKeySpec = new SecretKeySpec(decryptedText,"AES");
cipher2.init(Cipher.ENCRYPT_MODE,secretKeySpec);
Feedback = "Your answer is wrong".getBytes();
cipher2.doFinal(Feedback);
dos.writeInt(Feedback.length);
dos.write(Feedback);

Client code:

int result_len = 0;
result_len = din.readInt();            
byte[] result_Bytes = new byte[result_len];
din.readFully(result_Bytes);
cipher2 = Cipher.getInstance("AES");
cipher2.init(Cipher.DECRYPT_MODE,aesKey);             
byte[] encrypt = cipher2.doFinal(result_Bytes);

Exception thrown byte [] encrypt = cipher2 doFinal(result_Bytes);

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
    at javax.crypto.Cipher.doFinal(Cipher.java:2086)

What's the problem?

Solution

There is a similar problem But it is important to understand the root cause, which may vary from use case to use case

Scenario 1 you are trying to decrypt a value that was not correctly encoded first

byte[] encryptedBytes = Base64.decodeBase64(encryptedBase64String);

If the string is incorrectly configured or encoded for some reason, you will see the error "the input length must be a multiple of 16 when decrypting with fill password"

Scenario 2 now, if you can use this encoded string in the URL (try to pass in the base64encoded value in the URL, it will fail. You should do urlencoding and then pass in the token, and it will work

Scenario 3 when integrating with one of the vendors, we find that we must use urlencoder to encrypt Base64, but we do not need to decode it because it is completed internally by the vendor

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>