是一種單向加密算法,只能加密不能解密,示例
java
/** * MD5簡單加密 * @param content 加密內容 * @return String */ public static String md5Encrypt(final String content) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance(ALGORITHM_MD5); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } // md5.update(text.getBytes()); //digest()最後返回md5 hash值,返回值爲8位字符串。由於md5 hash值是16位的hex值,實際上就是8位的字符 //BigInteger函數則將8位的字符串轉換成16位hex值,用字符串來表示;獲得字符串形式的hash值 BigInteger digest = new BigInteger(md5.digest(content.getBytes())); //32位 return digest.toString(16); }
一般用做對二進制數據進行加密,示例
git
/** * base64加密 * @param content 待加密內容 * @return byte[] */ public static byte[] base64Encrypt(final String content) { return Base64.getEncoder().encode(content.getBytes()); } /** * base64解密 * @param encoderContent 已加密內容 * @return byte[] */ public static byte[] base64Decrypt(final byte[] encoderContent) { return Base64.getDecoder().decode(encoderContent); }
數據加密標準算法,和BASE64最明顯的區別就是有一個工做密鑰,該密鑰既用於加密、也用於解密,而且要求密鑰是一個長度至少大於8位的字符串,示例
github
/** * DES加密 * @param key 祕鑰key * @param content 待加密內容 * @return byte[] */ public static byte[] DESEncrypt(final String key, final String content) { return processCipher(content.getBytes(), getSecretKey(key), Cipher.ENCRYPT_MODE , ALGORITHM_DES); } /** * DES解密 * @param key 祕鑰key * @param encoderContent 已加密內容 * @return byte[] */ public static byte[] DESDecrypt(final String key, final byte[] encoderContent) { return processCipher(encoderContent, getSecretKey(key), Cipher.DECRYPT_MODE, ALGORITHM_DES); }
非對稱加密算法的典型表明,既能加密、又能解密。和對稱加密算法好比DES的明顯區別在於用於加密、解密的密鑰是不一樣的。使用RSA算法,只要密鑰足夠長(通常要求1024bit),加密的信息是不能被破解的。示例
算法
/** * RSA加密 * @param content 待加密內容 * @return byte[] */ public static byte[] RSAEncrypt(final String content) { return processCipher(content.getBytes(), keyPair.getPrivate(), Cipher.ENCRYPT_MODE , ALGORITHM_RSA); } /** * RSA解密 * @param encoderContent 已加密內容 * @return byte[] */ public static byte[] RSADecrypt(final byte[] encoderContent) { return processCipher(encoderContent, keyPair.getPublic(), Cipher.DECRYPT_MODE, ALGORITHM_RSA); }
數字簽名等密碼學應用中重要的工具,被普遍地應用於電子商務等信息安全領域,示例
安全
/** * SHA加密 * @param content 待加密內容 * @return String */ public static String SHAEncrypt(final String content) { try { MessageDigest sha = MessageDigest.getInstance(ALGORITHM_SHA); byte[] sha_byte = sha.digest(content.getBytes()); StringBuffer hexValue = new StringBuffer(); for (byte b : sha_byte) { //將其中的每一個字節轉成十六進制字符串:byte類型的數據最高位是符號位,經過和0xff進行與操做,轉換爲int類型的正整數。 String toHexString = Integer.toHexString(b & 0xff); hexValue.append(toHexString.length() == 1 ? "0" + toHexString : toHexString); } return hexValue.toString(); // StringBuffer hexValue2 = new StringBuffer(); // for (int i = 0; i < sha_byte.length; i++) { // int val = ((int) sha_byte[i]) & 0xff; // if (val < 16) { // hexValue2.append("0"); // } // hexValue2.append(Integer.toHexString(val)); // } // return hexValue2.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; }
使用一個密鑰生成一個固定大小的小數據塊,即MAC,並將其加入到消息中,而後傳輸。接收方利用與發送方共享的密鑰進行鑑別認證,示例
服務器
/** * HMAC加密 * @param key 給定祕鑰key * @param content 待加密內容 * @return String */ public static byte[] HMACEncrypt(final String key, final String content) { try { SecretKey secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM_MAC); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); //初始化mac mac.init(secretKey); return mac.doFinal(content.getBytes()); } catch (Exception e) { e.printStackTrace(); } return null; }
測試代碼:app
public static void main(String[] args) { //md5簡單加密 String text = "i am text"; System.out.println(EnDecoderUtil.md5Encrypt(text)); //base64進行加密解密,一般用做對二進制數據進行加密 byte[] base64Encrypt = EnDecoderUtil.base64Encrypt("123456789"); String toHexString = HexUtils.toHexString(base64Encrypt); System.out.println(toHexString); byte[] base64Decrypt = EnDecoderUtil.base64Decrypt(base64Encrypt); System.out.println(new String(base64Decrypt)); //DES對稱加密/解密 //要求key至少長度爲8個字符 String key = "123456789"; //加密 byte[] encode_bytes = EnDecoderUtil.DESEncrypt(key, "Hello, DES"); System.out.println(Base64.getEncoder().encodeToString(encode_bytes)); //解密 byte[] decode_bytes = EnDecoderUtil.DESDecrypt(key, encode_bytes); System.out.println(new String(decode_bytes)); //RSA //數據使用私鑰加密 byte[] en_byte = EnDecoderUtil.RSAEncrypt("Hi, RSA"); System.out.println(Base64.getEncoder().encodeToString(en_byte)); //用戶使用公鑰解密 byte[] de_byte = EnDecoderUtil.RSADecrypt(en_byte); System.out.println(new String(de_byte)); //服務器根據私鑰和加密數據生成數字簽名 byte[] sign_byte = EnDecoderUtil.getSignature(en_byte); System.out.println(Base64.getEncoder().encodeToString(sign_byte)); //用戶根據公鑰、加密數據驗證數據是否被修改過 boolean verify_result = EnDecoderUtil.verifySignature(en_byte, sign_byte); System.out.println(verify_result); //SHA String sha = EnDecoderUtil.SHAEncrypt("Hi, RSA"); System.out.println(sha); //HMAC byte[] mac_bytes = EnDecoderUtil.HMACEncrypt(key, "Hi, HMAC"); System.out.println(HexUtils.toHexString(mac_bytes)); }