JDK一些加密技术应用

/**
 * 
 */
package encrypt;

import java.math.BigInteger;
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;

/**
 * @descript EncryptDemo.java
 * @author sinclair
 * @date Jun 9, 2010
 */
public class EncryptDemo {
    public static final String KEY_SHA = "SHA";
    public static final String KEY_MD5 = "MD5";
    /**
     * MAC算法可选以下多种算法 HmacMD5 HmacSHA1 HmacSHA256 HmacSHA384 HmacSHA512
     */
    public static final String KEY_MAC = "HmacMD5";

    /**
     * @param args
     * @throws Exception
     */
    public static void main( String[] args ) throws Exception {
	String src = "admin";
	System.out.println( "原数据:\n" + src );
	System.out.println( "--------------------MD5加密------------------" );
	BigInteger md5 = new BigInteger( encryptMD5( src.getBytes() ) );
	String md5_target = md5.toString( 16 );
	System.out.println( "MD5加密:\n" + md5_target );
	System.out.println( "--------------------SHA加密------------------" );
	BigInteger sha5 = new BigInteger( encryptSHA( src.getBytes() ) );
	String sha5_target = sha5.toString( 32 );
	System.out.println( "SHA加密:\n" + sha5_target );
	System.out.println( "--------------------HMAC加密-----------------" );
	String key = initMacKey();
	System.out.println( "HMAC密钥:\n" + key );
	BigInteger hmac = new BigInteger( encryptHMAC( src.getBytes(), key ) );
	String hmac_target = hmac.toString( 16 );
	System.out.println( "HMAC加密:\n" + hmac_target );
	System.out.println( "--------------------BASE64加密和解密----------" );
	byte[] inputData = src.getBytes();
	String code = encryptBASE64( inputData );
	System.out.println( "BASE64加密:\n" + code );
	byte[] output = decryptBASE64( code );
	String outputStr = new String( output );
	System.out.println( "BASE64解密:\n" + outputStr );
    }

    /**
     * MD5加密
     * 
     * @param input
     * @return
     * @throws Exception
     */
    public static byte[] encryptMD5( byte[] input ) throws Exception {
	MessageDigest md5 = MessageDigest.getInstance( KEY_MD5 );
	// md5.update( input );
	return md5.digest( input );
    }

    /**
     * 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( data );

    }

    /**
     * BASE64加密
     * 
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64( byte[] key ) throws Exception {
	return ( new BASE64Encoder() ).encodeBuffer( key );
    }

    /**
     * BASE64解密
     * 
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64( String key ) throws Exception {
	return ( new BASE64Decoder() ).decodeBuffer( key );
    }

    /**
     * 初始化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 );
    }

}

你可能感兴趣的:(jdk,算法,Security,sun)