欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

三中常见的加密算法

程序员文章站 2022-03-13 14:50:35
...
实现类:
package jm;

import java.security.MessageDigest;   
  
import javax.crypto.KeyGenerator;   
import javax.crypto.Mac;   
import javax.crypto.SecretKey;   
import javax.crypto.spec.SecretKeySpec;
  
import sun.misc.BASE64Decoder;   
import sun.misc.BASE64Encoder;   
  
/**  
 * 基础加密组件  
 *   
 * @author 
 * @version 1.0  
 * @since 1.0  
 */  
public abstract class Coder {   
    public static final String KEY_SHA = "SHA";   
    public static final String KEY_MD5 = "MD5";   
  
    /**  
     * MAC算法可选以下多种算法  
     *   
     * <pre>  
     * HmacMD5   
     * HmacSHA1   
     * HmacSHA256   
     * HmacSHA384   
     * HmacSHA512  
     * </pre>  
     */  
    public static final String KEY_MAC = "HmacMD5";   
  
    /**  
     * BASE64解密  
     *   
     * @param key  
     * @return  
     * @throws Exception  
     */  
    public static byte[] decryptBASE64(String key) throws Exception {   
        return (new BASE64Decoder()).decodeBuffer(key);   
    }   
  
   /**  
     * BASE64加密  
     *   
     * @param key  
     * @return  
     * @throws Exception  
     */  
    public static String encryptBASE64(byte[] key) throws Exception {   
        return (new BASE64Encoder()).encodeBuffer(key);   
    }   
  
    /**  
     * MD5加密  
     *   
     * @param data  
     * @return  
     * @throws Exception  
     */  
    public static byte[] encryptMD5(byte[] data) throws Exception {   
  
        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);   
        md5.update(data);   
  
        return md5.digest();   
  
    }   
  
    /**  
     * SHA加密  
     *   
     * @param data  
     * @return  
     * @throws Exception  
     */  
    public static byte[] encryptSHA(byte[] data) throws Exception {   
  
        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);   
        sha.update(data);   
  
        return sha.digest();   
  
    }   
  
    /**  
     * 初始化HMAC**  
     *   
     * @return  
     * @throws Exception  
     */  
    public static String initMacKey() throws Exception {   
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);   
  
        SecretKey secretKey = keyGenerator.generateKey();   
        return encryptBASE64(secretKey.getEncoded());   
    }   
  
    /**  
     * HMAC加密  
     *   
     * @param data  
     * @param key  
     * @return  
     * @throws Exception  
     */  
    public static byte[] encryptHMAC(byte[] data, String key) throws Exception {   
  
        SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);   
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());   
        mac.init(secretKey);   
  
        return mac.doFinal(data);   
  
    }   
}  

 测试类:

package jm;

import java.math.BigInteger;

import org.junit.Test;

public class csCoder{

	@Test
	public void test() throws Exception{
		
		String name="陈少";
		byte[] st=name.getBytes();
		
		 BigInteger md5 = new BigInteger(Coder.encryptMD5(st));   
		 System.err.println("MD5:\n" + md5.toString(16));   
		 BigInteger sha = new BigInteger(Coder.encryptSHA(st));   
		 System.err.println("SHA:\n" + sha.toString(32));   
		 BigInteger mac = new BigInteger(Coder.encryptHMAC(st, name));   
		 System.err.println("HMAC:\n" + mac.toString(16));   

	}
}

 

相关标签: 加密