gpt4 book ai didi

java - 使用相同密码实例的 BadPadding 异常

转载 作者:行者123 更新时间:2023-11-29 04:42:12 25 4
gpt4 key购买 nike

我有这个加密代码,没问题。我可以用另一种语言解密它加密的文本,但我现在需要用 java 解密它。

 private static final String AES = "AES";
private static final String CBC_BLOCK = "CBC";
private static final String ECB_BLOCK = "ECB";
private static final String PADDING = "PKCS5Padding";
private static final String AES_CBC_PCKS5_CIPHER_CONFIG = AES + "/" + CBC_BLOCK + "/" + PADDING;
private static final String AES_ECB_PCKS5_CIPHER_CONFIG = AES + "/" + ECB_BLOCK + "/" + PADDING;

public static String encryptInAesEcbPkcs5Padding(String salt, String message) {
String encryptedMessage = "";
SecretKeySpec key = null;
try {
if (message != null && !message.equals("")) {
key = new SecretKeySpec(salt.getBytes(StandardCharsets.UTF_8), AES);
Cipher cipher = Cipher.getInstance(AES_ECB_PCKS5_CIPHER_CONFIG);
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedMessage = convertMessageToBase64(cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)));
}
} catch (NoSuchAlgorithmException e) {
LOGGER.error(LogPreFix.ERROR + "No such algorithm [" + AES + "]", e);
} catch (NoSuchPaddingException e) {
LOGGER.error(LogPreFix.ERROR + "No such padding for algorithm [" + AES + "]", e);
} catch (IllegalBlockSizeException e) {
LOGGER.error(LogPreFix.ERROR + "Invalid block size for [" + AES + "/" + ECB_BLOCK + "]", e);
} catch (BadPaddingException e) {
LOGGER.error(LogPreFix.ERROR + "Invalid padding [" + PADDING + "]", e);
} catch (InvalidKeyException e) {
LOGGER.error("Invalid key [" + key + "]", e);
}
return encryptedMessage;
}

尝试使用此代码解密。我使用与加密完全相同的盐,并传入加密器创建的字符串作为“消息”

  public static String decrypt(String message, String salt) throws InvalidAlgorithmParameterException {
SecretKeySpec key = null;
String string = null;
try {
if (message != null && !message.equals("")) {
String decoded = convertBase64ToMessage(message.getBytes(StandardCharsets.UTF_8));
key = new SecretKeySpec(salt.getBytes(StandardCharsets.UTF_8), AES);
Cipher cipher = Cipher.getInstance(AES_ECB_PCKS5_CIPHER_CONFIG);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(decoded.getBytes(StandardCharsets.UTF_8));
string = new String(decrypted);
}
} catch (NoSuchAlgorithmException e) {
LOGGER.error(LogPreFix.ERROR + "No such algorithm [" + AES + "]", e);
} catch (NoSuchPaddingException e) {
LOGGER.error(LogPreFix.ERROR + "No such padding for algorithm [" + AES + "]", e);
} catch (IllegalBlockSizeException e) {
LOGGER.error(LogPreFix.ERROR + "Invalid block size for [" + AES + "/" + ECB_BLOCK + "]", e);
} catch (BadPaddingException e) {
LOGGER.error(LogPreFix.ERROR + "Invalid padding [" + PADDING + "]", e);
} catch (InvalidKeyException e) {
LOGGER.error("Invalid key [" + key + "]", e);
}
return string;
}

但我得到了这个错误,因为我使用的是与加密时相同的 Cipher 实例,我不确定为什么我无法解密消息。

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
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:2087)

最佳答案

convertBase64ToMessage 不应返回 String,而应返回 byte[],因为密文不能由可打印的字符串表示(带有高概率)。

关于java - 使用相同密码实例的 BadPadding 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com