zoukankan      html  css  js  c++  java
  • 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;  
        }  
    }
  • 相关阅读:
    There is no Action mapped for namespace [/pages/action/student] and action name [findStudent]
    站点设计至尊 (展示文)
    【翻译自mos文章】DBA_JOBS 和 DBA_JOBS_RUNNING 不同的结果的解释
    Effective C++ 条款45
    NYOJ109 数列转换 【守恒法】
    Windows 上安装 pip
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0
    解决Eclipse 项目报错:Unbound classpath container: ‘JRE System Library [JavaSE-1.7]
    neuroph Perceptron Sample
    Visio 画图去掉页边距(图形四周的空白区域)的解决办法
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6844340.html
Copyright © 2011-2022 走看看