Javax.crypto.BadPaddingException: данный последний блок не дополнен должным образом. Такие проблемы могут возникнуть, есJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Javax.crypto.BadPaddingException: данный последний блок не дополнен должным образом. Такие проблемы могут возникнуть, ес

Сообщение Anonymous »

У меня есть следующая служба Spring Boot, используемая для шифрования и расшифровки строк:

Код: Выделить всё

@Service
public class EncryptionServiceImpl implements EncryptionService {
// Strong AES key (256-bit)
private static final String AES_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final int AES_KEY_SIZE = 256;

// Generate a strong secret key for AES encryption
private SecretKey secretKey;
private SecureRandom secureRandom = new SecureRandom();

private DataKeysRepository dataKeysRepository;

@Autowired
public EncryptionServiceImpl(DataKeysRepository dataKeysRepository) throws Exception {
// Generate or load your AES SecretKey securely
this.secretKey = generateSecretKey();
if (this.secretKey == null) {
throw new IllegalStateException("SecretKey generation failed!");
}
this.dataKeysRepository = dataKeysRepository;
}

/**
* Generates a strong AES SecretKey.
* @return SecretKey
* @throws Exception If key generation fails.
*/
private SecretKey generateSecretKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(AES_KEY_SIZE, secureRandom); // 256-bit AES key
return keyGenerator.generateKey();
}

@Override
public String encrypt(String plainText)
{
try {
byte[] iv = new byte[16];
secureRandom.nextBytes(iv);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

// Initialize cipher for encryption
Cipher cipher = Cipher.getInstance(AES_CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);

// Encrypt the plain text
byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));

// Combine IV and encrypted text for storage/transmission
byte[] ivAndEncryptedText = new byte[iv.length + encryptedTextBytes.length];
System.arraycopy(iv, 0, ivAndEncryptedText, 0, iv.length);
System.arraycopy(encryptedTextBytes, 0, ivAndEncryptedText, iv.length, encryptedTextBytes.length);

DataKeys data = new DataKeys();
data.setText(Base64.getEncoder().encodeToString(ivAndEncryptedText));
data.setCreatedAt(OffsetDateTime.now());
DataKeys key = dataKeysRepository.save(data);

// Return UUID of the encrypted text
return key.getId().toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
public String decrypt(String key)
{
Optional byId = dataKeysRepository.findById(UUID.fromString(key));
if (byId.isPresent()) {
DataKeys dataKeys = byId.get();

try {
byte[] ivAndEncryptedText = Base64.getDecoder().decode(dataKeys.getText());

// Extract the IV
byte[] iv = new byte[16];
System.arraycopy(ivAndEncryptedText, 0, iv, 0, iv.length);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

// Extract encrypted text
byte[] encryptedTextBytes = new byte[ivAndEncryptedText.length - iv.length];
System.arraycopy(ivAndEncryptedText, iv.length, encryptedTextBytes, 0, encryptedTextBytes.length);

// Initialize cipher for decryption
Cipher cipher = Cipher.getInstance(AES_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

// Decrypt and return as plain text
byte[] decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
return new String(decryptedTextBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
Когда я отправляю запрос на шифрование и расшифровку текста, он работает нормально.
Но когда я перезапускаю микросервис, а затем пытаюсь расшифровать строку, я получаю ошибку:
п>

Код: Выделить всё

javax.crypto.BadPaddingException: Given final block not properly padded.  Such issues can arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:861)
at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:941)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:734)
at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2244)
в этом методе Java: doFinal(...)
Знаете ли вы, что вызывает эту проблему?

Подробнее здесь: https://stackoverflow.com/questions/791 ... ed-such-is
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»