AES 解密后 进行ZIP解压还原问题

m583210969 2014-11-05 04:08:17
项目中 从webservice 得到了一串BASE64的码 是通过先通过ZIP压缩,然后AES加密,最后BASE64 形成的。


tn63IpL/zfnztSfQDON0GpQlGzMn+zy2RgxEm+XEUsqOuSB9bZJkaCI/YCkJY/j8pkfE/Gv0/cySYq1yf/rSjX8T04O04cBZgyAsmNcuMV7yQYMDgViHith6zYTd8H+C3PlFlyZCmjGXiBs8zdE1GYkuXvXm1GRTVieM0mkZVOImUVNoX5rK167Xmt9TfzG+Gpva1gbTkfqgsxvp1EKsVfZXe/9BE8BGuWqC3GiAOnW1Bcv9CMa3OFVkZ7qHyht6yrewn/tJgHKAKw3C/PTKMu/+Mh/9Pc8FjwpKOyb0Q+VEV+j2NLJaHnNEme+u7+gOs1mtSWd0IVBcXMo8h4Tszk7EbFi+NOkIZvNlxxnGL5Zh5/QUDaJtLCWwU4TBQ74IUNAvah2lRhToMU5ObTij/RwIAQIPYXmyOftn+G3CUGbIfCg5Dg+ibEblZj3j0fMTtKeZpIO6vJQezlHQqXj/8wljPqwrvhYdMxjmAGbkxxU=


就是上面这段。

然后我这边需要通过 AES 解密后 进行ZIP解压。还原出数据。


/ZIP解压
public static final String unzip(String compressedStr) {
if (compressedStr == null) {
return null;
}

ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed = null;
try {
//解开BASE64编码
byte[] compressed = new sun.misc.BASE64Decoder()
.decodeBuffer(compressedStr);

System.out.println(compressed);
//解密
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom("QMQWLBKK64WUS1W6".getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(compressed);
//解密结束


out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(result);
zin = new ZipInputStream(in);
while(zin.getNextEntry() != null){
byte[] buffer = new byte[result.length];
int offset = -1;
while ((offset = zin.read(buffer,0,buffer.length)) != -1) {
out.write(buffer, 0, offset);
}
}

decompressed = out.toString();
} catch (Exception e) {
e.printStackTrace();
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;
}


这是解密 解压的代码,但是结果是什么都解压不出来,百度 GOOGLE 翻了一大堆,还是不行。想问哪位大神写过相关代码,
帮看看这段代码 问题在哪?谢谢了。
...全文
150 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧