解密后缺少字符 [英] few characters missing after decryption

查看:170
本文介绍了解密后缺少字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我原来的xml:

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

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

This is the output after going through encryption/decryption process

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

如您所见,几乎没有字符丢失。

As you can see, few characters are missing.

这是我的代码。

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");
    }

}

请帮忙。谢谢。

推荐答案

发现问题。 do

        cos.close();

在线之前:

        fos.close();

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

At least, that fixed it on my box.

这篇关于解密后缺少字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆