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; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; /**
*
* 对字符串进行加解密和加解压
*
*/
@SuppressWarnings("restriction")
public class Base64Util { private static Logger log = LoggerFactory.getLogger(Base64Util.class); /**
* 将字符串压缩后Base64
* @param primStr 待加压加密函数
* @return
*/
public static String encodeString(String primStr) {
if (primStr == null || primStr.length() == 0) {
return primStr;
}
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try{
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(primStr.getBytes("UTF-8"));
zout.closeEntry();
return new BASE64Encoder().encode(out.toByteArray());
} catch (IOException e) {
log.error("对字符串进行加压加密操作失败:", e);
return null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
log.error("对字符串进行加压加密操作,关闭zip操作流失败:", e);
}
}
}
} /**
* 将压缩并Base64后的字符串进行解密解压
* @param compressedStr 待解密解压字符串
* @return
*/
public static final String decodeString(String compressedStr) {
if (compressedStr == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed = null;
try {
byte[] compressed = new BASE64Decoder().decodeBuffer(compressedStr);
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString("UTF-8");
} catch (IOException e) {
log.error("对字符串进行解密解压操作失败:", e);
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
log.error("对字符串进行解密解压操作,关闭压缩流失败:", e);
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
log.error("对字符串进行解密解压操作,关闭输入流失败:", e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.error("对字符串进行解密解压操作,关闭输出流失败:", e);
}
}
}
return decompressed;
}
}

base64加解密字符串的更多相关文章

  1. django删除表重建&修改用户密码&base64加密解密字符串&ps aux参数说明&各种Error例子

    1.django的queryset不支持负索引 AssertionError: Negative indexing is not supported. 2.django向前端JavaScript传递列 ...

  2. 学习Java AES加解密字符串和文件方法,然后写个简单工具类

    Reference Core Java Volume Ⅱ 10th Edition 1 对称加密 "Java密码扩展"包含了一个Cipher,它是所有密码算法的超类.通过getIn ...

  3. java base64加解密

    接上篇java Base64算法. 根据之前过程使用base64加解密,所以写成了工具类. 代码示例; public class Base64Util { private static Logger ...

  4. QuickBase64 - Android 下拉通知栏快捷base64加解密工具

    Android Quick Setting Tile Base64 Encode/Decode Tool Android 下拉通知栏快捷 base64 加解密,自动将剪切板的内容进行 base64 E ...

  5. 对字符串进行base64加解密---基于python

    本文介绍Python 2.7中的base64模块,该模块提供了基于rfc3548的Base16, 32, 64编解码的接口.官方文档,参考这里. 当前接口基于rfc3548的Base16/32/64编 ...

  6. JAVA加解密 -- Base64加解密

    Base64算法实现:可以将任意的字节数组数据,通过算法,生成只有(大小写英文.数字.+./)(一共64个字符)内容表示的字符串数据. private static final String str ...

  7. Java 使用AES/CBC/PKCS7Padding 加解密字符串

    介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别要实现在java端用PKCS7Padding填充, ...

  8. python base64加解密

    加密字符串 encodestr = base64.b64encode("chenglee1234".encode(encoding='utf-8')) 解密字符串 decodest ...

  9. oracle里面base64加解密

    1. base64 的解密函数select utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw('dGVzdA= ...

随机推荐

  1. 12. binary search Trees

    12. binary search Trees    The search tree data structure supports many dynamic-set operations,inclu ...

  2. php判断是否引入某文件

    Code: /* 判断是否引入了公共文件demo.php */ $include_files = get_included_files(); $include_files_exist = 0 ; fo ...

  3. JSP的有哪些内置对象,作用分别是什么?

    request:表示HttpServletRequest对象,它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie,header和session数据的有用方法: response:表示Ht ...

  4. [转]c++应用程序文件的编译过程

    原文地址 这里讲下C++文件的编译过程及其中模板的编译过程: 一:一般的C++应用程序的编译过程.    一般说来,C++应用程序的编译过程分为三个阶段.模板也是一样的. 在cpp文件中展开inclu ...

  5. HTML5 history API与ajax分页实例页面

    <ul id="choMenu" class="rel cho_menu"> <li><a href="ajax.php ...

  6. ie兼容的解决办法,6,7、8、9、10

    在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html> ...

  7. 关于统一代码规范ResultBean<T>

    之前看了一篇文章,是java团长的一篇代码规范的文章,很有启发.统一返回格式确实给开发带来方便和美感, 有时候Colltroller返回String或者什么Map,list什么的,虽然都转成json返 ...

  8. ubuntu(linux)占领小米平板2(mipad2)

    昨天 2014年,媳妇坐月子,给媳妇买了mi6和一个小米平板2(16G).是我们人生拥有的第一个平板,激动不已. 买之前看了小米平板1的口碑不错,arm构架,NVIDIA的主板好像,图形处理做得当然没 ...

  9. win7右键菜单不见解决办法

    直接 开始 运行: cmd /k reg add "HKEY_CLASSES_ROOT\Directory\Background\shellex\ContextMenuHandlers\Ne ...

  10. Uva 1572 自组合

    贴个源码// UVa1572 Self-Assembly // Rujia Liu #include<cstdio> #include<cstring> #include< ...