暗号化研究2

3945 ワード

import java.security.MessageDigest;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;


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

	}
}
import static org.junit.Assert.*;

import java.math.BigInteger;

import org.junit.Test;

/**
 *       ,BASE64     ,          ,                 。      。
 *   HMAC   ,         ,        。
 *                ,       ,       。
 * @author Administrator
 *
 */
public class CoderTest {

	@Test
	public void test() throws Exception {
		String inputStr = "    ";
		System.err.println("  :
" + inputStr); byte[] inputData = inputStr.getBytes(); String code = Coder.encryptBASE64(inputData); System.err.println("BASE64 :
" + code); byte[] output = Coder.decryptBASE64(code); String outputStr = new String(output); System.err.println("BASE64 :
" + outputStr); // BASE64 assertEquals(inputStr, outputStr); // MD5 assertArrayEquals(Coder.encryptMD5(inputData), Coder.encryptMD5(inputData)); // SHA assertArrayEquals(Coder.encryptSHA(inputData), Coder.encryptSHA(inputData)); // HMAC String key = Coder.initMacKey(); System.err.println("Mac :
" + key); // HMAC , assertArrayEquals(Coder.encryptHMAC(inputData, key), Coder.encryptHMAC(inputData, key)); BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData)); // 16 。. System.err.println("MD5:
" + md5); System.err.println("MD5:
" + md5.toString()); System.err.println("MD5:
" + md5.toString(16)); BigInteger sha = new BigInteger(Coder.encryptSHA(inputData)); System.err.println("SHA:
" + sha.toString(32)); BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr)); System.err.println("HMAC:
" + mac.toString(16)); } }