java 加密解密工具类(实用!!!)【转】
java 加密解密工具类(实用!!!)
最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下。
https://www.cnblogs.com/zfding/p/9268245.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | package com.sh.springboottdemo2.util; import com.sun.org.apache.xerces. internal .impl.dv.util.Base64; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.MessageDigest; import java.security.SecureRandom; public class EncryptUtil { public static final String MD5 = "MD5" ; public static final String SHA1 = "SHA1" ; public static final String HmacMD5 = "HmacMD5" ; public static final String HmacSHA1 = "HmacSHA1" ; public static final String DES = "DES" ; public static final String AES = "AES" ; /**编码格式;默认使用uft-8*/ public String charset = "utf-8" ; /**DES*/ public int keysizeDES = 0; /**AES*/ public int keysizeAES = 128; public static EncryptUtil me; private EncryptUtil(){ //单例 } //双重锁 public static EncryptUtil getInstance(){ if (me== null ) { synchronized (EncryptUtil. class ) { if (me == null ){ me = new EncryptUtil(); } } } return me; } /** * 使用MessageDigest进行单向加密(无密码) * @param res 被加密的文本 * @param algorithm 加密算法名称 * @return */ private String messageDigest(String res,String algorithm){ try { MessageDigest md = MessageDigest.getInstance(algorithm); byte [] resBytes = charset== null ?res.getBytes():res.getBytes(charset); return base64(md.digest(resBytes)); } catch (Exception e) { e.printStackTrace(); } return null ; } /** * 使用KeyGenerator进行单向/双向加密(可设密码) * @param res 被加密的原文 * @param algorithm 加密使用的算法名称 * @param key 加密使用的秘钥 * @return */ private String keyGeneratorMac(String res,String algorithm,String key){ try { SecretKey sk = null ; if (key== null ) { KeyGenerator kg = KeyGenerator.getInstance(algorithm); sk = kg.generateKey(); } else { byte [] keyBytes = charset== null ?key.getBytes():key.getBytes(charset); sk = new SecretKeySpec(keyBytes, algorithm); } Mac mac = Mac.getInstance(algorithm); mac.init(sk); byte [] result = mac.doFinal(res.getBytes()); return base64(result); } catch (Exception e) { e.printStackTrace(); } return null ; } /** * 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错 * @param res 加密的原文 * @param algorithm 加密使用的算法名称 * @param key 加密的秘钥 * @param keysize * @param isEncode * @return */ private String keyGeneratorES(String res,String algorithm,String key, int keysize,boolean isEncode){ try { KeyGenerator kg = KeyGenerator.getInstance(algorithm); if (keysize == 0) { byte [] keyBytes = charset== null ?key.getBytes():key.getBytes(charset); kg.init( new SecureRandom(keyBytes)); } else if (key== null ) { kg.init(keysize); } else { byte [] keyBytes = charset== null ?key.getBytes():key.getBytes(charset); kg.init(keysize, new SecureRandom(keyBytes)); } SecretKey sk = kg.generateKey(); SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm); Cipher cipher = Cipher.getInstance(algorithm); if (isEncode) { cipher.init(Cipher.ENCRYPT_MODE, sks); byte [] resBytes = charset== null ?res.getBytes():res.getBytes(charset); return parseByte2HexStr(cipher.doFinal(resBytes)); } else { cipher.init(Cipher.DECRYPT_MODE, sks); return new String(cipher.doFinal(parseHexStr2Byte(res))); } } catch (Exception e) { e.printStackTrace(); } return null ; } private String base64( byte [] res){ return Base64.encode(res); } /**将二进制转换成16进制 */ public static String parseByte2HexStr( byte buf[]) { StringBuffer sb = new StringBuffer(); for ( int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /**将16进制转换为二进制*/ public static byte [] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null ; byte [] result = new byte [hexStr.length()/2]; for ( int i = 0;i< hexStr.length()/2; i++) { int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); result[i] = ( byte ) (high * 16 + low); } return result; } /** * md5加密算法进行加密(不可逆) * @param res 需要加密的原文 * @return */ public String MD5(String res) { return messageDigest(res, MD5); } /** * md5加密算法进行加密(不可逆) * @param res 需要加密的原文 * @param key 秘钥 * @return */ public String MD5(String res, String key) { return keyGeneratorMac(res, HmacMD5, key); } /** * 使用SHA1加密算法进行加密(不可逆) * @param res 需要加密的原文 * @return */ public String SHA1(String res) { return messageDigest(res, SHA1); } /** * 使用SHA1加密算法进行加密(不可逆) * @param res 需要加密的原文 * @param key 秘钥 * @return */ public String SHA1(String res, String key) { return keyGeneratorMac(res, HmacSHA1, key); } /** * 使用DES加密算法进行加密(可逆) * @param res 需要加密的原文 * @param key 秘钥 * @return */ public String DESencode(String res, String key) { return keyGeneratorES(res, DES, key, keysizeDES, true ); } /** * 对使用DES加密算法的密文进行解密(可逆) * @param res 需要解密的密文 * @param key 秘钥 * @return */ public String DESdecode(String res, String key) { return keyGeneratorES(res, DES, key, keysizeDES, false ); } /** * 使用AES加密算法经行加密(可逆) * @param res 需要加密的密文 * @param key 秘钥 * @return */ public String AESencode(String res, String key) { return keyGeneratorES(res, AES, key, keysizeAES, true ); } /** * 对使用AES加密算法的密文进行解密 * @param res 需要解密的密文 * @param key 秘钥 * @return */ public String AESdecode(String res, String key) { return keyGeneratorES(res, AES, key, keysizeAES, false ); } /** * 使用异或进行加密 * @param res 需要加密的密文 * @param key 秘钥 * @return */ public String XORencode(String res, String key) { byte [] bs = res.getBytes(); for ( int i = 0; i < bs.length; i++) { bs[i] = ( byte ) ((bs[i]) ^ key.hashCode()); } return parseByte2HexStr(bs); } /** * 使用异或进行解密 * @param res 需要解密的密文 * @param key 秘钥 * @return */ public String XORdecode(String res, String key) { byte [] bs = parseHexStr2Byte(res); for ( int i = 0; i < bs.length; i++) { bs[i] = ( byte ) ((bs[i]) ^ key.hashCode()); } return new String(bs); } /** * 直接使用异或(第一调用加密,第二次调用解密) * @param res 密文 * @param key 秘钥 * @return */ public int XOR( int res, String key) { return res ^ key.hashCode(); } /** * 使用Base64进行加密 * @param res 密文 * @return */ public String Base64Encode(String res) { return Base64.encode(res.getBytes()); } /** * 使用Base64进行解密 * @param res * @return */ public String Base64Decode(String res) { return new String(Base64.decode(res)); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2017-04-13 oracle: 分割字符串,或者查找字段里面的关键字(关键字1,关键字2,关键字3)
2017-04-13 mysql:字符分割,将字符分割成数组