java加密演算法BASE64&MD5&SHA
阿新 • • 發佈:2018-12-20
1、BASE64
Base64定義:Base64內容傳送編碼被設計用來把任意序列的8位位元組描述為一種不易被人直接識別的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.) 常見於郵件、http加密,擷取http資訊,你就會發現登入操作的使用者名稱、密碼欄位通過BASE64加密的。
例項:
package cn.tzz.java.crypto; import java.io.IOException; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class BASE64Util { /**加密*/ public static String encrypt(byte[] key) { return (new BASE64Encoder()).encodeBuffer(key); } /**解密*/ public static String decrypt(String key) { try { return new String((new BASE64Decoder()).decodeBuffer(key),"utf-8"); } catch (IOException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String encryptStr = encrypt("123456".getBytes()); System.out.println("加密:"+encryptStr); System.out.println("解密:"+decrypt(encryptStr)); } }
2、MD5&SHA
MD5 -- message-digest algorithm 5 (資訊-摘要演算法)
SHA(Secure Hash Algorithm,安全雜湊演算法)
package cn.tzz.java.crypto.simple; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5SHACryptoUtil { /**md5加密*/ public static String md5Encrypt(String str) { String md5String = null; try { // 獲得MD5摘要演算法的 MessageDigest 物件 MessageDigest md = MessageDigest.getInstance("MD5"); // 使用指定的位元組更新摘要 md.update(str.getBytes()); // 獲得密文,把密文轉換成十六進位制的字串形式 //方式一 md5String = byte2Hex(md.digest()); //方式二 //md5String = byteToHex(md.digest()); //方式三 //md5String = byteToString(md.digest()); } catch (Exception ex) { ex.printStackTrace(); } return md5String; } /**SHA1加密*/ public static String shaEncrypt(String date) { byte[] digest = null; String rs = null; try { // 得到一個SHA-1的訊息摘要 MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); // 新增要進行計算摘要的資訊 messageDigest.update(date.getBytes()); // 得到該摘要 digest = messageDigest.digest(); // 將摘要轉為字串 rs = byte2Hex(digest); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return rs; } // 1.把密文轉換成十六進位制的字串形式(Integer.toHexString函式) public static String byte2Hex(byte[] b) { StringBuffer sb = new StringBuffer(); String tmp = ""; for (int i = 0; i < b.length; i++) { tmp = Integer.toHexString(b[i] & 0XFF); if (tmp.length() == 1){ sb.append("0"); } sb.append(tmp); } return sb.toString(); } // 2.把密文轉換成十六進位制的字串形式(自定義) public static String byteToHex(byte[] b) { // 全域性陣列 char[] hexDigits = {'0' , '1' , '2' , '3' , '4' , '5' ,'6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f'}; // 把密文轉換成十六進位制的字串形式 int j = b.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = b[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } // 3.轉換位元組陣列為16進位制字串 private static String byteToString(byte[] b) { StringBuffer sBuffer = new StringBuffer(); for (int i = 0; i < b.length; i++) { sBuffer.append(byteToArrayString(b[i])); } return sBuffer.toString(); } // 返回形式為數字跟字串 private static String byteToArrayString(byte b) { String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; int iRet = b; if (iRet < 0) { iRet += 256; } int iD1 = iRet / 16; int iD2 = iRet % 16; return strDigits[iD1] + strDigits[iD2]; } public static void main(String[] args) { System.out.println(md5Encrypt("123456")); System.out.println(shaEncrypt("123456")); } }