首页 > 代码库 > 兔子--HMAC加密算法

兔子--HMAC加密算法

	public static String initMacKey() throws Exception {

		KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
		SecretKey secretKey = keyGenerator.generateKey();
		return Base64.encodeToString(secretKey.getEncoded(), Base64.DEFAULT);
	}

	public String encrypt_HMAC(String source, String key) throws Exception {
		SecretKey secretKey = new SecretKeySpec(Base64.decode(key,
				Base64.DEFAULT), "HmacMD5");

		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		mac.init(secretKey);
		return Base64.encodeToString(mac.doFinal(source.getBytes()),
				Base64.DEFAULT);

	}
	//使用
	try{
		String key=initMacKey();
		String result=encrypt_HMAC("哈哈哈", key);
		
	}catch(Exception e){
		
	}

兔子--HMAC加密算法