http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5231|回复: 1

[Java] URL的加密和解密

[复制链接]
发表于 2014-1-18 16:47:03 | 显示全部楼层 |阅读模式


代码如下:
[Java] 纯文本查看 复制代码
1.	package cn;   
2.	  
3.	import java.security.Key;   
4.	import java.security.SecureRandom;   
5.	  
6.	import javax.crypto.Cipher;   
7.	import javax.crypto.KeyGenerator;   
8.	  
9.	public class Arithmetic {   
10.	  
11.	 static Key key;   
12.	  
13.	 /**  
14.	  * 根据参数生成KEY  
15.	  *   
16.	  * @param strKey  
17.	  */  
18.	 public static void getKey(String strKey) {   
19.	  try {   
20.	   KeyGenerator _generator = KeyGenerator.getInstance("DES");   
21.	   _generator.init(new SecureRandom(strKey.getBytes()));   
22.	   key = _generator.generateKey();   
23.	   _generator = null;   
24.	  } catch (Exception e) {   
25.	   e.printStackTrace();   
26.	  }   
27.	 }   
28.	  
29.	 /**  
30.	  * 加密String明文输入,String密文输出  
31.	  *   
32.	  * @param strMing  
33.	  * [url=home.php?mod=space&uid=3039]@return[/url]  
34.	  */  
35.	 public static String getEncString(String strMing) {   
36.	  byte[] byteMi = null;   
37.	  byte[] byteMing = null;   
38.	  String strMi = "";   
39.	  try {   
40.	   return byte2hex(getEncCode(strMing.getBytes()));   
41.	  
42.	   // byteMing = strMing.getBytes("UTF8");   
43.	   // byteMi = this.getEncCode(byteMing);   
44.	   // strMi = new String( byteMi,"UTF8");   
45.	  } catch (Exception e) {   
46.	   e.printStackTrace();   
47.	  } finally {   
48.	   byteMing = null;   
49.	   byteMi = null;   
50.	  }   
51.	  return strMi;   
52.	 }   
53.	  
54.	 /**  
55.	  * 解密 以String密文输入,String明文输出  
56.	  *   
57.	  * @param strMi  
58.	  * @return  
59.	  */  
60.	 public static String getDesString(String strMi) {   
61.	  byte[] byteMing = null;   
62.	  byte[] byteMi = null;   
63.	  String strMing = "";   
64.	  try {   
65.	   return new String(getDesCode(hex2byte(strMi.getBytes())));   
66.	  
67.	   // byteMing = this.getDesCode(byteMi);   
68.	   // strMing = new String(byteMing,"UTF8");   
69.	  } catch (Exception e) {   
70.	   e.printStackTrace();   
71.	  } finally {   
72.	   byteMing = null;   
73.	   byteMi = null;   
74.	  }   
75.	  return strMing;   
76.	 }   
77.	  
78.	 /**  
79.	  * 加密以byte[]明文输入,byte[]密文输出  
80.	  *   
81.	  * @param byteS  
82.	  * @return  
83.	  */  
84.	 private static byte[] getEncCode(byte[] byteS) {   
85.	  byte[] byteFina = null;   
86.	  Cipher cipher;   
87.	  try {   
88.	   cipher = Cipher.getInstance("DES");   
89.	   cipher.init(Cipher.ENCRYPT_MODE, key);   
90.	   byteFina = cipher.doFinal(byteS);   
91.	  } catch (Exception e) {   
92.	   e.printStackTrace();   
93.	  } finally {   
94.	   cipher = null;   
95.	  }   
96.	  return byteFina;   
97.	 }   
98.	  
99.	 /**  
100.	  * 解密以byte[]密文输入,以byte[]明文输出  
101.	  *   
102.	  * @param byteD  
103.	  * @return  
104.	  */  
105.	 private static byte[] getDesCode(byte[] byteD) {   
106.	  Cipher cipher;   
107.	  byte[] byteFina = null;   
108.	  try {   
109.	   cipher = Cipher.getInstance("DES");   
110.	   cipher.init(Cipher.DECRYPT_MODE, key);   
111.	   byteFina = cipher.doFinal(byteD);   
112.	  } catch (Exception e) {   
113.	   e.printStackTrace();   
114.	  } finally {   
115.	   cipher = null;   
116.	  }   
117.	  return byteFina;   
118.	 }   
119.	  
120.	 /**  
121.	  * 二行制转字符串  
122.	  *   
123.	  * @param b  
124.	  * @return  
125.	  */  
126.	 public static String byte2hex(byte[] b) { // 一个字节的数,   
127.	  // 转成16进制字符串   
128.	  String hs = "";   
129.	  String stmp = "";   
130.	  for (int n = 0; n < b.length; n++) {   
131.	   // 整数转成十六进制表示   
132.	   stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));   
133.	   if (stmp.length() == 1)   
134.	    hs = hs + "0" + stmp;   
135.	   else  
136.	    hs = hs + stmp;   
137.	  }   
138.	  return hs.toUpperCase(); // 转成大写   
139.	 }   
140.	  
141.	 public static byte[] hex2byte(byte[] b) {   
142.	  if ((b.length % 2) != 0)   
143.	   throw new IllegalArgumentException("长度不是偶数");   
144.	  byte[] b2 = new byte[b.length / 2];   
145.	  for (int n = 0; n < b.length; n += 2) {   
146.	   String item = new String(b, n, 2);   
147.	   // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节   
148.	   b2[n / 2] = (byte) Integer.parseInt(item, 16);   
149.	  }   
150.	  
151.	  return b2;   
152.	 }   
153.	  
154.	 public static void main(String[] args) {   
155.	  Arithmetic des = new Arithmetic();// 实例化一个对像   
156.	  des.getKey("aadd");// 生成密匙   
157.	  
158.	  String strEnc = des.getEncString("胡锦涛");// 加密字符串,返回String的密文   
159.	  System.out.println(strEnc);   
160.	  
161.	  String strDes = des.getDesString(strEnc);// 把String 类型的密文解密   
162.	  System.out.println(strDes);   
163.	 }   
164.	  
165.	}   



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-1-18 20:16:57 | 显示全部楼层
这么麻烦吗?
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-14 01:09

© 2014-2021

快速回复 返回顶部 返回列表