使用 AES 的 Android Lollipop 解密无法正常工作

标签 android encryption aes android-5.0-lollipop

在 kitkat 之前,加密/解密运行良好,但在 Lollipop 中它只能解密部分数据。

我在加密方面没有问题,因为我用 Lollipop 加密了一个文件并用 kitkat 解密它,它工作正常,但反之则不然。

这是代码。

加密代码

Encrypt(BufferedInputStream is, File destfile, String passcode) {
        bis = is;
        try {
            fos = new FileOutputStream(destfile);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dest = new BufferedOutputStream(fos, 1024);
        this.passcode = passcode;
    }

    static void encrypt() throws IOException, NoSuchAlgorithmException,
    NoSuchPaddingException, InvalidKeyException {

        // Length is 16 byte
        SecretKeySpec sks = new SecretKeySpec(passcode.getBytes(), "AES");

        // Create cipher
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, sks);
        // Wrap the output stream
        CipherOutputStream cos = new CipherOutputStream(fos, cipher);
        // Write bytes
        int b;
        byte[] d = new byte[1024];
        while ((b = bis.read(d)) != -1) {
            cos.write(d, 0, b);
        }
        // Flush and close streams.
        cos.flush();
        cos.close();
        bis.close();
    }

解密代码

public Decrypt(String path, String pathcode) {
        // TODO Auto-generated constructor stub
        filepath = path;
        try {
            fis = new FileInputStream(new File(path));
            this.passcode = pathcode;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    static String decrypt() throws IOException, NoSuchAlgorithmException,
    NoSuchPaddingException, InvalidKeyException {

        SecretKeySpec sks = new SecretKeySpec(passcode.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, sks);
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        int size = fis.available();
        byte[] resdata = new byte[size];
        cis.read(resdata, 0, size);
        String newres = new String(resdata, "UTF-8").trim();
        //write("decrypted_file.xhtml",newres);  
        if(fis!=null)
        {
        fis.close();
        }
        if(cis!=null)
            cis.close();
        return newres;
    }

这段代码有什么问题?我还需要做些什么吗?

最佳答案

available() 不一定返回整个流的长度,只是返回可以无阻塞读取的估计字节数。因此,使用 ByteArrayOutputStream 来存储字节,然后转换为字节数组:

CipherInputStream cis = new CipherInputStream(fis, cipher);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int bytesRead;
byte[] data = new byte[1024];
while ((bytesRead = cis.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, bytesRead);
}
buffer.flush();
byte[] resdata = buffer.toByteArray();
String newres = new String(resdata, "UTF-8").trim();

关于使用 AES 的 Android Lollipop 解密无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29642271/

相关文章:

java - 在多个合作伙伴预加载后,我们如何跟踪使用我们的 Android 应用程序的设备数量?

python - 在 Python 中计算十六进制的 MD5

java - 提取文件中的值并将其转换为字符串以获得更多功能

MYSQL SELECT WHERE LIKE WITH AES_ENCRYPT

java - 每次加密使用随机盐时,我可以避免每次加密/解密调用的密码重新初始化吗?

c# - TCL AES <-> C# AES - Tcl 加密文本为 ASCII

android - 检测安装的应用程序

java - Android Studio - 应用程序中使用的库项目如何遵循 app-release.apk

java - 如何正确使用 "PBEWithHmacSHA512AndAES_256"算法?

java - 如何在 Androidx 中更改首选项文本颜色