1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.util.zip.ZipEntry;
  5. import java.util.zip.ZipInputStream;
  6. import java.util.zip.ZipOutputStream;
  7.  
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10.  
  11. import sun.misc.BASE64Decoder;
  12. import sun.misc.BASE64Encoder;
  13.  
  14. /**
  15. *
  16. * 对字符串进行加解密和加解压
  17. *
  18. */
  19. @SuppressWarnings("restriction")
  20. public class Base64Util {
  21.  
  22. private static Logger log = LoggerFactory.getLogger(Base64Util.class);
  23.  
  24. /**
  25. * 将字符串压缩后Base64
  26. * @param primStr 待加压加密函数
  27. * @return
  28. */
  29. public static String encodeString(String primStr) {
  30. if (primStr == null || primStr.length() == 0) {
  31. return primStr;
  32. }
  33. ByteArrayOutputStream out = null;
  34. ZipOutputStream zout = null;
  35. try{
  36. out = new ByteArrayOutputStream();
  37. zout = new ZipOutputStream(out);
  38. zout.putNextEntry(new ZipEntry("0"));
  39. zout.write(primStr.getBytes("UTF-8"));
  40. zout.closeEntry();
  41. return new BASE64Encoder().encode(out.toByteArray());
  42. } catch (IOException e) {
  43. log.error("对字符串进行加压加密操作失败:", e);
  44. return null;
  45. } finally {
  46. if (zout != null) {
  47. try {
  48. zout.close();
  49. } catch (IOException e) {
  50. log.error("对字符串进行加压加密操作,关闭zip操作流失败:", e);
  51. }
  52. }
  53. }
  54. }
  55.  
  56. /**
  57. * 将压缩并Base64后的字符串进行解密解压
  58. * @param compressedStr 待解密解压字符串
  59. * @return
  60. */
  61. public static final String decodeString(String compressedStr) {
  62. if (compressedStr == null) {
  63. return null;
  64. }
  65. ByteArrayOutputStream out = null;
  66. ByteArrayInputStream in = null;
  67. ZipInputStream zin = null;
  68. String decompressed = null;
  69. try {
  70. byte[] compressed = new BASE64Decoder().decodeBuffer(compressedStr);
  71. out = new ByteArrayOutputStream();
  72. in = new ByteArrayInputStream(compressed);
  73. zin = new ZipInputStream(in);
  74. zin.getNextEntry();
  75. byte[] buffer = new byte[1024];
  76. int offset = -1;
  77. while((offset = zin.read(buffer)) != -1) {
  78. out.write(buffer, 0, offset);
  79. }
  80. decompressed = out.toString("UTF-8");
  81. } catch (IOException e) {
  82. log.error("对字符串进行解密解压操作失败:", e);
  83. decompressed = null;
  84. } finally {
  85. if (zin != null) {
  86. try {
  87. zin.close();
  88. } catch (IOException e) {
  89. log.error("对字符串进行解密解压操作,关闭压缩流失败:", e);
  90. }
  91. }
  92. if (in != null) {
  93. try {
  94. in.close();
  95. } catch (IOException e) {
  96. log.error("对字符串进行解密解压操作,关闭输入流失败:", e);
  97. }
  98. }
  99. if (out != null) {
  100. try {
  101. out.close();
  102. } catch (IOException e) {
  103. log.error("对字符串进行解密解压操作,关闭输出流失败:", e);
  104. }
  105. }
  106. }
  107. return decompressed;
  108. }
  109. }

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. Objective-C 里面的类对象复用小结

    OC 提供了单继承 (Inheritance), Category, Extension, Protocol 这几种基本的类与对象层面的复用机制,作一小结. 在这几个机制中,继承提供了纵向的复用,可以 ...

  2. C#方法参数关键字

    一.params关键字 prams告诉函数的调用者,该函数的参数数量是可变,如果调用函数的参数标识了params关键字,那么我们可以使用逗号分割的参数或者一个数组来作为参数: 1.这里只能是数组,Li ...

  3. Farseer.net轻量级ORM开源框架 V1.x 教程目录

    本篇教程将以Ver 1.x版本进行详细使用讲解 大家有任何疑问可以加入我们的官方QQ群进行讨论.QQ群:116228666 (Farseer.net开源框架交流) 请注明:Farseer.Net 整个 ...

  4. springboot学习笔记(二)

    首先我们来看一看,springboot启动类@RestController//@ResponseBody+@Controller@SpringBootApplicationpublic class H ...

  5. Js 之图片懒加载插件

    一.PC端(lazyload) 1.引入js文件 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.m ...

  6. splice用法解析

    splice()方法算是最强大的数组方法了,它有很多种用法,主要用于删除指定位置的数组项,在指定的位置插入数组项,在指定位置替换数组项,slpice()方法始终都会返回一个数组,该数组包括从原始数组中 ...

  7. c语言 c++ 实现查看本地ip,外网ip, 本地主机名,查看http网址对应的ip

    /******************************************************************************* 作者 :邓中强 Email :1246 ...

  8. 16第一章 ASP.Net编程基础知识

    第一章        ASP.Net编程基础知识 第一章        ASP.Net编程基础知识 本章首先介绍用ASP.Net技术编制服务器端动态网页所需的网络和HTML标记语言方面的有关知识.然后 ...

  9. 2.10.2 section元素

    section元素 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...

  10. Win2008 Server搭建FTP服务器

    首先创建一个专门的FTP用户,当然也可以不创建. 用系统自带的超管用户. 设置用户名和密码.用户下次登陆必须修改密码记得去掉勾选. 在角色里面的WEB服务器找到添加角色服务.我之前有安装IIS. 没有 ...