JAVA加密解密工具类

package cn.com.hkgt.apps.util;
2
3
4import java.security.*;
5import javax.crypto.Cipher;
6import javax.crypto.SecretKey;
7import javax.crypto.SecretKeyFactory;
8import javax.crypto.spec.DESKeySpec;
9
10/** *//**
11 * 字符串工具集合
12*/

13public class StringUtils {
14
15private static final String PASSWORD_CRYPT_KEY = "cindaportal";
16private final static String DES = "DES";
17
18/** *//**
19 * 加密
20 * @param src 数据源
21 * @param key 密钥,长度必须是8的倍数
22 * @return 返回加密后的数据
23 * @throws Exception
24*/

25public static byte[] encrypt(byte[] src, byte[] key)throws Exception {
26//DES算法要求有一个可信任的随机数源
27 SecureRandom sr = new SecureRandom();
28// 从原始密匙数据创建DESKeySpec对象
29 DESKeySpec dks = new DESKeySpec(key);
30// 创建一个密匙工厂,然后用它把DESKeySpec转换成
31// 一个SecretKey对象
32 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
33 SecretKey securekey = keyFactory.generateSecret(dks);
34// Cipher对象实际完成加密操作
35 Cipher cipher = Cipher.getInstance(DES);
36// 用密匙初始化Cipher对象
37 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
38// 现在,获取数据并加密
39// 正式执行加密操作
40 return cipher.doFinal(src);
41 }

42
43/** *//**
44 * 解密
45 * @param src 数据源
46 * @param key 密钥,长度必须是8的倍数
47 * @return 返回解密后的原始数据
48 * @throws Exception
49*/

50public static byte[] decrypt(byte[] src, byte[] key)throws Exception {
51// DES算法要求有一个可信任的随机数源
52 SecureRandom sr = new SecureRandom();
53// 从原始密匙数据创建一个DESKeySpec对象
54 DESKeySpec dks = new DESKeySpec(key);
55// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
56// 一个SecretKey对象
57 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
58 SecretKey securekey = keyFactory.generateSecret(dks);
59// Cipher对象实际完成解密操作
60 Cipher cipher = Cipher.getInstance(DES);
61// 用密匙初始化Cipher对象
62 cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
63// 现在,获取数据并解密
64// 正式执行解密操作
65 return cipher.doFinal(src);
66 }

67/** *//**
68 * 密码解密
69 * @param data
70 * @return
71 * @throws Exception
72*/

73public final static String decrypt(String data){
74try {
75return new String(decrypt(hex2byte(data.getBytes()),PASSWORD_CRYPT_KEY.getBytes()));
76 }
catch(Exception e) {
77 }

78return null;
79 }

80/** *//**
81 * 密码加密
82 * @param password
83 * @return
84 * @throws Exception
85*/

86public final static String encrypt(String password){
87try {
88return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes()));
89 }
catch(Exception e) {
90 }

91return null;
92 }

93/** *//**
94 * 二行制转字符串
95 * @param b
96 * @return
97*/

98public static String byte2hex(byte[] b) {
99 String hs = "";
100 String stmp = "";
101for (int n = 0; n < b.length; n++) {
102 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
103if (stmp.length() == 1)
104 hs = hs + "0" + stmp;
105else
106 hs = hs + stmp;
107 }

108return hs.toUpperCase();
109 }

110
111public static byte[] hex2byte(byte[] b) {
112if((b.length%2)!=0)
113throw new IllegalArgumentException("长度不是偶数");
114byte[] b2 = new byte[b.length/2];
115for (int n = 0; n < b.length; n+=2) {
116 String item = new String(b,n,2);
117 b2[n/2] = (byte)Integer.parseInt(item,16);
118 }

119return b2;
120 }

121
122public static void main(String[] args) {
123 String pwd = "测试dasdfaaaaaaa";
124 System.out.println("测试数据="+pwd);
125 String data = encrypt(pwd);
126 System.out.println("加密后的数据data="+data);
127 pwd = decrypt(data);
128 System.out.println("解密后="+pwd);
129
130 }

131}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值