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. ListView相关知识点

    最近开发接触到了ListView控件,其实简单的需求基本上源生的都可以满足,下面总结一下开发过程中所遇到的关键点: 1.多级ListView联动,保存位置:即切换第一层ListView的item过程中 ...

  2. asp.net 实现treeview 选中父节点其子节点也选种中 选中子节点其父节点与根节点也被选中

    1.在 Page_Load(object sender, EventArgs e) 里面加入: TreeView1.Attributes.Add("onclick", " ...

  3. 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(一)-----mapState

    一.通过mapState函数的对象参数来赋值: <p>{{ count }}</p> <p>{{ count1 }}</p> <p>{{ c ...

  4. (转)淘淘商城系列——VMware添加已配置好的虚拟机

    http://blog.csdn.net/yerenyuan_pku/article/details/72802323 我们有时候会碰到虚拟机环境搭建特别麻烦,很容易出错的问题,而这时我们又刚好有别人 ...

  5. (function(){})() 立即执行函数

    (function(){})() 立即执行函数 (function(a){})(5) 带参的

  6. 边框带阴影 box-shadow

    .chosen-container-active .chosen-single { border: 1px solid #5897fb; -webkit-box-shadow: 0 0 5px rgb ...

  7. CAD使用SetxDataLong写数据(com接口)

    主要用到函数说明: MxDrawEntity::SetxDataLong 写一个long扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据名称 n ...

  8. AngularJS小练习20170508

    首先可能需要安装npm,并且配置环境. 1.打开Dos(命令提示符).按Windows徽标键+R组合键,输入cmd然后按回车键进入Dos. 2.安装Yeoman.在Dos下输入npm install ...

  9. rsync_ssh

    rsync -av -e "ssh" /data/wwwroot/a1 node2:/data/wwwroot/

  10. Linux环境下c程序的编译和执行

    1 单个文件的编译和执行创建main.c文件,内容如下: #include <stdio.h> #include <stdlib.h> int main(void){ prin ...