java数据加密算法
需要commons-codec-1.7.jar包,下载地址:http://commons.apache.org/codec/download_codec.cgi
直接看我改过的代码(栗子里还要转换成byte数组再转换回来太麻烦,我重新封装了栗子里操作BASE64的方法,用的时候直接传进去需要加密的String,它就返回加完密的字符串,方便使用。需要加密的同学直接拿下面代码用就好。不过BASE64好像可以直接传String参数,反正都一样 无所谓了):
1 import org.apache.commons.codec.binary.Base64; 2 3 public class MyEncrypt { 4 5 // sun不推荐使用它们自己的base64,用apache的挺好 6 /** 7 * BASE64解密 8 */ 9 public static String decryptBASE64(String dest) { 10 if (dest == null) { 11 return null; 12 } 13 byte[] d = dest.getBytes(); 14 String s = new String(Base64.decodeBase64(d)); 15 return s; 16 } 17 18 /** 19 * BASE64加密 20 */ 21 public static String encryptBASE64(String origin) { 22 if (origin == null) { 23 return null; 24 } 25 byte [] d = origin.getBytes(); 26 String s = new String(Base64.encodeBase64(d)); 27 return s; 28 } 29 public static void main(String[] args) throws Exception { 30 // TODO Auto-generated method stub 31 String d = "Akishimo"; 32 String ed = encryptBASE64(d); 33 String edd = decryptBASE64(ed); 34 System.out.println("我的密码为:"+d); 35 System.out.println("加密后为;"+ ed); 36 System.out.println("再解密之后:"+edd); 37 } 38 }
这个是网上找的栗子:
package com.qfnu.officeoa.util.encrypt; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class MyEncrypt { public static final String KEY_SHA = "SHA"; public static final String KEY_MD5 = "MD5"; public static final String KEY_MAC = "HmacMD5"; // sun不推荐使用它们自己的base64,用apache的挺好 /** * BASE64解密 */ public static byte[] decryptBASE64(byte[] dest) { if (dest == null) { return null; } return Base64.decodeBase64(dest); } /** * BASE64加密 */ public static byte[] encryptBASE64(byte[] origin) { if (origin == null) { return null; } return Base64.encodeBase64(origin); } /** * MD5加密 * * @throws NoSuchAlgorithmException */ public static byte[] encryptMD5(byte[] data) throws NoSuchAlgorithmException { if (data == null) { return null; } MessageDigest md5 = MessageDigest.getInstance(KEY_MD5); md5.update(data); return md5.digest(); } /** * SHA加密 * * @throws NoSuchAlgorithmException */ public static byte[] encryptSHA(byte[] data) throws NoSuchAlgorithmException { if (data == null) { return null; } MessageDigest sha = MessageDigest.getInstance(KEY_SHA); sha.update(data); return sha.digest(); } /** * 初始化HMAC密钥 * * @throws NoSuchAlgorithmException */ public static String initMacKey() throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC); SecretKey secretKey = keyGenerator.generateKey(); return new String(encryptBASE64(secretKey.getEncoded())); } /** * HMAC加密 * * @throws NoSuchAlgorithmException * @throws InvalidKeyException */ public static byte[] encryptHMAC(byte[] data, String key) throws NoSuchAlgorithmException, InvalidKeyException { SecretKey secretKey = new SecretKeySpec(decryptBASE64(key.getBytes()), KEY_MAC); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return mac.doFinal(data); } public static void main(String[] args) throws Exception { // 例子里给的那一大长串传参调用太头疼,我简化一下写出来了,d加密之前的字符,ed代表加密之后,edd代表解密之后,大家直接拿来用就行。
String d = "Akishimo"; String ed = new String(encryptBASE64(d.getBytes())); String edd = new String(decryptBASE64(ed.getBytes())); System.out.println("我的密码为:"+d); System.out.println("加密后为;"+ ed); System.out.println("再解密之后"+edd); System.out.println("****************************************************************"); String data = "简单加密"; String a =new BigInteger(encryptBASE64(data.getBytes())) .toString(32); System.out.println(new BigInteger(encryptBASE64(data.getBytes())).toString(16)); System.out.println(a); System.out.println("haha"+new String(decryptBASE64(encryptBASE64(data.getBytes())))); System.out.println(new BigInteger(encryptMD5(data.getBytes())) .toString()); System.out.println(new BigInteger(encryptSHA(data.getBytes())) .toString()); System.out.println(new BigInteger(encryptHMAC(data.getBytes(), initMacKey())).toString()); } }
这里面有好几种加密方法,我觉得BASE64好用就只用了这种,其他的使用方法例子里也有,大家自己拿来用就行。原文地址http://justsee.iteye.com/blog/747824