【问题标题】:Files are truncated while decrypting解密时文件被截断
【发布时间】:2011-04-03 18:23:23
【问题描述】:

我编写了以下代码来使用 java 加密库加密和解密文件。

import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

class Blowfish {
    public static void main(String[] args) throws Exception {
        String s;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Cipher encrypt = Cipher.getInstance("DES");
        Cipher decrypt = Cipher.getInstance("DES");
        System.out.print("Enter the key: ");
        s = br.readLine();
        /*
         * Names of algorithms used "Blowfish" "DES" 64 bit key ie. 8 bytes
         * "AES" key size has to be 16 bytes ie. 128 bits
         */

        byte key[] = new byte[8];
        for (int i = 0; i < s.length() && i < 8; i++)
            key[i] = (byte) s.charAt(i);

        encrypt.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DES"));
        FileInputStream fin = new FileInputStream("test.txt");
        FileOutputStream out = new FileOutputStream("encrypted.p4e");
        CipherOutputStream cout = new CipherOutputStream(out, encrypt);

        int input = 0;
        while ((input = fin.read()) != -1) {
            cout.write(input);
        }

        out.close();
        cout.close();
        System.out.println("Starting the decryption");
        System.out.print("Enter the key: ");
        s = br.readLine();

        byte key2[] = new byte[8];
        for (int i = 0; i < s.length() && i < 8; i++)
            key2[i] = (byte) s.charAt(i);

        decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key2, "DES"));
        fin = new FileInputStream("encrypted.p4e");
        out = new FileOutputStream("test2.txt");
        CipherInputStream in = new CipherInputStream(fin, decrypt);
        input = 0;
        while ((input = in.read()) != -1) {
            out.write(input);
        }
        out.close();
        in.close();
    }
}

但是,当我尝试在一个示例 .txt 文件上对其进行测试时,加密和解密运行时没有错误。但是解密后的文件与原始文件并不完全相同...结尾部分的某些部分被截断

测试文件

用于加密的测试文件..检查 正确性

加密使用 -> pralhad

使用密钥解密后 -> pralhad

用于加密的测试文件..检查

请提出一些解决方案。

【问题讨论】:

    标签: java encryption


    【解决方案1】:

    您需要删除encrypted.p4e 的第一个FileOutputStream 上的out.close()。该流由CipherOutputStream 包装,cout.close() 将处理关闭底层流。通过提前关闭底层流,您将丢失 CipherOutputStream 为当前密码块缓冲的内容。

    FileInputStream fin = new FileInputStream("test.txt");
    FileOutputStream out = new FileOutputStream("encrypted.p4e");
    CipherOutputStream cout = new CipherOutputStream(out, encrypt);
    int input = 0;
    while ((input = fin.read()) != -1) {
        cout.write(input);
    }
    out.close(); // remove this line and it works
    cout.close();
    

    【讨论】:

    • Pralhad,这似乎是正确的答案。如果你还在,请接受。
    【解决方案2】:
    key[i]=(byte)s.charAt(i);
    

    由于 Java 的字符大小是 2 个字节,我认为将 char 转换为 byte 存在问题。一半的信息将被截断。如果您使用前 128 个字符以外的其他字符,则会很苛刻。

    【讨论】:

      【解决方案3】:

      在关闭之前尝试cout 致电flush()

      cout.flush()
      

      【讨论】:

      • close() 进行刷新(参见this thread)。这里的问题是别的。
      猜你喜欢
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2015-12-19
      相关资源
      最近更新 更多