(九) 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 = javax.crypto.spec.SecretKeySpec@173a5
原始值 : AesEncoding
加密值 : d4bbd7b1c184e09996e80e443a5e05c6c9cfc1257d4762105ad08341d32e5ea5
解密值 : AesEncoding



你可能感兴趣的:(shiro)