zoukankan      html  css  js  c++  java
  • md5和base64加密解密

    import java.io.IOException;
    import java.security.MessageDigest;
    
    import sun.misc.BASE64Encoder;
    import sun.misc.BASE64Decoder;
    
    
    public class MD5Util {
        /**
         * MD5加密
         */
         public static String md5Encryption(String str)  {
              MessageDigest md5 = null;   
              try {   
               md5 = MessageDigest.getInstance("MD5");   
              } catch (Exception e) {   
               System.out.println(e.toString());   
               e.printStackTrace();   
               return "";   
              }   
              char[] charArray = str.toCharArray();   
              byte[] byteArray = new byte[charArray.length];   
              
              for (int i = 0; i < charArray.length; i++)   
               byteArray[i] = (byte) charArray[i];   
              
              byte[] md5Bytes = md5.digest(byteArray);   
              
              StringBuffer hexValue = new StringBuffer();   
              
              for (int i = 0; i < md5Bytes.length; i++) {   
               int val = ((int) md5Bytes[i]) & 0xff;   
               if (val < 16)   
                hexValue.append("0");   
               hexValue.append(Integer.toHexString(val));   
              }   
              
              return hexValue.toString(); 
        }
         
         /**
          * base64加密
          */
         public static String base64Encryption(String str)  {
            if (str == null) return null; 
            String encodeStr = "";
           
            try {
                BASE64Encoder b64Encoder = new BASE64Encoder();
                encodeStr = b64Encoder.encode(str.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            }
            
           
            return encodeStr;
        }
         /**
          * base64解密
          */
         public static String base64Dcrypt(String str)  {
            if (str == null) return null; 
            String decoderStr = "";
            
            try {
                BASE64Decoder decoder = new BASE64Decoder();
                byte[] b = decoder.decodeBuffer(str);
                decoderStr = new String(b);
            } catch (IOException e) {
                e.printStackTrace();
                return null; 
            }
           
            return decoderStr;
        }
    }
  • 相关阅读:
    5.2基础标签学习
    6.15ajax选房子
    6.15ajax写数据库的增删改查
    [Ext JS 4] 实战之Grid, Tree Gird 添加按钮列
    DOM4J 读取XML配置文件进行数据库连接
    Bit Map解析
    图像像素操作
    九度OJ 1025 最大报销额(01背包)
    MYSQL :逗号分隔串表,分解成竖表
    interrupt & storage & DMA
  • 原文地址:https://www.cnblogs.com/lm970585581/p/8891187.html
Copyright © 2011-2022 走看看