Shiro加密

在开发的时候,很多数据我们都希望是以加密过后的形式存储起来,而不是最原始的数据。

在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());
    }
}

查看源码:https://github.com/oukafu/shiro

时间: 2024-12-29 11:00:10

Shiro加密的相关文章

shiro权限控制(一):shiro介绍以及整合SSM框架

shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是否被允许做某事 会话管理:在任何环境下使用 Session API,即使没有 Web 或EJB 容器. 加密:以更简洁易用的方式使用加密功能,保护或隐藏数据防止被偷窥 Realms:聚集一个或多个用户安全数据的数据源 单点登录(SSO)功能. 为没有关联到登录的用户启用 "Remember Me&q

springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制

项目结构: 1.maven项目的pom中引入shiro所需的jar包依赖关系 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.ap

Shiro报错-[org.apache.shiro.mgt.AbstractRememberMeManager] - There was a failure while trying to retrieve remembered principals.

2017-04-08 11:55:33,010 WARN [org.apache.shiro.mgt.AbstractRememberMeManager] - There was a failure while trying to retrieve remembered principals. This could be due to a configuration problem or corrupted principals. This could also be due to a rece

Shiro安全框架【快速入门】就这一篇!

Shiro 简介 照例又去官网扒了扒介绍: Apache Shiro? is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro's easy-to-understand API, you can quickly and easily secure any ap

Shiro权限验证详细教程

一.Shiro介绍 1.可从本教程中明白以下几个知识点: 认识Shiro的整体架构和各组件的概念: Shiro认证.授权过程: Shiro自定义Realm. 2.Shiro简介 Shiro是Apache强大灵活的开源的安全框架,有认证.授权.企业会话管理.安全加密.缓存管理等功能. Shiro和Spring Security相比较:Shiro更加简单方便.并且可脱离Spring,Spring Security比较笨重复杂,不可脱离Spring. 3.Shiro架构图 详细图如上面所示:在这里就不

Shiro安全框架「快速入门」就这一篇

Shiro 简介 照例又去官网扒了扒介绍: Apache Shiro™ is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any ap

支持APP手机应用(android和ios)接口调用 ,传输验证可用 shiro 的 MD5、SHA 等加密

请认准本正版代码,售后技术有保障,代码有持续更新.(盗版可耻,违者必究)         此为本公司团队开发 ------------------------------------------------------------------------------------------------------------------------- 1. 有 oracle .msyql.spring3.0.spring4.0  一共 4 套版本全部提供没有打jar没有加密的源代码(最下面截图2

第五章 编码/加密——《跟我学Shiro》

在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shiro提供了base64和16进制字符串编码/解码的API支持,方便一些编码解码操作.Shiro内部的一些数据的存储/表示都使用了base64和16进制字符串. Java代码 String str = "hello"; String base64Encoded = Base64.encodeT

将 Shiro 作为应用的权限基础 五:密码的加密/解密在Spring中的应用

考虑系统密码的安全,目前大多数系统都不会把密码以明文的形式存放到数据库中. 一把会采取以下几种方式对密码进行处理 密码的存储 "编码"存储 Shiro 提供了 base64和 16 进制字符串编码/解码的 API支持,方便一些编码解码操作. Shiro内部的一些数据的存储/表示都使用了 base64和 16 进制字符串. 下面两端代码分别对其进行演示 Stringstr = "hello"; Stringbase64Encoded = Base64.encodeTo