解码java中的加密文本文件时出错

时间:2013-12-19 12:49:52

标签: java encryption file-io aes

我用AES算法加密了一个文本文件。我无法解密它。我使用了相同的密钥,整个过程在同一个方法体中运行。 首先,input.txt文件被加密为encrypted.txt文件。然后解码器将encrypted.txt解密为decrypted.txt 这是代码。谢谢你的帮助。

public static void main(String[] args) throws IOException,
        NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException {

    Scanner sc = new Scanner(System.in);
    String filename = sc.nextLine();
    sc.close();

    System.out.println("The file requested is " + filename);

    File file = new File(filename);

    if (file.exists())
        System.out.println("File found");

    File to_b_encf = new File("encrypted.txt");

    if (!to_b_encf.exists())
        to_b_encf.createNewFile();

    System.out.println("encrypting");

    Cipher encipher = Cipher.getInstance("AES");
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    SecretKey key = keygen.generateKey();

    encipher.init(Cipher.ENCRYPT_MODE, key);

    FileOutputStream output = new FileOutputStream(to_b_encf);
    FileInputStream input = new FileInputStream(filename);
    CipherInputStream cis = new CipherInputStream(input, encipher);

    int read;

    while ((read = cis.read()) != -1) {
        output.write(read);
        output.flush();
    }

    input.close();
    output.close();

    System.out.println("done");
    System.out.println("decrypting");

    Cipher decipher = Cipher.getInstance("AES");//initiate a cipher for decryption
    decipher.init(Cipher.DECRYPT_MODE, key);//decrypt the file 

    File sourcefile = new File("encrypted.txt");
    File destfile = new File("decrypted.txt");

    if (!destfile.exists())
        destfile.createNewFile();

    FileInputStream decf = new FileInputStream(sourcefile);
    CipherInputStream c_decf = new CipherInputStream(decf,decipher);
    FileOutputStream destf = new FileOutputStream(destfile);

    cout = new CipherOutputStream(destf,decipher);

    while ((read = c_decf.read()) != -1) {
        cout.write(read);
        cout.flush();
    }

    c_decf.close();
    destf.close();
    cout.close();
    decf.close();
    System.out.println("done");
}

2 个答案:

答案 0 :(得分:2)

你搞砸了InputStreamOutputStream等等。我制作了一个简化的代码版本(没有文件,所有内存中的I / O),说明了主要概念:

public class EncDec {

    public static void main(String[] args) throws IOException
            , InvalidKeyException, NoSuchAlgorithmException
            , NoSuchPaddingException {

        final String MESSAGE = "I'm a secret message";
        final Charset CHARSET = Charset.defaultCharset();

        Cipher cipher = Cipher.getInstance("AES");
        SecretKey key = KeyGenerator.getInstance("AES").generateKey();
        cipher.init(Cipher.ENCRYPT_MODE, key);

        // Encrypt the message
        InputStream plainIn = new ByteArrayInputStream(
                MESSAGE.getBytes(CHARSET));
        ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream();
        copy(plainIn, new CipherOutputStream(encryptedOut, cipher));

        // Decrypt the message
        cipher.init(Cipher.DECRYPT_MODE, key);
        InputStream encryptedIn = new CipherInputStream(
                new ByteArrayInputStream(encryptedOut.toByteArray()), cipher);
        ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
        copy(encryptedIn, plainOut);

        System.out.println(new String(plainOut.toByteArray(), CHARSET));
    }

    private static void copy(InputStream in, OutputStream out)
            throws IOException {
        byte[] buffer = new byte[4096];
        while ( in.read(buffer) > -1) {
            out.write(buffer);
        }
        out.flush();
    }
}

Java I / O API受装饰器模式的启发。加密/解密库提供了一个用于读取加密内容的装饰器CipherInputStream和一个装饰器CipherOutputStream来加密一个普通源并将其写入装饰的输出目的地。

答案 1 :(得分:0)

CipherInputStream c_decf = new CipherInputStream(decf,decipher);
FileOutputStream destf = new FileOutputStream(destfile);

cout = new CipherOutputStream(destf,decipher);

while ((read = c_decf.read()) != -1) {
    cout.write(read);
    cout.flush();
}

看起来你要解密它两次。