最后一个块不完整的与CipherInputStream / CipherOutputStream,即使填充AES / CBC / PKCS5Padding [英] last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding

查看:941
本文介绍了最后一个块不完整的与CipherInputStream / CipherOutputStream,即使填充AES / CBC / PKCS5Padding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实,我搜索很多,从互联网和计算器也为这个,

在我的加密和解密起初我没用过填充,

不过,最后我从这里的解决方案

<一个href="http://stackoverflow.com/a/10775577/1115788">http://stackoverflow.com/a/10775577/1115788

和我更新了我的code与填充的AES / CBC / PKCS5Padding 而同样的错误来了,最后一块未解密...

我在这方面的工作的最后两天,但没有找到解决办法

我Crypter code:

 包mani.droid.browsedropbox;

进口java.io.FileInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.math.BigInteger的;
进口java.security.InvalidAlgorithmParameterException;
进口java.security.InvalidKeyException;
进口java.security.NoSuchAlgorithmException;
进口javax.crypto.Cipher中;
进口javax.crypto.CipherInputStream中;
进口javax.crypto.CipherOutputStream中;
进口javax.crypto.NoSuchPaddingException;
进口javax.crypto.SecretKey;
进口javax.crypto.spec.IvParameterSpec;
进口javax.crypto.spec.SecretKeySpec;

公共类Crypter {

    密码加密器;
    密码破译;
CipherInputStream顺;
CipherOutputStream COS;
的FileInputStream FIS;
byte []的ivbytes =新的字节[] {(字节)'A',(字节),B,(字节)'C',(字节)'D',(字节)'E',(字节)'F (字节),'G',(字节)'H',(字节)'我',(字节)'J',(字节)'K'(字节),L,(字节),M (字节)'N',(字节)的O,(字节)'P'};
    IvParameterSpec IV =新IvParameterSpec(ivbytes);

公共布尔加密(字符串键,InputStream的是,OutputStream的OS)
{
    尝试 {
        byte []的EN codeD =新的BigInteger(键,16).toByteArray();
        SecretKey的seckey =新SecretKeySpec(EN codeD,AES);
        加密器= Cipher.getInstance(AES / CBC / PKCS7Padding);
        encipher.init(Cipher.ENCRYPT_MODE,seckey,ⅳ);
        顺=新CipherInputStream(是,加密器);
        copyByte(顺,OS);
        返回true;
    }
    赶上(InvalidKeyException将E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(抛出:NoSuchAlgorithmException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(NoSuchPaddingException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(InvalidAlgorithmParameterException使用E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    返回false;
}

公共布尔解密(字符串键,InputStream的是,OutputStream的OS)
{
    尝试 {
        byte []的EN codeD =新的BigInteger(键,16).toByteArray();
        SecretKey的seckey =新SecretKeySpec(EN codeD,AES);
        加密器= Cipher.getInstance(AES / CBC / PKCS7Padding);
        encipher.init(Cipher.DECRYPT_MODE,seckey,ⅳ);
        COS =新CipherOutputStream(OS,加密器);
        copyByte(是,COS);
        //cos.close();
        返回true;
    }
    赶上(InvalidKeyException将E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(抛出:NoSuchAlgorithmException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(NoSuchPaddingException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(InvalidAlgorithmParameterException使用E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    返回false;
}

公共无效copyByte(InputStream的是,OutputStream的操作系统)抛出IOException异常
{
    byte []的BUF =新的字节[8192];
    INT的numBytes;
    而((的numBytes = is.​​read(BUF))!= -1)
    {
        os.write(BUF,0,的numBytes);
        os.flush();
    }
    os.close();
    is.close();
}
}
 

解决方案

最后我得到的答案我自己的问题,与试错 其实这里的冲突是我在:编码= Cipher.getInstance(AES / CBC / PKCS7Padding)设置填充;

和第四组的一些价值观.....

最后,我得到了答案才刚刚更换了算法

从:

  

AES / CBC / PKCS7Paddinng

要:

  

AES / CFB8 / NoPadding

和它的工作就像魅力......,所以我建议这个答案的人谁这个问题苦苦挣扎,如果你解决你的问题,在这里提到的其他人...

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

http://stackoverflow.com/a/10775577/1115788

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

my Crypter Code:

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();
}
}

解决方案

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

From:

AES/CBC/PKCS7Paddinng

To:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆