java 解密_java 加密解密工具类(实用!!!)

1 packagecom.sh.springboottdemo2.util;2

3

4 importcom.sun.org.apache.xerces.internal.impl.dv.util.Base64;5

6 importjavax.crypto.Cipher;7 importjavax.crypto.KeyGenerator;8 importjavax.crypto.Mac;9 importjavax.crypto.SecretKey;10 importjavax.crypto.spec.SecretKeySpec;11 importjava.security.MessageDigest;12 importjava.security.SecureRandom;13

14 public classEncryptUtil {15 public static final String MD5 = "MD5";16 public static final String SHA1 = "SHA1";17 public static final String HmacMD5 = "HmacMD5";18 public static final String HmacSHA1 = "HmacSHA1";19 public static final String DES = "DES";20 public static final String AES = "AES";21

22 /**编码格式;默认使用uft-8*/

23 public String charset = "utf-8";24 /**DES*/

25 public int keysizeDES = 0;26 /**AES*/

27 public int keysizeAES = 128;28

29 public staticEncryptUtil me;30

31 privateEncryptUtil(){32 //单例

33 }34 //双重锁

35 public staticEncryptUtil getInstance(){36 if (me==null) {37 synchronized (EncryptUtil.class) {38 if(me == null){39 me = newEncryptUtil();40 }41 }42 }43 returnme;44 }45

46 /**

47 * 使用MessageDigest进行单向加密(无密码)48 *@paramres 被加密的文本49 *@paramalgorithm 加密算法名称50 *@return

51 */

52 privateString messageDigest(String res,String algorithm){53 try{54 MessageDigest md =MessageDigest.getInstance(algorithm);55 byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);56 returnbase64(md.digest(resBytes));57 } catch(Exception e) {58 e.printStackTrace();59 }60 return null;61 }62

63 /**

64 * 使用KeyGenerator进行单向/双向加密(可设密码)65 *@paramres 被加密的原文66 *@paramalgorithm 加密使用的算法名称67 *@paramkey 加密使用的秘钥68 *@return

69 */

70 privateString keyGeneratorMac(String res,String algorithm,String key){71 try{72 SecretKey sk = null;73 if (key==null) {74 KeyGenerator kg =KeyGenerator.getInstance(algorithm);75 sk =kg.generateKey();76 }else{77 byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);78 sk = newSecretKeySpec(keyBytes, algorithm);79 }80 Mac mac =Mac.getInstance(algorithm);81 mac.init(sk);82 byte[] result =mac.doFinal(res.getBytes());83 returnbase64(result);84 } catch(Exception e) {85 e.printStackTrace();86 }87 return null;88 }89

90 /**

91 * 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错92 *@paramres 加密的原文93 *@paramalgorithm 加密使用的算法名称94 *@paramkey 加密的秘钥95 *@paramkeysize96 *@paramisEncode97 *@return

98 */

99 private String keyGeneratorES(String res,String algorithm,String key,int keysize,booleanisEncode){100 try{101 KeyGenerator kg =KeyGenerator.getInstance(algorithm);102 if (keysize == 0) {103 byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);104 kg.init(newSecureRandom(keyBytes));105 }else if (key==null) {106 kg.init(keysize);107 }else{108 byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);109 kg.init(keysize, newSecureRandom(keyBytes));110 }111 SecretKey sk =kg.generateKey();112 SecretKeySpec sks = newSecretKeySpec(sk.getEncoded(), algorithm);113 Cipher cipher =Cipher.getInstance(algorithm);114 if(isEncode) {115 cipher.init(Cipher.ENCRYPT_MODE, sks);116 byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);117 returnparseByte2HexStr(cipher.doFinal(resBytes));118 }else{119 cipher.init(Cipher.DECRYPT_MODE, sks);120 return newString(cipher.doFinal(parseHexStr2Byte(res)));121 }122 } catch(Exception e) {123 e.printStackTrace();124 }125 return null;126 }127

128 private String base64(byte[] res){129 returnBase64.encode(res);130 }131

132 /**将二进制转换成16进制*/

133 public static String parseByte2HexStr(bytebuf[]) {134 StringBuffer sb = newStringBuffer();135 for (int i = 0; i < buf.length; i++) {136 String hex = Integer.toHexString(buf[i] & 0xFF);137 if (hex.length() == 1) {138 hex = '0' +hex;139 }140 sb.append(hex.toUpperCase());141 }142 returnsb.toString();143 }144 /**将16进制转换为二进制*/

145 public static byte[] parseHexStr2Byte(String hexStr) {146 if (hexStr.length() < 1)147 return null;148 byte[] result = new byte[hexStr.length()/2];149 for (int i = 0;i< hexStr.length()/2; i++) {150 int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);151 int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);152 result[i] = (byte) (high * 16 +low);153 }154 returnresult;155 }156

157 /**

158 * md5加密算法进行加密(不可逆)159 *@paramres 需要加密的原文160 *@return

161 */

162 publicString MD5(String res) {163 returnmessageDigest(res, MD5);164 }165

166 /**

167 * md5加密算法进行加密(不可逆)168 *@paramres 需要加密的原文169 *@paramkey 秘钥170 *@return

171 */

172 publicString MD5(String res, String key) {173 returnkeyGeneratorMac(res, HmacMD5, key);174 }175

176 /**

177 * 使用SHA1加密算法进行加密(不可逆)178 *@paramres 需要加密的原文179 *@return

180 */

181 publicString SHA1(String res) {182 returnmessageDigest(res, SHA1);183 }184

185 /**

186 * 使用SHA1加密算法进行加密(不可逆)187 *@paramres 需要加密的原文188 *@paramkey 秘钥189 *@return

190 */

191 publicString SHA1(String res, String key) {192 returnkeyGeneratorMac(res, HmacSHA1, key);193 }194

195 /**

196 * 使用DES加密算法进行加密(可逆)197 *@paramres 需要加密的原文198 *@paramkey 秘钥199 *@return

200 */

201 publicString DESencode(String res, String key) {202 return keyGeneratorES(res, DES, key, keysizeDES, true);203 }204

205 /**

206 * 对使用DES加密算法的密文进行解密(可逆)207 *@paramres 需要解密的密文208 *@paramkey 秘钥209 *@return

210 */

211 publicString DESdecode(String res, String key) {212 return keyGeneratorES(res, DES, key, keysizeDES, false);213 }214

215 /**

216 * 使用AES加密算法经行加密(可逆)217 *@paramres 需要加密的密文218 *@paramkey 秘钥219 *@return

220 */

221 publicString AESencode(String res, String key) {222 return keyGeneratorES(res, AES, key, keysizeAES, true);223 }224

225 /**

226 * 对使用AES加密算法的密文进行解密227 *@paramres 需要解密的密文228 *@paramkey 秘钥229 *@return

230 */

231 publicString AESdecode(String res, String key) {232 return keyGeneratorES(res, AES, key, keysizeAES, false);233 }234

235 /**

236 * 使用异或进行加密237 *@paramres 需要加密的密文238 *@paramkey 秘钥239 *@return

240 */

241 publicString XORencode(String res, String key) {242 byte[] bs =res.getBytes();243 for (int i = 0; i < bs.length; i++) {244 bs[i] = (byte) ((bs[i]) ^key.hashCode());245 }246 returnparseByte2HexStr(bs);247 }248

249 /**

250 * 使用异或进行解密251 *@paramres 需要解密的密文252 *@paramkey 秘钥253 *@return

254 */

255 publicString XORdecode(String res, String key) {256 byte[] bs =parseHexStr2Byte(res);257 for (int i = 0; i < bs.length; i++) {258 bs[i] = (byte) ((bs[i]) ^key.hashCode());259 }260 return newString(bs);261 }262

263 /**

264 * 直接使用异或(第一调用加密,第二次调用解密)265 *@paramres 密文266 *@paramkey 秘钥267 *@return

268 */

269 public int XOR(intres, String key) {270 return res ^key.hashCode();271 }272

273 /**

274 * 使用Base64进行加密275 *@paramres 密文276 *@return

277 */

278 publicString Base64Encode(String res) {279 returnBase64.encode(res.getBytes());280 }281

282 /**

283 * 使用Base64进行解密284 *@paramres285 *@return

286 */

287 publicString Base64Decode(String res) {288 return newString(Base64.decode(res));289 }290 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值