我有一个使用Java解密AES的任务,但我不断收到pad block corrupted错误。我知道这个问题已经在多个线程中得到了解决,但我仍然无法弄明白。
我已将此信息提供给我:
键:0123456789abcdef
IV:0000000000000000
加密字符串:1ff4ec7cef0e00d81b2d55a4bfdad4ba
根据作业应该给出字符串“纯文本”。
这是我的代码:( IVtest数组与KeyIvEncrypted相同,但使用十六进制代替)
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.crypto.BufferedBlockCipher;
import java.util.Base64;
public class AESdecryptor {
/*
private static String[] KeyIvEncrypted = new String[]{
"ABEiM0RVZneImaq7zN3u/w==",
"AAECAwQFBgcICQoLDA0ODw==",
"ZtrkahwcMzTu7e/WuJ3AZmF09DE="
};*/
public static String[] KeyIvEncrypted = new String[]{
new String("0123456789abcdef"),
new String("0000000000000000"),
new String("1ff4ec7cef0e00d81b2d55a4bfdad4ba")
};
public static byte[][] Ivtest = {{0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0x1,0xf,0xf,0x4,0xe,0xc,0x7,0xc,0xe,0xf,0x0,0xe,0x0,0x0,0xd,0x8,0x1,0xb,0x2,0xd,0x5,0x5,0xa,0x4,0xb,0xf,0xd,0xa,0xd,0x4,0xb,0xa}};
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException, UnsupportedEncodingException, InvalidKeySpecException{
Security.addProvider(new BouncyCastleProvider());
System.out.println(new String(decrypt(),"ISO-8859-1"));
}
private static byte[] transform(int mode, byte[] keyBytes, byte[] ivBytes, byte[] messageBytes) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeySpecException
{
final SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
final IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
final Cipher cipher = Cipher.getInstance("AES/CBC/pkcs7Padding");
cipher.init(mode, keySpec, ivSpec);
return cipher.doFinal(messageBytes);
}
public static byte[] decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException, UnsupportedEncodingException, InvalidKeySpecException{
//return AESdecryptor.transform(Cipher.DECRYPT_MODE, Base64.getDecoder().decode(KeyIvEncrypted[0]), Base64.getDecoder().decode(KeyIvEncrypted[1]), Base64.getDecoder().decode(KeyIvEncrypted[2]));
return AESdecryptor.transform(Cipher.DECRYPT_MODE, Ivtest[0], Ivtest[1], Ivtest[2]);
}
}
答案 0 :(得分:1)
以下代码将解决您的问题,我将在下面发表评论......
package so;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.util.encoders.Hex;
public class AESdecryptor {
public static String[] KeyIvEncrypted = new String[]{
new String("0123456789abcdef"),
new String("0000000000000000"),
new String("1ff4ec7cef0e00d81b2d55a4bfdad4ba")
};
public static void main(String[] args) throws GeneralSecurityException {
// Security.addProvider(new BouncyCastleProvider());
byte[] decrypted = decrypt();
System.out.println(new String(decrypted, StandardCharsets.ISO_8859_1));
}
private static byte[] transform(int mode, byte[] keyBytes, byte[] ivBytes, byte[] messageBytes)
throws GeneralSecurityException {
final SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
final IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(mode, keySpec, ivSpec);
return cipher.doFinal(messageBytes);
}
public static byte[] decrypt() throws GeneralSecurityException {
return AESdecryptor.transform(Cipher.DECRYPT_MODE, KeyIvEncrypted[0].getBytes(), KeyIvEncrypted[1].getBytes(), Hex.decode(KeyIvEncrypted[2]));
}
}
发现了以下问题:
Hex
类,就像您已经拥有的那样)。 / LI>
请注意,即使正确执行,加密也不等于安全。