CipherInputStream / CipherOutputStream的最后一个块不完整,即使使用填充AES / CBC / PKCS5Padding [英] last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding
问题描述
实际上,我从互联网和stackoverflow中搜索了很多,
Actually, I searched lot from internet and in stackoverflow too for this,
最初我没有在加密和解密中使用填充,
Initially I don't used padding in my encryption and decryption,
但最后我从这里得到解决方案
But Finally I got solution from here
https://stackoverflow.com/a/10775577/1115788
,我更新了我的代码,填充为AES / CBC / PKCS5Padding
和相同的错误即将到来,最后一个块不解密...
and I updated my code with padding as AES/CBC/PKCS5Padding and the same error is coming, and last block is not decrypted...
我在过去两天正在处理这个问题,但找不到解决方案
I'm working on this for last two day, but no solution found
我的密码:
package mani.droid.browsedropbox;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Crypter {
Cipher encipher;
Cipher decipher;
CipherInputStream cis;
CipherOutputStream cos;
FileInputStream fis;
byte[] ivbytes = new byte[]{(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o', (byte)'p'};
IvParameterSpec iv = new IvParameterSpec(ivbytes);
public boolean enCrypt(String key, InputStream is, OutputStream os)
{
try {
byte[] encoded = new BigInteger(key, 16).toByteArray();
SecretKey seckey = new SecretKeySpec(encoded, "AES");
encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
encipher.init(Cipher.ENCRYPT_MODE, seckey, iv);
cis = new CipherInputStream(is, encipher);
copyByte(cis, os);
return true;
}
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean deCrypt(String key, InputStream is, OutputStream os)
{
try {
byte[] encoded = new BigInteger(key, 16).toByteArray();
SecretKey seckey = new SecretKeySpec(encoded, "AES");
encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
encipher.init(Cipher.DECRYPT_MODE, seckey, iv);
cos = new CipherOutputStream(os, encipher);
copyByte(is, cos);
//cos.close();
return true;
}
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void copyByte(InputStream is, OutputStream os) throws IOException
{
byte[] buf = new byte[8192];
int numbytes;
while((numbytes = is.read(buf)) != -1)
{
os.write(buf, 0, numbytes);
os.flush();
}
os.close();
is.close();
}
}
推荐答案
我得到了我自己的问题的答案,尝试和错误
实际上这里冲突是我设置填充在 encipher = Cipher.getInstance(AES / CBC / PKCS7Padding);
Finally I got answer for my own question, with trial and error
Actually here Conflict is I set Padding in encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
并设置一些值.....,
and Set IV with some values.....,
最后我只有答案只替换算法
Finally I got Answer only just replaced the Algorithm
从:
AES / CBC / PKCS7Paddinng
AES/CBC/PKCS7Paddinng
To:
AES / CFB8 / NoPadding
AES/CFB8/NoPadding
,它的工作就像魅力....所以我建议这个答案为其他人谁挣扎着这个问题,如果你解决了你的问题在这里提到别人...
and its worked like charm...., So I suggest this answer for others who struggling with this problem, if you solved you problem, mention here for others...
这篇关于CipherInputStream / CipherOutputStream的最后一个块不完整,即使使用填充AES / CBC / PKCS5Padding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!