[英]Cannot decrypt when encrypted using AES/ECB/PKCS5Padding - input length must be multiple of 16
I have this encryption code which works no problem. 我有没有问题的加密代码。 I can decrypt the text it encrypts in another language, but I need to decrypt it in java now.
我可以解密用另一种语言加密的文本,但是现在需要在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;
}
Trying to decrypt with this code. 尝试使用此代码解密。 I am using the exact same salt as the encryption and passing in the string the encrypter creates as the "message"
我使用与加密完全相同的盐,并将加密器创建的字符串作为“消息”传递
public static String decrypt(String message, String salt) throws InvalidAlgorithmParameterException {
SecretKeySpec key = null;
String string = 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.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(message.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;
}
But i get this error, what am i doing wrong? 但是我收到此错误,我在做什么错? Seems like it should work.
好像它应该工作。 I tried padding the encrypted text with "=" added until it is divisible by 16 but that returns a bad padding error.
我尝试用“ =”填充加密的文本,直到被16整除,但这会返回错误的填充错误。
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
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)
首先进行Base64解码,以反转在原始加密过程中执行的Base64编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.