Android AES / CBC / PKCS5Padding加密

时间:2017-09-04 01:21:57

标签: java android encryption cryptography

我正在读取具有加密数据的数据库。我可以阅读解密内容,但我不能解密我加密的东西。结果不同:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
SecretKey skey = new SecretKeySpec(key, 0, key.length, "AES");

以下是我解密字符串的方法:

public byte[] decrypt(byte[] data) {                                                                                                                                                                                                                               
    byte[] result = new byte[0];                                                                                                    
    try {                                                                                                                           
        cipher.init(DECRYPT_MODE, skey, new IvParameterSpec(iv));                                                                   

        InflaterInputStream inflaterStream = new InflaterInputStream(
            new CipherInputStream(new ByteArrayInputStream(data), cipher));
        return IOUtils.toByteArray(inflaterStream);                                                                                 
    } catch (Exception e) {                                                                                                         
        e.printStackTrace();                                                                                                        
        return result;                                                                                                              
    }                                                                                                                               
}

以下是我如何加密字符串:

public byte[] encrypt(byte[] data) {                                                                                                                         
    try {                                                                                                                          
        cipher.init(ENCRYPT_MODE, skey, new IvParameterSpec(iv));                                                                  
        DeflaterInputStream deflaterInput = new DeflaterInputStream(
            new CipherInputStream(new ByteArrayInputStream(data), cipher));

        return IOUtils.toByteArray(deflaterInput);                                                                                 
    } catch (Exception e) {                                                                                                        
        e.printStackTrace();                                                                                                       
        return new byte[0];                                                                                                        
    }                                                                                                                              
} 

我得到的错误是:

W/System.err: java.util.zip.ZipException: incorrect header check
W/System.err:     at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:170)
W/System.err:     at java.io.FilterInputStream.read(FilterInputStream.java:107)
W/System.err:     at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2146)
W/System.err:     at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102)
W/System.err:     at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123)
W/System.err:     at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
W/System.err:     at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:721)

1 个答案:

答案 0 :(得分:3)

您在加密时嵌套流是不正确的。加密时应该是

new CipherInputStream(new DeflaterInputStream(new ByteArrayInputStream(data)), cipher);

解密时应该是

new InflaterInputStream(new CipherInputStream(new ByteArrayInputStream(data), cipher));

你是如何拥有它并且是正确的。