java - 解密后缺少几个字符

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

}

请帮忙。谢谢。

最佳答案

发现问题。做

        cos.close();

行前:

        fos.close();

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

关于java - 解密后缺少几个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1219377/

相关文章:

ssl - webroot 不会检测 Let's Encrypt 证书创建的根路径

ssl - 是否可以使用带有服务器证书和客户端密码的 SSL?

java - Java 8 Streams Filter 和 Collect 是否返回对列表中相同对象的引用?

python - 解密算法问题

java - Spring MVC 并显示进程中的验证

java - Hystrix 请求缓存示例

vb.net - 反编译 vb.net 应用程序

encryption - 发送发布请求时在表单数据中隐藏密码

java - 错误: The type T cannot be used in GenericTypeMatcher - Expression expected

java - Android 将 Json 对象添加到 google 标记