java单项加密算法

 
 
 
 
欢迎技术交流。 QQ:138986722
 
 
Base64可以解密、MD5、SHA、不行、也算不上是加密算法!
 
 
 
 
package com.boxun.crm.test;

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;

public abstract class Base64Test {

	private static final String KEY_MD5 = "MD5";
	private static final String KEY_SHA = "SHA";
	/**  
	 * 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 {  
		/**-- getInstance(String algorithm)返回实现指定摘要算法的 MessageDigest 对象  --*/
		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 { 
		/**-- getInstance(String algorithm)返回生成指定算法的秘密密钥的 KeyGenerator 对象  --*/
		KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
		/**-- generateKey()生成一个密钥  --*/
		SecretKey secretKey = keyGenerator.generateKey(); 
		/**-- secretKey.getEncoded()返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null --
		 * encryptBASE64调用Base64加密方法
		 */
		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);   
	}

}
 
 
下面是main方法类:
 
 
package com.boxun.crm.test;

import java.math.BigInteger;


/**
 * 
 * @author 旦旦而学
 * @version 1.0
 * @since 1.0
 */
public class CoderTest {
	
	public void test() throws Exception {
		String inputStr = "单项加密";
		System.err.println("原值:\t" + inputStr);

		byte[] inputData = inputStr.getBytes();
		String code = Base64Test.encryptBASE64(inputData);

		System.err.println("BASE64加密后:\t" + code);

		byte[] output = Base64Test.decryptBASE64(code);

		String outputStr = new String(output);

		System.err.println("BASE64解密后:\t" + outputStr);

		// 验证BASE64加密解密一致性
		assertEquals(inputStr, outputStr);

		// 验证MD5对于同一内容加密是否一致
		assertArrayEquals(Base64Test.encryptMD5(inputData), Base64Test
				.encryptMD5(inputData),"MD5");

		// 验证SHA对于同一内容加密是否一致
		assertArrayEquals(Base64Test.encryptSHA(inputData), Base64Test
				.encryptSHA(inputData),"SHA");

		String key = Base64Test.initMacKey();
		System.err.println("Mac密钥:\t" + key);

		// 验证HMAC对于同一内容,同一密钥加密是否一致
		assertArrayEquals(Base64Test.encryptHMAC(inputData, key), Base64Test.encryptHMAC(
				inputData, key),"HMAC");

		BigInteger md5 = new BigInteger(Base64Test.encryptMD5(inputData));
		System.err.println("MD5:\t" + md5.toString(16));

		BigInteger sha = new BigInteger(Base64Test.encryptSHA(inputData));
		System.err.println("SHA:\t" + sha.toString(32));

		BigInteger mac = new BigInteger(Base64Test.encryptHMAC(inputData, inputStr));
		System.err.println("HMAC:\t" + mac.toString(16));
	}

	private void assertArrayEquals(byte[] encryptMD5, byte[] encryptMD52,String type) {
		String out = "不一致" ;
		byte[] by = encryptMD5 ;
		byte[] by2 = encryptMD52 ;
		String byt = "" ;
		String byt2 = "" ;
		//转换成16进制数据
		for (int i = 0; i < by.length; i++) {
			byt += Integer.toHexString((by[i] & 0xFF) | 0x100).substring(1,3);
		}
		for (int i = 0; i < by2.length; i++) {
			byt2 += Integer.toHexString((by2[i] & 0xFF) | 0x100).substring(1,3);
		}
		if(byt.equals(byt2)){
			out = "一致" ;
		}
		System.out.println("验证"+type+"对于同一内容,同一密钥加密是否一致:"+out);
		System.out.println(byt + "  "+type+"  " + byt2);
	}

	private void assertEquals(String inputStr, String outputStr) {
		String out = "不一致" ;
		if(inputStr.equals(outputStr)){
			out = "一致" ;
		}
		System.out.println("验证BASE64加密解密一致性:"+out);
	}
	/**
	 * 2011-7-5上午10:43:51
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		CoderTest te = new CoderTest();
		te.test();
	}



}

原文参考:http://security.group.iteye.com/group/wiki/1710-one-way-encryption-algorithm


你可能感兴趣的:(java,exception,加密,String,解密,byte)