Android javax.io.IOException:解密时最后一个块不完整

时间:2015-04-17 01:56:51

标签: java android encryption ioexception fileinputstream

我正在尝试制作加密和解密应用,但在解密方法中,我收到了javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整

byte[] key = ("Sh").getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);

secretKeySpec = new SecretKeySpec(key, "AES");
cipher = Cipher.getInstance("AES");

public void dec (String dir)
{       
    try{            
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);            
        String cleartextFile = dir;         
        FileInputStream fis = new FileInputStream(cleartextFile);                   
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        FileOutputStream fos = new FileOutputStream(dir);

        block = new byte[6];
        while ((i = cis.read(block)) != -1) {
            fos.write(block, 0, i);
        }
        fos.close();        
    }
    catch(Exception ex)
    {
        Toast.makeText(MainActivity.this, "Chiper Error"+ex, Toast.LENGTH_LONG).show();
    }
}

0 个答案:

没有答案