加密工具例子(MD5)1:
MD5 是一種常見的加密手段,只有加密方法沒有解密方法。可以用做登錄密碼加密等場景。
/* * @author qt * @date 2020年4月29日 * @Description */ import java.security.MessageDigest; //MD5加密 //md5 是一種常見的加密手段,只有加密方法沒有解密方法。可以用做登錄密碼加密等場景 public class MD5Util { public static final String CHARSET = "UTF-8"; public final static String MD5(String s) { char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; try { byte[] btInput = s.getBytes(); // 獲得MD5摘要算法的 MessageDigest 對象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字節更新摘要 mdInst.update(btInput); // 獲得密文 byte[] md = mdInst.digest(); // 把密文轉換成十六進制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } } }
加密工具例子2:
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)); } }
問題:
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
出現的問題是這個在eclipse中無法使用,解決方法如下:
(1)進入eclipse中的project下的properities:
(2)雙擊 java Build Path->JRE SystemLibrary->Access rules
(3)雙擊進入后,點擊Add
(4)輸入如下:
點擊ok並應用即可。