解密加密文件会破坏文件? Android - Java

时间:2015-10-13 04:04:32

标签: java android encryption cryptography

我使用以下代码加密/解密:

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


public class CryptoUtils {
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";

    public static void encrypt(String key, File inputFile, File outputFile)
            throws CryptoException {
        doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
    }

    public static void decrypt(String key, File inputFile, File outputFile)
            throws CryptoException {
        doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
    }

    private static void doCrypto(int cipherMode, String key, File inputFile,
                                 File outputFile) throws CryptoException {
        try {
            Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PAdding");
            cipher.init(cipherMode, secretKey);

            FileInputStream inputStream = new FileInputStream(inputFile);
            byte[] inputBytes = new byte[(int) inputFile.length()];
            inputStream.read(inputBytes);

            byte[] outputBytes = cipher.doFinal(inputBytes);

            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);

            inputStream.close();
            outputStream.close();

        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException
                | InvalidKeyException | BadPaddingException
                | IllegalBlockSizeException | IOException ex) {
            throw new CryptoException("Error encrypting/decrypting file", ex);
        }
    }

    public static class CryptoException extends Exception {

        public CryptoException() {
        }

        public CryptoException(String message, Throwable throwable) {
            super(message, throwable);
        }
    }
}

我可以加密和解密视频文件。 但是,我无法播放由相同代码加密的解密文件。

原始文件:http://www.megafileupload.com/hSTZ/a.mp4

加密文件:http://www.megafileupload.com/hSU2/encrypted-a.mp4

解密文件:http://www.megafileupload.com/hSU1/decrypted-final-a.mp4

解密文件的前16个字节与原始文件不同。长度是一样的。

1 个答案:

答案 0 :(得分:0)

你假设:

  1. 该文件适合内存。
  2. 文件大小适合int。
  3. read()填充缓冲区。
  4. 加密或解密的结果与源内容一起适合内存。
  5. 不需要任何这些假设。

    FileInputStream inputStream = new FileInputStream(inputFile);
    FileOutputStream outputStream = new FileOutputStream(outputFile);
    byte[] buffer = new byte[8192];
    int count;
    while ((count = in.read(buffer)) > 0)
    {
        byte[] output = cipher.update(buffer, 0, count);
        outputStream.write(output);
    }
    outputStream.write(cipher.doFinal());
    outputStream.close();
    inputStream.close();
    

    E& OE但你明白了。使用CipherInput/OutputStreams,可以让您的生活变得更简单,但是您会因此而忽略加密异常。