(七) shiro 加密与解密

1 项目结构图


(七) shiro 加密与解密_第1张图片


2 代码

Encryption.java

package nufront.shiro.util;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.crypto.hash.Md5Hash;

/**
 * 这里是shiro加密
 * */
public class Encryption {
	
	//加密解密次数
	private static int number = 6;
	
	/**
	 * 封装好的shiro加密
	 * */
	public static String encode(String souce){
		for(int i = 0 ; i < number ;i++){
			souce = encBase64(souce);
		}
		return souce;
	}
	
	/**
	 * 封装好的shiro解密
	 * */
	public static String decode(String souce){
		for(int i = 0 ; i < number ;i++){
			souce = decBase64(souce);
		}
		return souce;
	}
	
	
	
	
	/**
	 * shiro Base64加密
	 * */
	public static String encBase64(String souce){
		return Base64.encodeToString(souce.getBytes());
	}
	
	/**
	 * shiro Base64解密
	 * */
	public static String decBase64(String souce){
		return Base64.decodeToString(souce);
	}

	/**
	 * souce需要加密的字符串
	 * salt自定义盐
	 * */
	public static String md5(String souce,String salt){
		return new Md5Hash(souce,salt).toString();
	}
	
	public static void main(String args[]){
		String password = "jiangpeng241241";
		String enCode = Encryption.encode(password);
		String deCode = Encryption.decode(enCode);
		System.out.println("解密之前 : " + password );
		System.out.println("解密后 : " + enCode );
		System.out.println("还原后 : " + deCode );
		
	}
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>shiro.leanring</groupId>
  <artifactId>shiro</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	      <version>4.12</version>
	      <scope>test</scope>
    </dependency>
    <dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>3.1.0</version>
	</dependency>
    <dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>javax.servlet.jsp-api</artifactId>
		<version>2.3.1</version>
	</dependency>
    <dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>
    <dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
    </dependency>
    <dependency>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
		<version>1.2</version>
	</dependency>
  
  
  
  
  <!-- 添加 shiro需要一些包 开始 -->
  
    <dependency>
		 <groupId>org.apache.shiro</groupId>
		 <artifactId>shiro-core</artifactId>
		 <version>1.2.4</version>
	</dependency>
	<dependency>
		<groupId>org.apache.shiro</groupId>
		<artifactId>shiro-web</artifactId>
		<version>1.2.4</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.12</version>
	</dependency> 
	
  <!-- 添加 shiro需要一些包 结束 -->
   <!-- 添加数据源 开始 -->
    <dependency>
		<groupId>c3p0</groupId>
		<artifactId>c3p0</artifactId>
		<version>0.9.1.2</version>
	</dependency>
	<dependency>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
		<version>1.2</version>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.37</version>
	</dependency>
  <!-- 添加数据源 结束 -->

  </dependencies>
</project>


3 结果

运行Encryption.java

(七) shiro 加密与解密_第2张图片

你可能感兴趣的:((七) shiro 加密与解密)