hexutil加解密_java常用加密和解密工具类EncryptUtil.java

1 packagecn.util;2

3 importjava.io.UnsupportedEncodingException;4 importjava.security.MessageDigest;5 importjava.security.NoSuchAlgorithmException;6 importjava.security.SecureRandom;7

8 importjavax.crypto.Cipher;9 importjavax.crypto.SecretKey;10 importjavax.crypto.SecretKeyFactory;11 importjavax.crypto.spec.DESKeySpec;12

13 /**

14 * 加密工具类15 *16 * md5加密出来的长度是32位17 *18 * sha加密出来的长度是40位19 *20 *@author伍玉林21 *22 */

23 public final classEncryptUtil {24

25 private static final String PASSWORD_CRYPT_KEY = "88444488";26

27 private final static String DES = "DES";28

29 /**

30 * 二次加密 先sha-1加密再用MD5加密31 *32 *@paramsrc33 *@return

34 */

35 public final staticString md5AndSha(String src) {36 returnmd5(sha(src));37 }38

39 /**

40 * 二次加密 先MD5加密再用sha-1加密41 *42 *@paramsrc43 *@return

44 */

45 public final staticString shaAndMd5(String src) {46 returnsha(md5(src));47 }48

49 /**

50 * md5加密51 *52 *@paramsrc53 *@return

54 */

55 public final staticString md5(String src) {56 return encrypt(src, "md5");57 }58

59 /**

60 * sha-1加密61 *62 *@paramsrc63 *@return

64 */

65 public final staticString sha(String src) {66 return encrypt(src, "sha-1");67 }68

69 /**

70 * md5或者sha-1加密71 *72 *@paramsrc73 * 要加密的内容74 *@paramalgorithmName75 * 加密算法名称:md5或者sha-1,不区分大小写76 *@return

77 */

78 private final staticString encrypt(String src, String algorithmName) {79 if (src == null || "".equals(src.trim())) {80 throw new IllegalArgumentException("请输入要加密的内容");81 }82 if (algorithmName == null || "".equals(algorithmName.trim())) {83 algorithmName = "md5";84 }85 String encryptText = null;86 try{87 MessageDigest m =MessageDigest.getInstance(algorithmName);88 m.update(src.getBytes("UTF8"));89 byte s[] =m.digest();90 //m.digest(src.getBytes("UTF8"));

91 returnhex(s);92 } catch(NoSuchAlgorithmException e) {93 e.printStackTrace();94 } catch(UnsupportedEncodingException e) {95 e.printStackTrace();96 }97 returnencryptText;98 }99

100 /**

101 * 密码解密102 *103 *@paramdata104 *@return

105 *@throwsException106 */

107 public final staticString decrypt(String src) {108 try{109 return newString(decrypt(hex2byte(src.getBytes()), PASSWORD_CRYPT_KEY.getBytes()));110 } catch(Exception e) {111 }112 return null;113 }114

115 /**

116 * 密码加密117 *118 *@parampassword119 *@return

120 *@throwsException121 */

122 public final staticString encrypt(String src) {123 try{124 returnbyte2hex(encrypt(src.getBytes(), PASSWORD_CRYPT_KEY.getBytes()));125 } catch(Exception e) {126 }127 return null;128 }129

130 /**

131 * 加密132 *133 *@paramsrc134 * 数据源135 *@paramkey136 * 密钥,长度必须是8的倍数137 *@return返回加密后的数据138 *@throwsException139 */

140 private static byte[] encrypt(byte[] src, byte[] key) throwsException {141 //DES算法要求有一个可信任的随机数源

142 SecureRandom sr = newSecureRandom();143 //从原始密匙数据创建DESKeySpec对象

144 DESKeySpec dks = newDESKeySpec(key);145 //创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象

146 SecretKeyFactory keyFactory =SecretKeyFactory.getInstance(DES);147 SecretKey securekey =keyFactory.generateSecret(dks);148 //Cipher对象实际完成加密操作

149 Cipher cipher =Cipher.getInstance(DES);150 //用密匙初始化Cipher对象

151 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);152 //现在,获取数据并加密正式执行加密操作

153 returncipher.doFinal(src);154 }155

156 /**

157 * 解密158 *159 *@paramsrc160 * 数据源161 *@paramkey162 * 密钥,长度必须是8的倍数163 *@return返回解密后的原始数据164 *165 *@throwsException166 */

167 private final static byte[] decrypt(byte[] src, byte[] key) throwsException {168 //DES算法要求有一个可信任的随机数源

169 SecureRandom sr = newSecureRandom();170 //从原始密匙数据创建一个DESKeySpec对象

171 DESKeySpec dks = newDESKeySpec(key);172 //创建一个密匙工厂,然后用它把DESKeySpec对象转换成一个SecretKey对象

173 SecretKeyFactory keyFactory =SecretKeyFactory.getInstance(DES);174 SecretKey securekey =keyFactory.generateSecret(dks);175 //Cipher对象实际完成解密操作

176 Cipher cipher =Cipher.getInstance(DES);177 //用密匙初始化Cipher对象

178 cipher.init(Cipher.DECRYPT_MODE, securekey, sr);179 //现在,获取数据并解密正式执行解密操作

180 returncipher.doFinal(src);181 }182

183 private final static byte[] hex2byte(byte[] b) {184 if ((b.length % 2) != 0)185 throw new IllegalArgumentException("长度不是偶数");186 byte[] b2 = new byte[b.length / 2];187 for (int n = 0; n < b.length; n += 2) {188 String item = new String(b, n, 2);189 b2[n / 2] = (byte) Integer.parseInt(item, 16);190 }191 returnb2;192 }193

194 /**

195 * 二行制转字符串196 *197 *@paramb198 *@return

199 */

200 private final static String byte2hex(byte[] b) {201 String hs = "";202 String stmp = "";203 for (int n = 0; n < b.length; n++) {204 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));205 if (stmp.length() == 1)206 hs = hs + "0" +stmp;207 else

208 hs = hs +stmp;209 }210 returnhs.toUpperCase();211 }212

213 /**

214 * 返回十六进制字符串215 *216 *@paramarr217 *@return

218 */

219 private final static String hex(byte[] arr) {220 StringBuffer sb = newStringBuffer();221 for (int i = 0; i < arr.length; ++i) {222 sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));223 }224 returnsb.toString();225 }226

227 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值