我已使用此方法在C#中加密了文件:
public byte[] Crypt(byte[] filearray, string sKey)
{
AesManaged DES = new AesManaged();
DES.Key = Encoding.UTF8.GetBytes(sKey);
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform crypt = DES.CreateEncryptor();
byte[] cipher = crypt.TransformFinalBlock(niz, 0, filearray.Length);
String encryptedText = Convert.ToBase64String(cipher);
return cipher.ToArray();
}
现在我尝试使用Java解密文件:
private static byte[] transform(string base64Key, final byte[] fileBytes) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchProviderException
{
final byte[] keyBytes = base64Key.getBytes(StandardCharsets.UTF_8);
final byte[] ivBytes = base64Iv.getBytes(StandardCharsets.UTF_8);
final SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
final IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
byte[] transformedBytes = null;
try
{
Security.addProvider(new BouncyCastleProvider());
final Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");;
cipher.init(Cipher.DECRYPT_MODE, keySpec);
transformedBytes = cipher.doFinal(fileBytes);
}
catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
BadPaddingException e)
{
e.printStackTrace();
}
return transformedBytes;
}
当我尝试执行此功能时,出现异常
javax.crypto.BadPaddingException:填充块已损坏在org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher $ BufferedGenericBlockCipher.doFinal处(未知来源)在org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(未知来源)在javax.crypto.Cipher.doFinal(Cipher.java:2165)
[尝试使用其他填充,例如“ AES / ECB / NoPadding”代替“ AES / ECB / PKCS7Padding”