碰到的一个编码问题
系统有两个子系统,一个是BS的,一个是delphi做的CS,中间的数据传输是通过XML进行传输的。在XML传输的功能实现后,要求对XML进行加密解密.加密解密算法是CS端用delphi写的,然后这边用JAVA写个同样的算法。现在碰到的一个问题是:
用该算法的时候,CS和BS各自都能够加解密,我这边的过程是这样的。我这边解密的过程是这样的,首先获取到CS那边的加密文件,然后通过
StringBuffer strbuf = new StringBuffer();try {FileInputStream in = new FileInputStream(file);int size = 0;byte [] buf = new byte[1024];while ((size=in.read(buf)) != -1) {strbuf.append(new String(buf,0,size));}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} return strbuf; 在得到返回的StringBuffer的基础上,对StringBuffer进行解密操作。
StringBuffer xmlStr=new StringBuffer(""); xmlStr.append(encode.UnEncryptString(readBuf,key)); 然后在得到解密后的字符串写入XML文件
有两种方式,一种是写入普通 ,另外一种是采用dom4j提供的字符串转XML的方式
Document doc = DocumentHelper.parseText(xmlStr.toString()); OutputFormat format = OutputFormat.createPrettyPrint(); FileOutputStream writefile=new FileOutputStream(importFile);//输出流,用来接受生成的xml表示的doc format.setEncoding("UTF-8"); XMLWriter writer=new XMLWriter(writefile,format); writer.write(doc); 最终不管我采用什么方式结论是中文始终是乱码。
我想问的问题是
1. 如果双方导入导出的都是标准的XML文件(UTF-8),在此基础上进行加密,
那么在以后读取与写入文件(无论是字节流还是字符流或其他的)过程中,是不是都不涉及到转的 问题。
2.在此过程中,正常的情况编码可能需要变化的是什么地方。
3.一个前提是算法没有问题,单独的不涉及文件存储,双方是可以互加解密。StringBuffer UnEncryptString(StringBuffer Source, StringBuffer Key) {// 对字符串解密(Src:源 Key:密匙)int KeyLen;int KeyPos;int offset;StringBuffer dest = new StringBuffer();int SrcPos;int SrcAsc;int TmpSrcAsc;KeyLen=Key.length();if (KeyLen == 0) {Key = new StringBuffer("XiongJunJie");}KeyPos = 0;offset = Integer.parseInt(Source.substring(0, 2), 16);SrcPos = 2;byte[] keybyte = Key.toString().getBytes();do {SrcAsc = Integer.parseInt(Source.substring(SrcPos, SrcPos + 2), 16);if (KeyPos < KeyLen)KeyPos = KeyPos + 1;elseKeyPos = 1;TmpSrcAsc = SrcAsc ^ keybyte[KeyPos - 1];if (TmpSrcAsc <= offset)TmpSrcAsc = 255 + TmpSrcAsc - offset;elseTmpSrcAsc = TmpSrcAsc - offset;if (TmpSrcAsc > 128) {offset = SrcAsc;int temp = TmpSrcAsc;SrcPos = SrcPos + 2;SrcAsc = Integer.parseInt(Source.substring(SrcPos, SrcPos + 2),16);if (KeyPos < KeyLen)KeyPos = KeyPos + 1;elseKeyPos = 1;TmpSrcAsc = SrcAsc ^ keybyte[KeyPos - 1];if (TmpSrcAsc <= offset)TmpSrcAsc = 255 + TmpSrcAsc - offset;elseTmpSrcAsc = TmpSrcAsc - offset;byte[] bytes = new byte[] { (byte) temp, (byte) TmpSrcAsc };dest = dest.append(new String(bytes));} elsedest = dest.append((char) (TmpSrcAsc));offset = SrcAsc;SrcPos = SrcPos + 2;} while (SrcPos < Source.length() - 1);return dest;}String format(String s) {String s1 = null;int len = s.length();if (len == 0)s1 = "00";else if (len == 1)s1 = "0" + s;else if (len == 2)s1 = s;return s1;}
有人说不能用StringBuffer,所以正在改位取字节进行解密。用BS这边的加密解密,自己加的自己解是没问题的。 4 楼 Qieqie 2007-08-17 http://www.iteye.com/topic/113572 5 楼 cnng007 2007-08-19 FileInputStream in = new FileInputStream(file);
int size = 0;
byte [] buf = new byte[1024];
while ((size=in.read(buf)) != -1) {
strbuf.append(new String(buf,"UTF-8"));
} 6 楼 sea7 2007-08-19 new String(buf,0,size);
就会按本地字符集编码了