1.Base64加密算法
严格来说Base64并不是一种加密/解密算法,而是一种编码方式。Base64不生成密钥,通过Base64编码后的密文就可以直接“翻译”为明文,但是可以通过向明文中添加混淆字符来达到加密的效果。
主要就是BASE64Encoder加密类、BASE64Decoder解密类两个类,我们只需要知道使用对应的方法即可。
//**************************BASE64加密与解密************************************
/**
* 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);
}
测试
String inputStr = "简单加密";
System.err.println("原文:" + inputStr);
//**************************BASE64加密与解密************************************
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);
结果
2.MD5与SHA加密算法
可谓是非可逆加密,就是不可解密的加密方法,我们称之为单向加密算法。
//**************************MD5加密************************************
/**
* 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加密************************************
/**
* 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();
}
测试
//**************************MD5加密************************************
BigInteger md51 = new BigInteger(Coder.encryptMD5(inputData));
BigInteger md52 = new BigInteger(Coder.encryptMD5(inputData));
System.err.println("MD51:" + md51.toString());
System.err.println("MD52:" + md52.toString());
boolean md5equals = md51.equals(md52);
System.out.println("MD51==MD52="+ md5equals);
//**************************SHA加密************************************
BigInteger SHA1 = new BigInteger(Coder.encryptSHA(inputData));
BigInteger SHA2 = new BigInteger(Coder.encryptSHA(inputData));
System.err.println("SHA1:" + SHA1.toString());
System.err.println("SHA2:" + SHA2.toString());
boolean SHAequals = SHA1.equals(SHA2);
System.out.println("SHA1==SHA2="+ SHAequals);
结果
3.HMAC加密算法
初始化一个HMAC密钥,然后在通过密钥进行加密,对比是需要用到密钥对其加密再和一起加密后的结果进行对比。
/**************************初始化HMAC密钥************************************
/**
* 初始化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加密************************************
/**
* 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);
}
测试
//**************************HMAC加密************************************
String initMacKey = Coder.initMacKey();
BigInteger HMAC1 = new BigInteger(Coder.encryptHMAC(inputData,initMacKey));
BigInteger HMAC2 = new BigInteger(Coder.encryptHMAC(inputData,initMacKey));
System.err.println("HMAC1:" + HMAC1.toString());
System.err.println("HMAC2:" + HMAC2.toString());
System.out.println("Mac密钥="+initMacKey);
boolean HMACequals = HMAC1.equals(HMAC2);
System.out.println("HMAC1==HMAC2="+ HMACequals);
结果
全部类
public class Coder {
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";
//**************************BASE64加密与解密************************************
/**
* 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);
}
//**************************MD5加密************************************
/**
* 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加密************************************
/**
* 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密钥************************************
/**
* 初始化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加密************************************
/**
* 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);
}
public static void main(String[] args) throws Exception {
String inputStr = "简单加密";
System.err.println("原文:" + inputStr);
//**************************BASE64加密与解密************************************
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);
//**************************MD5加密************************************
BigInteger md51 = new BigInteger(Coder.encryptMD5(inputData));
BigInteger md52 = new BigInteger(Coder.encryptMD5(inputData));
System.err.println("MD51:" + md51.toString());
System.err.println("MD52:" + md52.toString());
boolean md5equals = md51.equals(md52);
System.out.println("MD51==MD52="+ md5equals);
//**************************SHA加密************************************
BigInteger SHA1 = new BigInteger(Coder.encryptSHA(inputData));
BigInteger SHA2 = new BigInteger(Coder.encryptSHA(inputData));
System.err.println("SHA1:" + SHA1.toString());
System.err.println("SHA2:" + SHA2.toString());
boolean SHAequals = SHA1.equals(SHA2);
System.out.println("SHA1==SHA2="+ SHAequals);
//**************************HMAC加密************************************
String initMacKey = Coder.initMacKey();
BigInteger HMAC1 = new BigInteger(Coder.encryptHMAC(inputData,initMacKey));
BigInteger HMAC2 = new BigInteger(Coder.encryptHMAC(inputData,initMacKey));
System.err.println("HMAC1:" + HMAC1.toString());
System.err.println("HMAC2:" + HMAC2.toString());
System.out.println("Mac密钥="+initMacKey);
boolean HMACequals = HMAC1.equals(HMAC2);
System.out.println("HMAC1==HMAC2="+ HMACequals);
}
}