在開發的時候,很多數據我們都希望是以加密過后的形式存儲起來,而不是最原始的數據。
在shiro中也提供了編碼,解碼,加密,加密算法實現等等一系列的內容。
編碼/解碼
在org.apache.shiro.codec包中,提供了Base64,16進制等的編碼解碼工具類的實現。
package com.fuwh.demo; import org.apache.shiro.codec.Base64; import org.apache.shiro.codec.Hex; public class CodecDemo { public static void main(String[] args) { String password="pass1234"; /* * Base64類提供了一些base64方式的編碼和解碼操作 */ System.out.println("Base64加密后:"+Base64.encodeToString(password.getBytes())); System.out.println("Base64解密后:"+Base64.decodeToString(Base64.encodeToString(password.getBytes()))); /* * Hex類提供了一些十六進制的編碼和解碼操作 */ System.out.println("Hex編碼后:"+Hex.encodeToString(password.getBytes())); System.out.println("Hex解碼后:"+new String(Hex.decode(Hex.encode(password.getBytes())))); } }
在這個包中,還有一個CodeSupport的類,提供了豐富的對象編碼,字符串編碼等等操作。
散列算法
在org.apache.shiro.crypto.hash包中,提供了一些列的Md2,Md5,Sha256等等的散列算法相關的操作。
package com.fuwh.demo; import org.apache.shiro.crypto.hash.Md5Hash; import org.apache.shiro.crypto.hash.Sha256Hash; public class HashDemo { public static void main(String[] args) { String password="pass1234"; /* * Md5散列解密,通常用來加密密碼 * 在散列解密的時候,可以指定鹽(salt)和加密的次數 * 鹽用來提高加密的復雜度,因為彈出的Md5加密還是可能被破解 * 但是,加上一個只有系統知道的鹽就基本上不會被破解了 * 加密次數,用來提高加密的復雜度 */ Md5Hash md5Hash1=new Md5Hash(password); Md5Hash md5Hash2=new Md5Hash(password, "123"); Md5Hash md5Hash3=new Md5Hash(password, "123",2); System.out.println("Md5加密--不加鹽:"+md5Hash1); System.out.println("Md5加密--加鹽:"+md5Hash2); System.out.println("Md5加密--加鹽--二次加密:"+md5Hash3); /* * Sha256Hash */ Sha256Hash sha256Hash1=new Sha256Hash(password); Sha256Hash sha256Hash2=new Sha256Hash(password, "123"); Sha256Hash sha256Hash3=new Sha256Hash(password, "123",2); System.out.println("Sha256Hash加密--不加鹽:"+sha256Hash1); System.out.println("Sha256Hash加密--加鹽:"+sha256Hash2); System.out.println("Sha256Hash加密--加鹽--二次加密:"+sha256Hash3); } }
當前,還有一些其他的實現。
在這個包中,還提供了一個可以個性化定制可重用的加密類,可以定制加密策略,隨機鹽,多次加密等等。
package com.fuwh.demo; import org.apache.shiro.crypto.SecureRandomNumberGenerator; import org.apache.shiro.crypto.hash.DefaultHashService; import org.apache.shiro.crypto.hash.HashRequest; import org.apache.shiro.util.SimpleByteSource; public class HashServiceDemo { public static void main(String[] args) { /* * 構建一個HashService * 默認情況下: * 散列算法:SHA-512 * 循環次數:1 * 不生成公鹽 */ DefaultHashService service=new DefaultHashService(); service.setHashAlgorithmName("SHA-512");//設置加密算法,默認就是這個 service.setPrivateSalt(new SimpleByteSource("123"));//設置私鹽 service.setGeneratePublicSalt(true);//設置生成公研 service.setRandomNumberGenerator(new SecureRandomNumberGenerator());//設置公鹽的生成方式 service.setHashIterations(1);//設置加密次數 /* * 構建一個HashRequest里面包含了HashService加密需要的一些信息。 */ HashRequest request=new HashRequest.Builder() .setAlgorithmName("MD5") .setSalt("12345") .setSource("pass1234") .setIterations(2) .build(); System.out.println(service.computeHash(request).toString()); } }
加密/解密
package com.fuwh.demo; import java.security.Key; import org.apache.shiro.crypto.AesCipherService; public class AesCipherServiceDemo { public static void main(String[] args) { AesCipherService acs=new AesCipherService(); String pass="pass1234"; Key key=acs.generateNewKey(); System.out.println(acs.encrypt(pass.getBytes(), key.getEncoded())); System.out.println(acs.encrypt(pass.getBytes(), key.getEncoded()).toString()); System.out.println(acs.encrypt(pass.getBytes(), key.getEncoded()).toHex()); System.out.println(acs.encrypt(pass.getBytes(), key.getEncoded()).toBase64()); } }