在企业级的应用开发中,避免不了对信息进行加密和解密。那么,在使用Java开发应用的时候,我们使用什么手段对信息进行加密和解密呢?接下来介绍常见的四种单向加密技术。
package com.cn.encrypt; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class BASE64 { /* * BASE64加密 * */ public static String encryptBASE64(byte[] key)throws Exception{ return (new BASE64Encoder()).encodeBuffer(key); } /* * BASE64解密 * */ public static byte[] decryptBASE64(String key)throws Exception{ return (new BASE64Decoder()).decodeBuffer(key); } }
package com.cn.encrypt.test; import com.cn.encrypt.BASE64; public class TestEncrypt { public static void main(String[] args) throws Exception { String str0="123456"; System.out.println("原文:"+str0); String str1=BASE64.encryptBASE64(str0.getBytes()); System.out.println("BASE64加密后:"+str1); byte[] str2=BASE64.decryptBASE64(str1); String outputStr = new String(str2); System.out.println("BASE64解密后:"+outputStr); } }测试结果:
package com.cn.encrypt; import java.security.MessageDigest; public class MD5 { public static final String KEY_MD5 = "MD5"; public static byte[] encryptMD5(byte[] data)throws Exception{ MessageDigest md5=MessageDigest.getInstance(KEY_MD5); md5.update(data); return md5.digest(); } }
package com.cn.encrypt.test; import java.math.BigInteger; import com.cn.encrypt.MD5; public class TestEncrypt { public static void main(String[] args) throws Exception { String str0="123456"; System.out.println("原文:"+str0); byte[] str1=MD5.encryptMD5(str0.getBytes()); BigInteger bigInteger = new BigInteger(str1); //将大数转换成16进制的字符串 System.out.println("MD5加密后:"+bigInteger.toString(16)); } }
package com.cn.encrypt; import java.security.MessageDigest; public class SHA { public static final String KEY_SHA = "SHA"; public static byte[] encryptSHA(byte[] data)throws Exception{ MessageDigest sha=MessageDigest.getInstance(KEY_SHA); sha.update(data); return sha.digest(); } }
package com.cn.encrypt.test; import java.math.BigInteger; import com.cn.encrypt.SHA; public class TestEncrypt { public static void main(String[] args) throws Exception { String str0="123456"; System.out.println("原文:"+str0); byte[] str1=SHA.encryptSHA(str0.getBytes()); BigInteger bigInteger = new BigInteger(str1); //将大数转换成32进制的字符串 System.out.println("SHA加密后:"+bigInteger.toString(32)); } }
package com.cn.encrypt; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class HMAC { public static final String KEY_MAC = "HmacMD5"; /* * 初始化HMAC密钥 * */ public static String initMacKey()throws Exception{ KeyGenerator keyGenerator=KeyGenerator.getInstance(KEY_MAC); SecretKey secretKey=keyGenerator.generateKey(); return BASE64.encryptBASE64(secretKey.getEncoded()); } /* * HMAC加密 * */ public static byte[] encryptHMAC(byte[] data,String key)throws Exception{ SecretKey secretKey=new SecretKeySpec(BASE64.decryptBASE64(key),KEY_MAC); Mac mac=Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return mac.doFinal(data); } }
package com.cn.encrypt.test; import com.cn.encrypt.HMAC; public class TestEncrypt { private static String key=null; public static void main(String[] args) throws Exception { try { String inputStr = "123456"; /*使用同一密钥:对数据进行加密:查看两次加密的结果是否一样*/ getResult1(inputStr); System.out.println("========================="); getResult2(inputStr,key); } catch (Exception e) { e.printStackTrace(); } } public static String getResult1(String inputStr){ System.out.println("(1)加密之前的数据:"+inputStr); String result=null; try { byte[] inputData=inputStr.getBytes(); key=HMAC.initMacKey();//产生密钥 System.out.println("(1)Mac密钥:"+key); //利用密钥加密信息 result=new String(HMAC.encryptHMAC(inputData, key)); System.out.println("(1)HMAC加密后:"+result); } catch (Exception e) { e.printStackTrace(); } return result.toString(); } public static String getResult2(String inputStr,String key0){ System.out.println("(2)加密之前的数据:"+inputStr); String result=null; try { byte[] inputData=inputStr.getBytes(); //使用getResult1的密钥 System.out.println("(2)Mac密钥:"+key0); //利用密钥加密信息 result=new String(HMAC.encryptHMAC(inputData, key0)); System.out.println("(2)HMAC加密后:"+result); } catch (Exception e) { e.printStackTrace(); } return result.toString(); } }