密文(其实也不算密文,就是zip之后的base64串)是对手系统给过来的报文demo里面的,我拿出来用自己的方法能解出来明文,但是用同样的方法加出来的密文发过去之后对方死活解不出来。所以我又把相同的明文又加了一边密发现我自己的加密密文跟对方demo里面的密文不一样,连长度都差了很多,但是明文是一样的。刚开始我用eclipse,发现我eclipse的编码格式是GBK,所以又改成用idea,把编码格式改成UTF-8之后还是老样子,仍然加出来的密文不一样。因为base64我没有深入研究过,以为是不同设备加出来的密文有可能不一样,又叫同事用他的电脑加出来一份密文,发现我俩加出来的密文是一摸一样的,所以有没有研究过base64的大佬来解释一下是怎么回事。
上代码
package ZipUtil;
import org.apache.axiom.om.util.Base64;
public class main {
public static void main(String args[]) throws Exception {
String data = "UEsDBBQACAgIAL10b00AAAAAAAAAAAAAAAADAAAAUlBDsylILErMLbazySsuKk7KsHu+a/PzFX1Pdu16sb7tacfqp/3b3+/pebq/9eneqTb6UDU2+lBNAFBLBwgqiVDyNwAAAD0AAABQSwECFAAUAAgICAC9dG9NKolQ8jcAAAA9AAAAAwAAAAAAAAAAAAAAAAAAAAAAUlBDUEsFBgAAAAABAAEAMQAAAGgAAAAAAA==";
System.out.println("密文:"+ data);
byte[] data_ys = Base64.decode(data);
String data_zm = ZipUtil.decompress(data_ys, "UTF-8");
System.out.println("明文:"+ data_zm);
data_ys = ZipUtil.compress(data_zm);
data_zm = Base64.encode(data_ys);
System.out.println("密文:"+ data_zm);
data_ys = Base64.decode(data_zm);
data_zm = ZipUtil.decompress(data_ys, "UTF-8");
System.out.println("明文:"+ data_zm);
}
}
package ZipUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
/**
* 压缩字符串为 byte[] 储存可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)方法
* 保存为字符串
*
* @param str 压缩前的文本
* @return
*/
public static final byte[] compress(String str) {
if (str == null) {
return null;
}
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch (IOException e) {
compressed = null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return compressed;
}
/**
* 将压缩后的 byte[] 数据解压缩
*
* @param compressed 压缩后的 byte[] 数据
* @return 解压后的字符串
*/
public static final String decompress(byte[] compressed, String encode) {
if (compressed == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString(encode);
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}
//解压byte[]返回byte[]
public static final byte[] decompress2(byte[] compressed) {
if (compressed == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
byte[] decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toByteArray();
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}
//压缩byte[] 返回byte[]
public static final byte[] compress2(byte[] str) {
if (str == null) {
return null;
}
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str);
zout.closeEntry();
compressed = out.toByteArray();
} catch (IOException e) {
compressed = null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return compressed;
}
}
运行结果:
密文:UEsDBBQACAgIAL10b00AAAAAAAAAAAAAAAADAAAAUlBDsylILErMLbazySsuKk7KsHu+a/PzFX1Pdu16sb7tacfqp/3b3+/pebq/9eneqTb6UDU2+lBNAFBLBwgqiVDyNwAAAD0AAABQSwECFAAUAAgICAC9dG9NKolQ8jcAAAA9AAAAAwAAAAAAAAAAAAAAAAAAAAAAUlBDUEsFBgAAAAABAAEAMQAAAGgAAAAAAA==
明文:纳税人识别号,必录
密文:UEsDBBQACAgIAABJQVIAAAAAAAAAAAAAAAABAAAAMLMpSCxKzC22s8krLipOyrB7vmvz8xV9T3bterG+7WnH6qf929/v6Xm6v/Xp3qk2+lA1NvpQTQBQSwcIKolQ8jcAAAA9AAAA
明文:纳税人识别号,必录