加密工具类,包含MD5,BASE64,SHA,CRC32的加密与解密方法。
源码如下:(点击下载 - CryptoUtils.java、commons-io-2.4.jar、commons-codec-1.9.jar )
1 import java.io.BufferedInputStream; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.UnsupportedEncodingException; 8 import java.net.URL; 9 import java.util.zip.CRC32; 10 import java.util.zip.CheckedInputStream; 11 import org.apache.commons.codec.binary.Base64; 12 import org.apache.commons.codec.digest.DigestUtils; 13 import org.apache.commons.io.IOUtils; 14 15 /** 16 * 加密工具类,包含MD5,BASE64,SHA,CRC32 17 * 18 */ 19 public class CryptoUtils { 20 21 private static final String DEFAULT_CHARSET = "UTF-8"; 22 23 /** 24 * MD5加密 25 * 26 * @param bytes 27 * an array of byte. 28 * @return a {@link java.lang.String} object. 29 */ 30 public static String encodeMD5(final byte[] bytes) { 31 return DigestUtils.md5Hex(bytes); 32 } 33 34 /** 35 * MD5加密,默认UTF-8 36 * 37 * @param str 38 * a {@link java.lang.String} object. 39 * @return a {@link java.lang.String} object. 40 */ 41 public static String encodeMD5(final String str) { 42 return encodeMD5(str, DEFAULT_CHARSET); 43 } 44 45 /** 46 * MD5加密 47 * 48 * @param str 49 * a {@link java.lang.String} object. 50 * @param charset 51 * a {@link java.lang.String} object. 52 * @return a {@link java.lang.String} object. 53 */ 54 public static String encodeMD5(final String str, final String charset) { 55 if (str == null) { 56 return null; 57 } 58 try { 59 byte[] bytes = str.getBytes(charset); 60 return encodeMD5(bytes); 61 } catch (UnsupportedEncodingException e) { 62 throw new RuntimeException(e); 63 } 64 } 65 66 /** 67 * SHA加密 68 * 69 * @param bytes 70 * an array of byte. 71 * @return a {@link java.lang.String} object. 72 */ 73 public static String encodeSHA(final byte[] bytes) { 74 return DigestUtils.sha512Hex(bytes); 75 } 76 77 /** 78 * SHA加密 79 * 80 * @param str 81 * a {@link java.lang.String} object. 82 * @param charset 83 * a {@link java.lang.String} object. 84 * @return a {@link java.lang.String} object. 85 */ 86 public static String encodeSHA(final String str, final String charset) { 87 if (str == null) { 88 return null; 89 } 90 try { 91 byte[] bytes = str.getBytes(charset); 92 return encodeSHA(bytes); 93 } catch (UnsupportedEncodingException e) { 94 throw new RuntimeException(e); 95 } 96 } 97 98 /** 99 * SHA加密,默认utf-8 100 * 101 * @param str 102 * a {@link java.lang.String} object. 103 * @return a {@link java.lang.String} object. 104 */ 105 public static String encodeSHA(final String str) { 106 return encodeSHA(str, DEFAULT_CHARSET); 107 } 108 109 /** 110 * BASE64加密 111 * 112 * @param bytes 113 * an array of byte. 114 * @return a {@link java.lang.String} object. 115 */ 116 public static String encodeBASE64(final byte[] bytes) { 117 return new String(Base64.encodeBase64String(bytes)); 118 } 119 120 /** 121 * BASE64加密 122 * 123 * @param str 124 * a {@link java.lang.String} object. 125 * @param charset 126 * a {@link java.lang.String} object. 127 * @return a {@link java.lang.String} object. 128 */ 129 public static String encodeBASE64(final String str, String charset) { 130 if (str == null) { 131 return null; 132 } 133 try { 134 byte[] bytes = str.getBytes(charset); 135 return encodeBASE64(bytes); 136 } catch (UnsupportedEncodingException e) { 137 throw new RuntimeException(e); 138 } 139 } 140 141 /** 142 * BASE64加密,默认UTF-8 143 * 144 * @param str 145 * a {@link java.lang.String} object. 146 * @return a {@link java.lang.String} object. 147 */ 148 public static String encodeBASE64(final String str) { 149 return encodeBASE64(str, DEFAULT_CHARSET); 150 } 151 152 /** 153 * BASE64解密,默认UTF-8 154 * 155 * @param str 156 * a {@link java.lang.String} object. 157 * @return a {@link java.lang.String} object. 158 */ 159 public static String decodeBASE64(String str) { 160 return decodeBASE64(str, DEFAULT_CHARSET); 161 } 162 163 /** 164 * BASE64解密 165 * 166 * @param str 167 * a {@link java.lang.String} object. 168 * @param charset 169 * 字符编码 170 * @return a {@link java.lang.String} object. 171 */ 172 public static String decodeBASE64(String str, String charset) { 173 try { 174 byte[] bytes = str.getBytes(charset); 175 return new String(Base64.decodeBase64(bytes)); 176 } catch (UnsupportedEncodingException e) { 177 throw new RuntimeException(e); 178 } 179 } 180 181 /** 182 * CRC32字节校验 183 * 184 * @param bytes 185 * an array of byte. 186 * @return a {@link java.lang.String} object. 187 */ 188 public static String crc32(byte[] bytes) { 189 CRC32 crc32 = new CRC32(); 190 crc32.update(bytes); 191 return Long.toHexString(crc32.getValue()); 192 } 193 194 /** 195 * CRC32字符串校验 196 * 197 * @param str 198 * a {@link java.lang.String} object. 199 * @param charset 200 * a {@link java.lang.String} object. 201 * @return a {@link java.lang.String} object. 202 */ 203 public static String crc32(final String str, String charset) { 204 try { 205 byte[] bytes = str.getBytes(charset); 206 return crc32(bytes); 207 } catch (UnsupportedEncodingException e) { 208 throw new RuntimeException(e); 209 } 210 } 211 212 /** 213 * CRC32字符串校验,默认UTF-8编码读取 214 * 215 * @param str 216 * a {@link java.lang.String} object. 217 * @return a {@link java.lang.String} object. 218 */ 219 public static String crc32(final String str) { 220 return crc32(str, DEFAULT_CHARSET); 221 } 222 223 /** 224 * CRC32流校验 225 * 226 * @param input 227 * a {@link java.io.InputStream} object. 228 * @return a {@link java.lang.String} object. 229 */ 230 public static String crc32(InputStream input) { 231 CRC32 crc32 = new CRC32(); 232 CheckedInputStream checkInputStream = null; 233 int test = 0; 234 try { 235 checkInputStream = new CheckedInputStream(input, crc32); 236 do { 237 test = checkInputStream.read(); 238 } while (test != -1); 239 return Long.toHexString(crc32.getValue()); 240 } catch (IOException e) { 241 e.printStackTrace(); 242 throw new RuntimeException(e); 243 } 244 } 245 246 /** 247 * CRC32文件唯一校验 248 * 249 * @param file 250 * a {@link java.io.File} object. 251 * @return a {@link java.lang.String} object. 252 */ 253 public static String crc32(File file) { 254 InputStream input = null; 255 try { 256 input = new BufferedInputStream(new FileInputStream(file)); 257 return crc32(input); 258 } catch (FileNotFoundException e) { 259 e.printStackTrace(); 260 throw new RuntimeException(e); 261 } finally { 262 IOUtils.closeQuietly(input); 263 } 264 } 265 266 /** 267 * CRC32文件唯一校验 268 * 269 * @param url 270 * a {@link java.net.URL} object. 271 * @return a {@link java.lang.String} object. 272 */ 273 public static String crc32(URL url) { 274 InputStream input = null; 275 try { 276 input = url.openStream(); 277 return crc32(input); 278 } catch (IOException e) { 279 e.printStackTrace(); 280 throw new RuntimeException(e); 281 } finally { 282 IOUtils.closeQuietly(input); 283 } 284 } 285 }