Different bytes are returned when decrypt file's content

杀马特。学长 韩版系。学妹 提交于 2019-12-21 06:40:10

问题


So I decided to expand my knowledge in cryptography, but I have the following code to encrypt a file's content and later decrypt it.

My problem is, when I decrypt the content and compare the two byte array what I have (original and decrypted content) I have two different arrays in length and even in content. Could any of you spot what could be the wrong in my code? The original content is a base 64 decoded byte array.

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

the content is a file downloaded from the server:

original byte[] = {48,-126,11,123,...}, .length = 2943

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

Why am I having this length difference?


回答1:


It seems like you are processing the file, but actually you make a few mistakes:

  • you don't need a ByteArrayInputStream if you already have a FileInputStream;
  • you are ignoring the amount of bytes read from the input stream when writing to the CipherOutputStream;
  • you forget to close the CipherOutputStream.

Basically you really need to handle the Java I/O streams correctly.



来源:https://stackoverflow.com/questions/45420936/different-bytes-are-returned-when-decrypt-files-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!