Этот код является частью алгоритма AES, выполняет шифрование и дешифрование, у меня есть этот метод, который возвращает зашифрованное значение как [Hex], когда я передаю зашифрованное значение в качестве ввода для метода дешифрования, я получил эту ошибку:
[Длина ввода должна быть кратна 16 при дешифровании с помощью шифра с подкладкой], есть предложения по ее исправлению, пожалуйста?
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());
// simply store the encoded byte array here
byte[] bytes = Base64.getEncoder().encode(encVal);
// loop over the bytes and append each byte as hex string
StringBuilder sb = new StringBuilder(bytes.length * 2);
for(byte b : bytes)
sb.append(String.format("%02x", b));
return sb.toString();
}
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;
}