MD5Util.java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class MD5Util {
/*
* 注:
* 1、 Message Digest Algorithm MD5将任意长度的“字节串”映射为一个128bit的大整数,并且是通过该128bit反推原始字符串是困难的
* 2、2004年8月17日的美国加州圣巴巴拉的国际密码学会议(Crypto’2004)上,来自中国山东大学的王小云教授做了破译MD5、HAVAL-128、
* MD4和RIPEMD算法的报告,公布了MD系列算法的破解结果。宣告了固若金汤的世界通行密码标准MD5的堡垒轰然倒塌,
* 引发了密码学界的轩然大波。(注意:并非是真正的破解,只是加速了杂凑冲撞)
*/
public static final String ALGORITHM = "MD5";
public static byte[] encode(byte[] content){
try{
MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
digest.update(content);
return digest.digest();
}catch(NoSuchAlgorithmException e){
}
return null;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(encode("Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数".getBytes())));
System.out.println(HexUtil.encode(encode("Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数".getBytes())));
/*
* 控制台输出:
*
* [95, 17, 75, 31, 32, -4, -66, 12, -49, 38, -59, -64, 17, 54, -97, -115]
* 5F114B1F20FCBE0CCF26C5C011369F8D
*/
}
}
SHA1Util.java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class SHA1Util {
/*
* 注:
* 1、安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的
* 数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。
* 2、SHA-1与MD5的比较:前者在长度上长32 位;强行攻击有更大的强度
* 3、MD5输出128bit、SHA1输出160bit、SHA256输出256bit、另外还有SHA244,SHA512,分别输出244bit,512bit
*/
public static final String ALGORITHM = "SHA1";
public static byte[] encode(byte[] content){
try{
MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
digest.update(content);
return digest.digest();
}catch(NoSuchAlgorithmException e){
}
return null;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(encode("对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。".getBytes())));
System.out.println(HexUtil.encode(encode("对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。".getBytes())));
/*
* 控制台输出:
*
* [-122, 88, 39, 32, 97, 76, -67, -82, 16, -67, -64, 34, -121, 41, 30, 95, 12, 80, 71, 31]
* 86582720614CBDAE10BDC02287291E5F0C50471F
*/
}
}
HmacMd5Util.java
import java.security.SecureRandom;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class HmacMD5Util {
/*
* 注:
* 1、mac(Message Authentication Code,消息认证码算法)是含有密钥散列函数算法,兼容了MD和SHA算法的特性,并在此基础上加上了密钥。
* 因此MAC算法也经常被称作HMAC算法。
* 2、经过mac算法得到的摘要值的长度与实现算法的摘要值长度相同
* 3、MAC系列算法支持表
* 算法 摘要长度 备注
* HmacMD5 128 JAVA6实现
* HmacSHA1 160 JAVA6实现
* HmacSHA256 256 JAVA6实现
* HmacSHA384 384 JAVA6实现
* HmacSHA512 512 JAVA6实现
* HmacMD2 128 BouncyCastle实现
* HmacMD4 128 BouncyCastle实现
* HmacSHA224 224 BouncyCastle实现
*/
public static final String ALGORITHM = "HmacMD5";
public static byte[] generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
public static byte[] generateKey(byte[] seed) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(new SecureRandom(seed));
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
public static byte[] encode(byte[] content, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(content);
}
public static void main(String[] args) throws Exception {
byte[] key = generateKey();
System.out.println(Arrays.toString(key));
System.out.println(key.length);
System.out.println(Arrays.toString(encode("兼容了MD和SHA算法的特性,并在此基础上加上了密钥。".getBytes(), key)));
/*
* 控制台输出:
*
* [-83, 61, 100, -52, -103, 38, 68, 18, 8, -49, 80, 52, 12, 63, -15, 75, 109, -27, 122, -83, 18, -101, 118, -83, 29, -80, -90, -23, 19, -25, 40, 1, 58, -33, -5, 9, 71, 114, 105, 29, -88, -128, -115, -29, 6, 107, 53, -47, -34, 31, 116, 55, -112, -64, 120, 27, -74, 106, -60, -7, 86, -35, 88, 103]
* 64
* [109, -70, 24, 124, -72, -71, 34, -43, -111, 7, 57, 90, 59, -77, 85, 57]
*/
}
}
HmacSHA1Util.java
import java.security.SecureRandom;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class HmacSHA1Util {
public static final String ALGORITHM = "HmacSHA1";
public static byte[] generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
public static byte[] generateKey(byte[] seed) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(new SecureRandom(seed));
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
public static byte[] encode(byte[] content, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(content);
}
public static void main(String[] args) throws Exception {
byte[] key = generateKey();
System.out.println(Arrays.toString(key));
System.out.println(key.length);
System.out.println(Arrays.toString(encode("兼容了MD和SHA算法的特性,并在此基础上加上了密钥。".getBytes(), key)));
/*
* 控制台输出:
*
* [49, -56, 99, 31, -61, 125, -108, 27, 82, -1, 95, -88, 17, 11, 39, 59, 24, 11, 97, 83, -126, 62, -69, 63, -40, 90, -61, 73, 68, -9, 81, -6, 43, -8, 69, 113, 63, 79, -122, -64, 36, -32, -17, -121, -15, -83, -76, 17, -75, -74, -97, 104, -95, -9, -107, -30, -76, -68, 24, 111, 124, 21, -108, 53]
* 64
* [-7, -11, -3, -91, -118, 102, 60, 5, -92, 105, 70, 69, -1, -99, -10, 38, -80, 20, -112, -1]
*/
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10246.html