Tôi nhận được một lỗi giải mã trong lớp java:javax.crypto.IllegalBlockSizeException: chiều dài đầu vào phải là bội số của 16 khi giải mã với đệm mật mã
javax.crypto.IllegalBlockSizeException :
Input length must be multiple of 16 when decrypting with padded cipher.
Tôi có thể làm gì để giải quyết vấn đề này?
UPDATE:
tôi quên đề cập đến nó đang làm việc một lần và khi thời gian im thứ hai cố gắng để thực hiện nó một lần nữa ném các lỗi nêu trên của nó.
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;
}
}
Bất cứ điều gì bạn giải mã đều sai kích thước. Nó cần phải là bội số của kích thước khối (16) hoặc nếu không nó không thể được giải mã. Làm thế nào bạn nhận được dữ liệu đã giải mã của bạn? – thegrinner
thực sự tôi đang mã hóa mật khẩu trước khi lưu nó vào sql db. sau đó trong khi lấy tôi đang cố giải mã nó. – baburao113
Bạn có chắc chắn dữ liệu bạn đang lưu và dữ liệu bạn đang đọc có cùng độ dài không? – thegrinner