解密文件的内容时返回不同的字节

时间:2017-07-31 16:24:54

标签: java android encryption cryptography

所以我决定扩展我在密码学方面的知识,但是我有以下代码来加密文件的内容,然后解密它。

我的问题是,当我解密内容并比较我拥有的两个字节数组(原始和解密的内容)时,我有两个不同的数组,甚至是内容。 你们中的任何人都可以发现我的代码中可能有什么问题吗?原始内容是基本64位解码字节数组。

public class KeyStoreHelper {

    public static byte[] decryptAES(FileInputStream fis, byte[] key) throws IOException {

        CipherInputStream cin = null;
        try {

            byte[] keyBytes = getKeyBytes(key);
            Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
            aesCipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);

            byte[] buffer = new byte[1024];

            ByteArrayInputStream cipherIn = new ByteArrayInputStream(buffer);
            ByteArrayOutputStream cipherOut = new ByteArrayOutputStream();
            cin = new CipherInputStream(cipherIn, aesCipher);

            int length;
            while ((length = fis.read(buffer)) != -1) {
                cin.read(buffer, 0, length);
                cipherOut.write(buffer);
            }

            return cipherOut.toByteArray();
        } catch (InvalidKeyException | NoSuchAlgorithmException
                | NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cin != null) {
                cin.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return new byte[1];
    }

    public static void encryptAES(FileOutputStream fos, byte[] plainText, byte[] key) throws IOException {

        CipherOutputStream cos = null;
        try {
            byte[] keyBytes = getKeyBytes(key);
            Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
            aesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

            cos = new CipherOutputStream(fos, aesCipher);
            cos.write(plainText);
            cos.flush();

        } catch (InvalidKeyException | NoSuchAlgorithmException
                | NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cos != null) {
                cos.close();
            }
            if (fos != null) {
                fos.flush();
                fos.close();
            }
        }
    }

    private static byte[] getKeyBytes(final byte[] key) throws Exception {
        byte[] keyBytes = new byte[16];
        System.arraycopy(key, 0, keyBytes, 0, Math.min(key.length, keyBytes.length));
        return keyBytes;
    }
}

内容是从服务器下载的文件:

原始字节[] = {48,-126,11,123,...},。length = 2943

decrypted byte [] = {48,-126,11,123,...},。length = 3072(!)

为什么我有这个长度差异?

1 个答案:

答案 0 :(得分:3)

好像你正在处理文件,但实际上你犯了一些错误:

    如果您已经拥有ByteArrayInputStream,则
  • 您不需要FileInputStream;
  • 在写入CipherOutputStream时,您忽略了从输入流中读取的字节数;
  • 您忘记关闭CipherOutputStream

基本上,您确实需要正确处理Java I / O流。