sto ottenendo un errore di decrittografia in classe Java:javax.crypto.IllegalBlockSizeException: lunghezza di ingresso deve essere multiplo di 16, quando decifrare con imbottitura cifra
javax.crypto.IllegalBlockSizeException :
Input length must be multiple of 16 when decrypting with padded cipher.
Cosa posso fare per risolvere questo problema?
UPDATE:
Ho dimenticato di dire che funzioni una volta e quando la seconda volta nel tentativo di eseguire di nuovo il suo gettando l'errore sopra citato.
package com.tb.module.service;
import java.security.Key;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
/**
* This class is used for encrypt and decrypt the password field.
*
*/
public class PswdEnc {
private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't','S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
Qualsiasi cosa decifri è la dimensione sbagliata. Deve essere un multiplo della dimensione del blocco (16), altrimenti non può essere decodificato. Come stai ottenendo i tuoi dati decrittografati? – thegrinner
in realtà sto crittografando la password prima di salvarla in sql db. quindi durante il recupero sto cercando di decrittografarlo. – baburao113
Sei sicuro che i dati che stai salvando e i dati che stai leggendo abbiano la stessa lunghezza? – thegrinner