| 算法 | 输出散列值长度(bits) | 中继散列值长度(bits) | 数据区块长度(bits) | 最大输入消息长度(bits) | 一个Word长度(bits) | 循环次数 | 使用到的运算符 | 碰撞攻击 |
|---|---|---|---|---|---|---|---|---|
| SHA-0 | 160 | 160 | 512 | 264 − 1 | 32 | 80 | +,and,or,xor,rotl | 是 |
| SHA-1 | 160 | 160 | 512 | 264 − 1 | 32 | 80 | +,and,or,xor,rotl | 存在263的攻击 |
| SHA-256/224 | 256/224 | 256 | 512 | 264 − 1 | 32 | 64 | +,and,or,xor,shr,rotr | 尚未出现 |
| SHA-512/384 | 512/384 | 512 | 1024 | 2128 − 1 | 64 | 80 | +,and,or,xor,shr,rotr | 尚未出现 |
package com.zzj.encryption;
import java.security.MessageDigest;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* 单向加密(非可逆加密)
* @author lenovo
*
*/
public class OneWayEncryption {
static final String ALGORITHM_MD5 = "MD5";
static final String ALGORITHM_SHA = "SHA";
/**
* MAC算法可选以下多种算法
* <pre>
* HmacMD5
* HmacSHA1
* HmacSHA256
* HmacSHA384
* HmacSHA512
* </pre>
*/
static final String ALGORITHM_MAC = "HmacMD5";
/** 密钥 **/
static final String MAC_KEY = "abcdef";
public static void main(String[] args) throws Exception {
String source = "我是程序猿!我很骄傲!";
// MD5加密
printBase64(encryptionMD5(source));
// SHA加密
printBase64(encryptionSHA(source));
// HMAC加密
printBase64(encryptionHMAC(source));
}
static void printBase64(byte[] out) throws Exception {
System.out.println(encodeBase64(out));
}
/**
* MD5加密
* @param source
* @return
* @throws Exception
*/
static byte[] encryptionMD5(String source) throws Exception {
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
md.update(source.getBytes("UTF-8"));
return md.digest();
}
/**
* SHA加密
* @param source
* @return
* @throws Exception
*/
static byte[] encryptionSHA(String source) throws Exception {
MessageDigest md = MessageDigest.getInstance(ALGORITHM_SHA);
md.update(source.getBytes("UTF-8"));
return md.digest();
}
/**
* HMAC加密
* @return
* @throws Exception
*/
static byte[] encryptionHMAC(String source) throws Exception {
SecretKey secretKey = new SecretKeySpec(MAC_KEY.getBytes("UTF-8"), ALGORITHM_MAC);
Mac mac = Mac.getInstance(ALGORITHM_MAC);
mac.init(secretKey);
mac.update(source.getBytes("UTF-8"));
return mac.doFinal();
}
/**
* base64编码
* @param source
* @return
* @throws Exception
*/
static String encodeBase64(byte[] source) throws Exception{
return new String(Base64.encodeBase64(source), "UTF-8");
}
}
1cNbZhnhFsFV3BFPLA71wA== kl5KI61Xq44E/SzSPa2sUntMAEc= JF2v/u9td5l8yGAImNvTZw==
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有