zoukankan      html  css  js  c++  java
  • zip加密-字符串加密成字符串

    转载来的。

    原文本zip加密的时候我会莫名其妙换行,我的原始数据是json数据

    所以我在加密完成的时候 把 都替换了

    如下

    String admin = zip(加密的字符串).replaceAll("
    ","");
    
    
        /**
         * 使用zip进行压缩
         * @param str 压缩前的文本
         * @return 返回压缩后的文本
         */
        public static  String zip(String str) {
            if (str == null)
                return null;
            byte[] compressed;
            ByteArrayOutputStream out = null;
            ZipOutputStream zout = null;
            String compressedStr = null;
            try {
                out = new ByteArrayOutputStream();
                zout = new ZipOutputStream(out);
                zout.putNextEntry(new ZipEntry("0"));
                zout.write(str.getBytes());
                zout.closeEntry();
                compressed = out.toByteArray();
                compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
            } catch (IOException e) {
                compressed = null;
            } finally {
                if (zout != null) {
                    try {
                        zout.close();
                    } catch (IOException e) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                    }
                }
            }
            return compressedStr;
        }
    
        /**
         * 使用zip进行解压缩
         * @param
         * @return 解压后的字符串   compressed压缩后的文本
         */
        public static  String unzip(String compressedStr) {
            if (compressedStr == null) {
                return null;
            }
    
            ByteArrayOutputStream out = null;
            ByteArrayInputStream in = null;
            ZipInputStream zin = null;
            String decompressed = null;
            try {
                byte[] compressed = new sun.misc.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();
            } catch (IOException e) {
                decompressed = null;
            } finally {
                if (zin != null) {
                    try {
                        zin.close();
                    } catch (IOException e) {
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                    }
                }
            }
            return decompressed;
        }
    
  • 相关阅读:
    简单的运动框架——分享给初学者
    Python数据分析学习日志(1. 书单)
    mysql恢复数据参考
    window cmd 杀掉 java.exe 进程
    转载: Ajax关于readyState和status的讨论
    开发问题bug记录
    vue基础part10
    vue基础part9
    vue基础part(7-8)
    vue基础part(4-6)
  • 原文地址:https://www.cnblogs.com/fangyuandoit/p/13713832.html
Copyright © 2011-2022 走看看