A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 15670379287 中级黑马   /  2016-4-8 21:54  /  351 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Java代码  [url=][/url]

  • package com.paic.umap.ucm.common.utils;  
  •   
  • import java.security.MessageDigest;  
  •   
  • import javax.crypto.Cipher;  
  • import javax.crypto.spec.SecretKeySpec;  
  •   
  • import org.bouncycastle.util.encoders.Base64;  
  •   
  • public class LoginDesUtil {  
  • public static final String ALGORITHM_DES = "DESede/ECB/PKCS5Padding";  
  •       
  •     public static final String ZZGJSPWDKEY = "1HH798GZDKUO2167W5GM5YNG";  
  •       
  •   
  •       
  •     /**
  •      * 3DES加密方法
  •      *  
  •      * @param value  待加密信息
  •      * @param desKey 密钥
  •      * @return  3DES加密后用Base64编码的字符串
  •      */  
  •     public static String encrypt(String value, String desKey) {  
  •         String result = null;  
  •         try {  
  •             SecretKeySpec key = new SecretKeySpec(desKey.getBytes(), 0, 24, "DESede");  
  •             Cipher cipher = Cipher.getInstance(ALGORITHM_DES);  
  •             cipher.init(Cipher.ENCRYPT_MODE, key);  
  •             result = new String(Base64.encode(cipher.doFinal(value.getBytes("UTF-8"))), "UTF-8");  
  •         } catch (Exception e) {  
  •         }   
  •          
  •         return result;  
  •          
  •     }  
  •       
  •     /**
  •      * 3DES解密方法
  •      *  
  •      * @param value  3DES加密后用Base64编码的字符串
  •      * @param desKey 密钥
  •      * @return 加密原文
  •      */  
  •     public static String decrypt(String value, String desKey) {  
  •         String result = null;  
  •         try {  
  •             SecretKeySpec key = new SecretKeySpec(desKey.getBytes(), 0, 24, "DESede");  
  •             Cipher cipher = Cipher.getInstance(ALGORITHM_DES);  
  •             cipher.init(Cipher.DECRYPT_MODE, key);  
  •             result = new String(cipher.doFinal(Base64.decode(value)), "UTF-8");  
  •         } catch (Exception e) {  
  •         }   
  •          
  •         return result;  
  •     }  
  •       
  •     public static void main(String[] args) {  
  •         String result="+=  +=//";  
  •         result = result.replaceAll("[+]", "%2B");  
  •         result = result.replaceAll("[/]", "%2F");  
  •         result = result.replaceAll("[=]", "%3D");  
  •         result = result.replaceAll("[ ]", "%20");  
  •         System.out.println(result);  
  •         result = result.replaceAll("%2F", "/");  
  •         result = result.replaceAll("%3D", "=");  
  •         result = result.replaceAll("%20", " ");  
  •         result = result.replaceAll("%2B", "+");  
  •         System.out.println(result);  
  •         System.out.println(LoginDesUtil.encryptToURL("!@#+/\\$  %^&*()[]{}【】|!金jin123", ZZGJSPWDKEY));  
  •          
  •         System.out.println(LoginDesUtil.decryptToURL(LoginDesUtil.encryptToURL("!@#+/\\$  %^&*()[]{}【】|!金jin123", ZZGJSPWDKEY), ZZGJSPWDKEY));  
  •     }  
  •       
  •       
  •     /**
  •      * 3Des的key加密(建议优先考虑) ,用于加密的URL使用
  •      * @param value 待加密信息
  •      * @return  3DES加密后用Base64编码的字符串
  •      */  
  •     public static String encryptToURL(String value, String key){  
  •         String result = encrypt(value, key);  
  •         // 将加密后字符串中“+”、“/”、“=”转换成替代字符串  
  •         result = result.replaceAll("[+]", "%2B");  
  •         result = result.replaceAll("[/]", "%2F");  
  •         result = result.replaceAll("[=]", "%3D");  
  •         result = result.replaceAll("[ ]", "%20");  
  •         return result;  
  •     }  
  •       
  •     /**  
  •      * 3Des使用默认的key解密URL (建议优先考虑)  
  •      * @param value 3DES加密后用Base64编码的字符串
  •      * @return 加密原文
  •      */  
  •     public static String decryptToURL(String value,String key){  
  •         String result = value.replaceAll("%2B", "+");  
  •         result = result.replaceAll("%2F", "/");  
  •         result = result.replaceAll("%3D", "=");  
  •         result = result.replaceAll("%20", " ");  
  •         result = decrypt(result, key);  
  •         return result;  
  •     }  
  •       
  •       
  •     /**
  •      * 3Des使用默认的key解密
  •      * @param value 待解密信息
  •      * @return  3DES加密后用Base64编码的字符串
  •      */  
  •     public static String decryptToZZGJS(String value){  
  •         return decrypt(value, ZZGJSPWDKEY);  
  •     }  
  •       
  •     public static String sign(String text, String sign) {  
  •         String sha1 = null;  
  •         try {  
  •             MessageDigest md = MessageDigest.getInstance("SHA1");  
  •             md.update(text.getBytes());  
  •             sha1 = byte2string(md.digest());  
  •         } catch (Exception e) {  
  •         }  
  •         return sha1;  
  •     }  
  •       
  •     private static String byte2string(byte[] data) {  
  •         StringBuffer sb = new StringBuffer();  
  •         char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  
  •                 'A', 'B', 'C', 'D', 'E', 'F' };  
  •         for(int i = 0; i < data.length; i++) {  
  •             char[] t = new char[2];  
  •             t[0] = hex[(data >>> 4) & 0x0F];  
  •             t[1] = hex[data & 0x0F];  
  •             sb.append(t);  
  •         }  
  •         return sb.toString();  
  •     }  
  • }  

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马