1. 程式人生 > >(九) shiro採用AES加密和解密

(九) shiro採用AES加密和解密

1 程式碼

AesEncoding.java

package nufront.shiro.util;

import java.security.Key;

import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.AesCipherService;

public class AesEncoding {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AesCipherService aesCipherService = new AesCipherService();  
		aesCipherService.setKeySize(128); //設定key長度  
		//生成key  
		Key key = aesCipherService.generateNewKey();
		System.out.println("key = " + key);
		String text = "AesEncoding";  
		//加密  
		String encrptText =  aesCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();  
		//解密  
		String text2 = new String(aesCipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());
		System.out.println("原始值 : " + text);
		System.out.println("加密值 : " + encrptText);
		System.out.println("解密值 : " + text2);
		
	}

}
2 結果
key = [email protected]
原始值 : AesEncoding
加密值 : d4bbd7b1c184e09996e80e443a5e05c6c9cfc1257d4762105ad08341d32e5ea5
解密值 : AesEncoding