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

posted @ 2017-09-02 13:21  Ouka傅  阅读(1834)  评论(0编辑  收藏  举报