base64加解密字符串
- 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加解密字符串的更多相关文章
- django删除表重建&修改用户密码&base64加密解密字符串&ps aux参数说明&各种Error例子
1.django的queryset不支持负索引 AssertionError: Negative indexing is not supported. 2.django向前端JavaScript传递列 ...
- 学习Java AES加解密字符串和文件方法,然后写个简单工具类
Reference Core Java Volume Ⅱ 10th Edition 1 对称加密 "Java密码扩展"包含了一个Cipher,它是所有密码算法的超类.通过getIn ...
- java base64加解密
接上篇java Base64算法. 根据之前过程使用base64加解密,所以写成了工具类. 代码示例; public class Base64Util { private static Logger ...
- QuickBase64 - Android 下拉通知栏快捷base64加解密工具
Android Quick Setting Tile Base64 Encode/Decode Tool Android 下拉通知栏快捷 base64 加解密,自动将剪切板的内容进行 base64 E ...
- 对字符串进行base64加解密---基于python
本文介绍Python 2.7中的base64模块,该模块提供了基于rfc3548的Base16, 32, 64编解码的接口.官方文档,参考这里. 当前接口基于rfc3548的Base16/32/64编 ...
- JAVA加解密 -- Base64加解密
Base64算法实现:可以将任意的字节数组数据,通过算法,生成只有(大小写英文.数字.+./)(一共64个字符)内容表示的字符串数据. private static final String str ...
- Java 使用AES/CBC/PKCS7Padding 加解密字符串
介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别要实现在java端用PKCS7Padding填充, ...
- python base64加解密
加密字符串 encodestr = base64.b64encode("chenglee1234".encode(encoding='utf-8')) 解密字符串 decodest ...
- oracle里面base64加解密
1. base64 的解密函数select utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw('dGVzdA= ...
随机推荐
- Objective-C 里面的类对象复用小结
OC 提供了单继承 (Inheritance), Category, Extension, Protocol 这几种基本的类与对象层面的复用机制,作一小结. 在这几个机制中,继承提供了纵向的复用,可以 ...
- C#方法参数关键字
一.params关键字 prams告诉函数的调用者,该函数的参数数量是可变,如果调用函数的参数标识了params关键字,那么我们可以使用逗号分割的参数或者一个数组来作为参数: 1.这里只能是数组,Li ...
- Farseer.net轻量级ORM开源框架 V1.x 教程目录
本篇教程将以Ver 1.x版本进行详细使用讲解 大家有任何疑问可以加入我们的官方QQ群进行讨论.QQ群:116228666 (Farseer.net开源框架交流) 请注明:Farseer.Net 整个 ...
- springboot学习笔记(二)
首先我们来看一看,springboot启动类@RestController//@ResponseBody+@Controller@SpringBootApplicationpublic class H ...
- Js 之图片懒加载插件
一.PC端(lazyload) 1.引入js文件 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.m ...
- splice用法解析
splice()方法算是最强大的数组方法了,它有很多种用法,主要用于删除指定位置的数组项,在指定的位置插入数组项,在指定位置替换数组项,slpice()方法始终都会返回一个数组,该数组包括从原始数组中 ...
- c语言 c++ 实现查看本地ip,外网ip, 本地主机名,查看http网址对应的ip
/******************************************************************************* 作者 :邓中强 Email :1246 ...
- 16第一章 ASP.Net编程基础知识
第一章 ASP.Net编程基础知识 第一章 ASP.Net编程基础知识 本章首先介绍用ASP.Net技术编制服务器端动态网页所需的网络和HTML标记语言方面的有关知识.然后 ...
- 2.10.2 section元素
section元素 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...
- Win2008 Server搭建FTP服务器
首先创建一个专门的FTP用户,当然也可以不创建. 用系统自带的超管用户. 设置用户名和密码.用户下次登陆必须修改密码记得去掉勾选. 在角色里面的WEB服务器找到添加角色服务.我之前有安装IIS. 没有 ...