Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解密使用加密流的转换器保存的xml文件?_Java_Xml_Encryption - Fatal编程技术网

Java 如何解密使用加密流的转换器保存的xml文件?

Java 如何解密使用加密流的转换器保存的xml文件?,java,xml,encryption,Java,Xml,Encryption,我必须使用以下代码(我从以前的工作人员那里继承)来创建xml文件: DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(CryptoHandler.encryptedStream(new FileOutputStream(fileName))); transformer.transform(source, result); 其中encryptedStream返回一个CipherOutputS

我必须使用以下代码(我从以前的工作人员那里继承)来创建xml文件:

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(CryptoHandler.encryptedStream(new FileOutputStream(fileName)));
transformer.transform(source, result);
其中encryptedStream返回一个CipherOutputStream。我想我可以使用以下代码来解密此文件:

InputSource inputSource = new InputSource(CryptoHandler.decryptedStream(new FileInputStream(fileName)));
xmlReader.setContentHandler(contentHandler.newInstance());
xmlReader.parse(inputSource);
其中decryptedStream()使用以前使用的密钥,但我得到一个SAXParseException:在第一个字符处prolog中不允许内容

更新#1:

验证加密和解密的代码:

CipherOutputStream cos = null;
CipherInputStream cis = null;
String text = "Some text to encryptSome text to encrypt";
String filePath = "D:/test.txt";
String result = "";        

cos = CryptoHandler.encryptedStream(new FileOutputStream(filePath));
cos.write(text.getBytes());
cos.flush();
cos.close();

cis = CryptoHandler.decryptedStream(new FileInputStream(filePath));
byte[] buffer = new byte[text.getBytes().length];
while (cis.read(buffer) != -1) {
    result += new String(buffer);
}

Assert.assertEquals(text, result);
更新#2:


我发现上面的测试是可以的,但仅对于这个特定的字符串,如果我将其长度更改为不同的值,解密后的末尾将丢失一些字节(从4到基本上插入的大部分数据)。虽然在一些特殊的长度上它可以工作(2,4,8,16,24,40,80)。此加密方法使用PBEWithMD5和DES密码。

检查
CryptoHandler.decryptedStream(CryptoHandler.encryptedStream(someStream))==someStream
。“prolog中不允许内容”表示xml读取器的输入在一开始就包含一些垃圾,第一个怀疑是解码和编码没有正确配对。“CryptoHandler.decryptedStream()”需要一个InputStream,而“CryptoHandler.encryptedStream()”需要一个OutputStream。我有一个测试,我已经把它放在更新1中了。