解密后几个字符丢失

时间:2009-08-02 17:47:33

标签: java encryption

这是我原来的xml:

<?xml version="1.0" encoding="UTF-8"?>
<table>
    <row>
        <id>12</id>
        <name>Mickey Mouse</name>
    </row>
</table>

这是经过加密/解密过程后的输出

<?xml version="1.0" encoding="UTF-8"?>
<table>
    <row>
        <id>12</id>
        <name>Mickey Mouse</name>
    </row>
</

如您所见,缺少一些字符。

这是我的代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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

public class Decrypter
{

    /**
     * @param args
     * @throws IOException
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static void main(String[] args) throws IOException,
        NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
    // TODO Auto-generated method stub
    File iFile = new File("normal.xml");
    FileInputStream fis = new FileInputStream(iFile);

    File oFile = new File("normal.xml.encrypted");
    FileOutputStream fos = new FileOutputStream(oFile);

    String algorithm = "DESede";
    byte[] keyBytes = new byte[] { 0x34, 0x11, 0x12, 0x06, 0x34, 0x11,
        0x12, 0x06, 0x34, 0x11, 0x12, 0x06, 0x34, 0x11, 0x12, 0x06,
        0x34, 0x11, 0x12, 0x06, 0x34, 0x11, 0x12, 0x06 };

    SecretKeySpec key = new SecretKeySpec(keyBytes, algorithm);

    // generates encrypted output from normal.xml.
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);

    int b;
    while ((b = fis.read()) != -1)
    {
        cos.write(b);
    }

    fos.close();
    fos = null;
    fis.close();
    fis = null;

    System.out.println("done");

    // decrypt encrypted xml to normal xml. 
    File ieFile = new File("normal.xml.encrypted");
    FileInputStream fies = new FileInputStream(ieFile);

    Cipher ieCipher = Cipher.getInstance(algorithm);
    ieCipher.init(Cipher.DECRYPT_MODE, key);
    CipherInputStream cis = new CipherInputStream(fies, ieCipher);

    File oeFile = new File("normal.xml.encrypted.xml");
    FileOutputStream foes = new FileOutputStream(oeFile);

    int c;
    while ((c = cis.read()) != -1)
    {
        foes.write(c);
    }

    foes.close();
    cis.close();
    fies.close();

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

}

请帮忙。感谢。

4 个答案:

答案 0 :(得分:8)

发现问题。做

        cos.close();

行之前:

        fos.close();

至少,把它固定在我的盒子上。

答案 1 :(得分:5)

写入加密输出后,调用

cos.close();

而不是

fos.close();

关闭cos对象也会关闭底层的fos对象。

答案 2 :(得分:1)

尝试关闭(或至少刷新)CipherOutputStreamcos),因为可能有一些字节从未写入原始加密文件。

答案 3 :(得分:0)

同意JASON。您必须关闭密码流而不是文件。即使您可以显式关闭两者,但请确保先关闭密码流,然后再关闭文件流。只有冲洗无济于事。至少它不适合我。 :)