java支持md5鹽值加密和des加密。
做項目時,某些模塊添加加密功能可以提高用戶個人信息安全性,防止信息泄露,其中des支持加密解密,MD5目前只支持加密(多用於用戶登錄密碼驗證,所以無需解密展示)。
一、MD5鹽值加密
1.在pom文件中導入相關jar包
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
2.編寫MD5類
import org.apache.shiro.crypto.hash.SimpleHash;
/**
* 加密工具類
* @author john
*
*/
public class MD5 {
//加密類型
private static String hashName="MD5";
//加密次數
private static int hashNum=1024;
//pwd是需要加密的字符,salt是鹽值(自定義),hashNum是加密次數,次數越多越安全
public static Object getMD5(String pwd,String salt){
Object obj=new SimpleHash(hashName, pwd, salt, hashNum);
return obj;
}
}
加密方法是靜態方法,使用時直接MD5.getMD5(pwd,salt).toString();即可。暫無解密方法。
二、Base64加密
1.同樣第一步也是導入base相關jar包
<!-- Base64 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
2.編寫base64類
import org.apache.commons.codec.binary.Base64;
public class Base64Utils {
/**
* 加密
*
* @param plainText
* @return
*/
public static String encodeStr(String plainText) {
byte[] b = plainText.getBytes();
Base64 base64 = new Base64();
b = base64.encode(b);
return new String(b);
}
/**
* 解密
*
* @param encodeStr
* @return
*/
public static String decodeStr(String encodeStr) {
byte[] b = encodeStr.getBytes();
Base64 base64 = new Base64();
b = base64.decode(b);
return new String(b);
}
}
加密解密的方法同樣是靜態方法,直接類名.方法名調用即可。
三、des加密
這個不需要導jar包,直接使用即可,但代碼量較多
import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* DES加密 解密算法
*
*/
public class DesUtil {
private final static String DES = "DES";
private final static String ENCODE = "GBK"; //設置編碼形式,防止亂碼
private final static String defaultKey = "test1234";
/**
* 使用 默認key 加密
*
*/
public static String encrypt(String data) throws Exception {
byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* 使用 默認key 解密
*
*/
public static String decrypt(String data) throws IOException, Exception {
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE));
return new String(bt, ENCODE);
}
/**
* Description 根據鍵值進行加密
*
*/
public static String encrypt(String data, String key) throws Exception {
byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* Description 根據鍵值進行解密
*
*/
public static String decrypt(String data, String key) throws IOException,
Exception {
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, key.getBytes(ENCODE));
return new String(bt, ENCODE);
}
/**
* Description 根據鍵值進行加密
*
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數據創建DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密鑰初始化Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
/**
* Description 根據鍵值進行解密
*
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數據創建DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密鑰初始化Cipher對象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
public static void main(String[] args) throws Exception {
String test = "今晚打老虎5.11ca";
//加密解密
System.out.println(encrypt(test));
System.out.println(decrypt(encrypt(test)));
}
}
靜態方法,直接調用