消息摘要的编程使用(MD5、SHA、HMAC)
生活随笔
收集整理的這篇文章主要介紹了
消息摘要的编程使用(MD5、SHA、HMAC)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
消息摘要的概念:?
???????? 唯一對應一個消息或文本的固定長度的值,由一個單向Hash加密函數對消息進行作用而產生
?消息摘要的分類:
(1)MD(Message Digest):消息摘要算法
(2)SHA(Secure Hash Algorithm):安全散列算法
(3)MAC(Message Authentication Code):消息認證碼算法
消息摘要的日常應用
? ? 驗證文件的完整性
MD 算法的編程使用
????為計算機安全領域廣泛使用的一種散列函數,用以提供消息的完整性保護,分為md2(128位),md5(128位),md2目前已被棄用。
package com.cry.md5;import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.DigestInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;import com.cry.ByteToHexUtil;/*** md5消息摘要(用于檢測文件的完整性)* @author zhangwx**/ public class MD5Util {/*** 字符串md5* @param data* @return* @throws NoSuchAlgorithmException*/public static String encryptMD5(byte[] data,String type) throws NoSuchAlgorithmException{MessageDigest md5 = MessageDigest.getInstance(type);md5.update(data);byte[] result = md5.digest();//轉為16進制String str = ByteToHexUtil.bytesToHexString(result);return str;}/*** 文件消息摘要* @param path* @return* @throws NoSuchAlgorithmException* @throws IOException*/public static String encryptMD5OfFile(String path,String type) throws NoSuchAlgorithmException, IOException{FileInputStream in = new FileInputStream(new File(path));DigestInputStream dis = new DigestInputStream(in, MessageDigest.getInstance(type));byte[] buffer = new byte[1024];int read = dis.read(buffer, 0, 1024);while (read != -1){read = dis.read(buffer, 0, 1024);}MessageDigest md5 = dis.getMessageDigest();String result = ByteToHexUtil.bytesToHexString(md5.digest());return result;}public static void main(String[] args) throws NoSuchAlgorithmException, IOException {String str = "test md5";System.out.println(MD5Util.encryptMD5(str.getBytes(),"MD5"));System.out.println(MD5Util.encryptMD5OfFile("D:/Installpackage/PieTTY 0.3.26.exe","MD5"));} }
package com.cry;public class ByteToHexUtil {public static String bytesToHexString(byte[] src){StringBuilder stringBuilder = new StringBuilder("");if(src==null||src.length<=0){return null;}for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF;String hv = Integer.toHexString(v);if (hv.length() < 2) {stringBuilder.append(0);} stringBuilder.append(hv);} return stringBuilder.toString(); } }
SHA算法的編程使用
????安全哈希算法,主要適用于數字簽名標準里面定義的數字簽名算法,SHA-1(160位),SHA-256(256位),
SHA-384(384位),SHA-512(512位),向上減少碰撞幾率。
package com.cry.sha;import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;import com.cry.ByteToHexUtil;public class SHAUtils {/*** sha安全消息摘要(相比md5更安全,其中SHA-256,SHA-384擁有更小的碰撞幾率)* @param data* @param type* @return* @throws NoSuchAlgorithmException*/public static String encryptSHA(byte[] data,String type) throws NoSuchAlgorithmException{MessageDigest sha = MessageDigest.getInstance(type);sha.update(data);//轉16進制String result = ByteToHexUtil.bytesToHexString(sha.digest());return result;}public static void main(String[] args) throws NoSuchAlgorithmException {String str = "test sha";System.out.println(str+">>>SHA-1>>>"+SHAUtils.encryptSHA(str.getBytes(), "SHA"));System.out.println(str+">>>SHA-256>>>"+SHAUtils.encryptSHA(str.getBytes(), "SHA-256"));System.out.println(str+">>>SHA-384>>>"+SHAUtils.encryptSHA(str.getBytes(), "SHA-384"));} }
引言:單一MD或SHA算法缺點?
? ? 摘要值容易被篡改!
HMAC 算法的基本概念?
???? ? ??結合了MD5和SHA算法的優勢,同時用密鑰對摘要加密,是一種更為安全的消息摘要算法
package com.cry.hmac;import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException;import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec;import com.cry.ByteToHexUtil;/*** 結合了MD5和SHA算法的優勢,同時用密鑰對摘要加密,是一種更為安全的消息摘要算法* 秘鑰只有發送方和接收方知道* @author zhangwx**/ public class HMACUtils {/*** 生成秘鑰* @param type* @return* @throws NoSuchAlgorithmException*/public static byte[] getSecretKey(String type) throws NoSuchAlgorithmException{//初始化KeyGeneratorKeyGenerator keyGen = KeyGenerator.getInstance(type);//生成秘鑰SecretKey secretKey = keyGen.generateKey();//轉化為字節數組byte[] key = secretKey.getEncoded();return key;}/*** 消息摘要* @param key* @param type* @return* @throws NoSuchAlgorithmException* @throws InvalidKeyException*/public static String encryptHmacMD5(byte[] key,String type) throws NoSuchAlgorithmException, InvalidKeyException{//還原秘鑰SecretKey secretKey = new SecretKeySpec(key, type);//實例化MacMac mac = Mac.getInstance(type);//用秘鑰初始化Macmac.init(secretKey);//獲取消息摘要byte[] result = mac.doFinal();String str = ByteToHexUtil.bytesToHexString(result);return str;}public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException {String type = "HmacMD5";byte[] secretKey = HMACUtils.getSecretKey(type);System.out.println(HMACUtils.encryptHmacMD5(secretKey, type));type = "HmacSHA1";secretKey = HMACUtils.getSecretKey(type);System.out.println(HMACUtils.encryptHmacMD5(secretKey, type));type = "HmacSHA256";secretKey = HMACUtils.getSecretKey(type);System.out.println(HMACUtils.encryptHmacMD5(secretKey, type));} }
總結
以上是生活随笔為你收集整理的消息摘要的编程使用(MD5、SHA、HMAC)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何抄报税
- 下一篇: 对称密码的编程使用(DES、3DES、A