Java 文件解密不适用于PDF文件

Java 文件解密不适用于PDF文件,java,android,encryption,Java,Android,Encryption,我正在Android中使用AsyncTask下载一个PDF文件。在onPostExecute()方法中,我编写了一段代码对其进行加密。正在存储器中创建加密文件,但未成功解密 我已经检查了加密和解密算法,两种方法都很好。我正在使用CipherInputStream和CipherOutputStream对文件进行加密和解密 我已经尝试在线程中加密文件。它仍然不起作用 public class EncryptionUtils { private final static String ALG

我正在Android中使用
AsyncTask
下载一个PDF文件。在
onPostExecute()
方法中,我编写了一段代码对其进行加密。正在存储器中创建加密文件,但未成功解密

我已经检查了加密和解密算法,两种方法都很好。我正在使用
CipherInputStream
CipherOutputStream
对文件进行加密和解密

我已经尝试在线程中加密文件。它仍然不起作用

public class EncryptionUtils {

    private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG";
    private final static String ALGO_SECRET_KEY_GENERATOR = "AES";
    private final static int IV_LENGTH = 16;

    private static SecretKey encryptionKey;
    private static SecretKey decryptionKey;
    private static AlgorithmParameterSpec paramSpec;

    private final static int DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE = 1024;
    private final static String ALGO_FILE_ENCRYPTER = "AES/CBC/PKCS5Padding";

    @SuppressWarnings("resource")
    public static void encrypt(SecretKey key, AlgorithmParameterSpec paramSpec, InputStream in, OutputStream out)
            throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException {

        try {
            Cipher c = Cipher.getInstance(ALGO_FILE_ENCRYPTER);
            c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            out = new CipherOutputStream(out, c);
            int count = 0;
            byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
            while ((count = in.read(buffer)) >= 0) {
                out.write(buffer, 0, count);
            }
        } finally {
            out.close();
        }
    }

    @SuppressWarnings("resource")
    public static void decrypt(SecretKey key, AlgorithmParameterSpec paramSpec,
                               InputStream in, OutputStream out)
            throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
            InvalidAlgorithmParameterException, IOException {
        try {
            Cipher c = Cipher.getInstance(ALGO_FILE_ENCRYPTER);
            c.init(Cipher.DECRYPT_MODE, key, paramSpec);
            out = new CipherOutputStream(out, c);
            int count = 0;
            byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
            while ((count = in.read(buffer)) >= 0) {
                out.write(buffer, 0, count);
            }
        } finally {
            out.close();
        }
    }

    private static void generateKeys() {
        encryptionKey = null;
        try {
            encryptionKey = KeyGenerator.getInstance(ALGO_SECRET_KEY_GENERATOR).generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] keyData = encryptionKey.getEncoded();

        decryptionKey = new SecretKeySpec(keyData, 0, keyData.length, ALGO_SECRET_KEY_GENERATOR);
        byte[] iv = new byte[IV_LENGTH];
        try {
            SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR).nextBytes(iv);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        paramSpec = new IvParameterSpec(iv);
    }
}

我希望实现的是,当文件被下载时,它应该被成功加密并保存在内部存储器中。如果我愿意,我可以随时解密回来

编辑: 我正在使用像这样的
EncryptionUtils

generateKeys();
final File inputFile = new File(EXTERNAL_STORAGE_PATH + File.separator + RESOURCE_DIRECTORY_NAME + File.separator + pdfSlug + ".pdf");
final File encFile = new File(EXTERNAL_STORAGE_PATH + File.separator + RESOURCE_DIRECTORY_NAME + File.separator +  "enc_file.enc");
try {
EncryptionUtils.encrypt(encryptionKey, paramSpec, new FileInputStream(inputFile), new FileOutputStream(encFile));

} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IOException e) {
                Log.d(TAG, e.getLocalizedMessage());
}

我已经用如何使用加密类的代码更新了问题,只需在设备上已有的pdf文件上尝试您的代码。您应该创建一个函数EncryptionUtils.encryptFile(String srcPath,String destPath)。@blackapps我已经尝试过了。它很好用。已在DeviceC上的文件加密和解密正确。您是否比较了两个加密文件的大小?他们平等吗?每个字节都计数。