java aes 填充_Java中的Aes解密 – 填充问题

在尝试解密大文件时遇到了 javax.crypto.BadPaddingException,提示给定的最终块未正确填充。问题出在使用CipherOutputStream时错误地调用了cipher.doFinal()。解决方法是仅使用CipherOutputStream,不需要手动调用doFinal(),并在适当时候关闭输出流,CipherOutputStream会在关闭时自动调用doFinal完成解密操作。
摘要由CSDN通过智能技术生成

我更改了一些代码行,以保证大文件的加密.

现在我遇到了问题,解密不起作用.

我收到以下错误消息/异常:

Error encrypting/decrypting file

at Algorithmus.Encryptor.doCrypto(Encryptor.java:71)

at Algorithmus.Encryptor.decrypt(Encryptor.java:39)

at GUI.MainWindow$encryptThread.run(MainWindow.java:838)

Caused by: javax.crypto.BadPaddingException: Given final block not properly padded

at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)

at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)

at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)

at javax.crypto.Cipher.doFinal(Cipher.java:2165)

at Algorithmus.Encryptor.doCrypto(Encryptor.java:60)

... 2 more

我试图将Transoformation参数更改为AES / CBC / PKCS5Padding,但这没有任何效果.有谁知道,如何优化给定的代码?

private static final String ALGORITHM = "AES";

private static final String TRANSFORMATION = "AES";

public static void encrypt(String key, File inputFile, File outputFile)

throws ExtendedException {

doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);

}

public static void decrypt(String key, File inputFile, File outputFile)

throws ExtendedException {

doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);

}

private static void doCrypto(int cipherMode, String key, File inputFile,

File outputFile) throws ExtendedException {

try {

Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);

Cipher cipher = Cipher.getInstance(TRANSFORMATION);

cipher.init(cipherMode, secretKey);

FileInputStream inputStream = new FileInputStream(inputFile);

CipherOutputStream out = new CipherOutputStream(new FileOutputStream(outputFile), cipher);

byte[] buffer = new byte[8192];

byte[] outputBytes = null;

FileOutputStream outputStream = new FileOutputStream(outputFile);

int count;

while ((count = inputStream.read(buffer)) > 0)

{

out.write(buffer, 0, count);

outputBytes = cipher.doFinal(buffer);

}

inputStream.close();

outputStream.close();

} catch (NoSuchPaddingException | NoSuchAlgorithmException

| InvalidKeyException | BadPaddingException

| IllegalBlockSizeException | IOException ex) {

throw new ExtendedException("Error encrypting/decrypting file", ex);

}

}

解决方法:

只需使用CipherOutputStream即可.不要调用cipher.doFinal(缓冲区),也不要忘记关闭输出流.

FileInputStream inputStream = new FileInputStream(inputFile);

FileOutputStream fileout = new FileOutputStream(outputFile);

CipherOutputStream out = new CipherOutputStream(fileout , cipher);

try {

byte[] buffer = new byte[8192];

int count;

while ((count = inputStream.read(buffer)) > 0) {

out.write(buffer, 0, count);

}

} finally {

out.close();

inputStream.close();

}

CipherOutputStream为您管理密码.它将在关闭流并刷新内部缓冲区时调用doFinal.

标签:java,padding,cryptography,encryption,aes

来源: https://codeday.me/bug/20190628/1311548.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,针对您的问题,我可以简单地给出以下代码示例,展示如何使用 JavaAES解密算法。 首先,我们需要引入以下包: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; ``` 然后,我们需要定义一个加解密的通用函数: ```java public static String encrypt(String data, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); int blockSize = cipher.getBlockSize(); byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return new String(Base64.getEncoder().encode(encrypted)); } public static String decrypt(String data, String key, String iv) throws Exception { byte[] encrypted = Base64.getDecoder().decode(data.getBytes()); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] decrypted = cipher.doFinal(encrypted); return new String(decrypted).trim(); } ``` 在上述函数,我们使用了 AES解密算法,采用了 CBC 模式和 NoPadding 填充方式。同时,我们需要传入三个参数: - data:待加解密的数据 - key:AES 密钥 - iv:AES 偏移量 最后,我们可以调用上述函数进行加解密操作: ```java String key = "0123456789abcdef"; String iv = "fedcba9876543210"; String data = "Hello, world!"; String encrypted = encrypt(data, key, iv); String decrypted = decrypt(encrypted, key, iv); System.out.println("Encrypted: " + encrypted); System.out.println("Decrypted: " + decrypted); ``` 以上代码展示了如何使用 JavaAES解密算法进行加解密操作。需要注意的是,为了确保加解密的正确性,我们需要在加密和解密时使用相同的密钥和偏移量。同时,由于使用了 NoPadding 填充方式,因此我们需要手动对待加密的数据进行填充
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值