Расшифровка не дает никакого вывода в выходной файл - PullRequest
1 голос
/ 03 ноября 2019

Когда я хочу расшифровать зашифрованный файл, выходной файл становится пустым.

Я использовал следующие методы, которые я вызываю decryptFile() в своем методе main. Коды следующие:

private static void decryptFile(String outfile, String fileName, String keyName) throws Exception {
        String original = outfile;
        String encrypted = fileName;
        Cipher cipher;
        byte[] iv = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };
        SecretKey ks = readKey(keyName, "PBKDF2WithHmacSHA1");
        AlgorithmParameterSpec algoSpec = new IvParameterSpec(iv);
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, ks, algoSpec);
        decrypt(new FileInputStream(encrypted), new FileOutputStream(original), cipher);

    }

    private static void decrypt(InputStream input, OutputStream output, Cipher cipher) throws Exception {
        input = new CipherInputStream(input, cipher);
        writeBytes(input, output);
    }

    private static void writeBytes(InputStream input, OutputStream output) throws Exception {
        byte[] writeBuffer = new byte[1024];
        int readBytes = 0;
        while ((readBytes = input.read(writeBuffer)) >= 0) {
            output.write(writeBuffer, 0, readBytes);
        }
        output.close();
        input.close();

    }

    private static SecretKey readKey(String input, String algo) throws Exception {
        FileInputStream fis = new FileInputStream(input);
        int kl = fis.available();
        byte[] kb = new byte[kl];
        fis.read(kb);
        fis.close();
        KeySpec ks = null;
        SecretKey key = null;
        SecretKeyFactory kf = null;
        ks = new DESKeySpec(kb);
        kf = SecretKeyFactory.getInstance(algo);
        key = kf.generateSecret(ks);
        return key;
    }
}

Я ожидаю, что выходной файл будет заполнен расшифрованным текстом, который будет расшифрован с использованием значения ключа, уже сгенерированного и сохраненного в файле с именем keyname в файле с именем outfile, но outfile пусто. В чем может быть проблема? Я новичок в криптографии. Что мне делать?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...