(七)shiro之编码/加密
一、编码/解码
- 使用Base64编码/解码操作
public class TestMain {
public static void main(String[] args) {
SecurityManager securityManager=new IniSecurityManagerFactory("classpath:shiro.ini").getInstance();
SecurityUtils.setSecurityManager(securityManager); String str="hello";
String base64String=Base64.encodeToString(str.getBytes()); String decodeStr=Base64.decodeToString(base64String); System.out.println(str==decodeStr);
System.out.println(str);
System.out.println(decodeStr);
System.out.println(str.equals(decodeStr));
}
}
结果:
- 使用16进制字符串编码/解码操作
public class TestMain {
public static void main(String[] args) {
SecurityManager securityManager=new IniSecurityManagerFactory("classpath:shiro.ini").getInstance();
SecurityUtils.setSecurityManager(securityManager); String str="hello";
String base64String=Hex.encodeToString(str.getBytes()); String decodeStr=new String(Hex.decode(base64String.getBytes())); System.out.println(str==decodeStr);
System.out.println(str);
System.out.println(decodeStr);
System.out.println(str.equals(decodeStr)); }
}
二、散列算法
- 散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5、SHA等。一般进行散列时最好提供一个salt(盐),比如加密密码“admin”,产生的散列值是“21232f297a57a5a743894a0e4a801fc3”,可以到一些md5解密网站很容易的通过散列值得到密码“admin”,即如果直接对密码进行散列相对来说破解更容易,此时我们可以加一些只有系统知道的干扰数据,如用户名和ID(即盐);这样散列的对象是“密码+用户名+ID”,这样生成的散列值相对来说更难破解。
2.1 案例一
public class TestMain {
public static void main(String[] args) { String userName="admin";
String passWord="123520";
String userId="1"; /**
* 通过盐"123520"和"1"MD5散列“admin”。另外散列时还可以指定散列次数,如2次表示:md5(md5(str)):“new Md5Hash(str, salt, 2).toString()”。
*/
String md5=new Md5Hash(userName, passWord+userId).toString();
System.out.println(md5); /**
* 使用SHA256算法生成相应的散列数据,另外还有如SHA1、SHA512算法。
*/
String sha1 = new Sha256Hash(userName, passWord+userId).toString();
System.out.println(sha1);
}
}
结果:
2.2 使用HashService,默认提供了DefaultHashService实现。
public class TestMain {
public static void main(String[] args) { DefaultHashService hashService = new DefaultHashService(); //默认算法SHA-512
hashService.setHashAlgorithmName("SHA-512");
hashService.setPrivateSalt(new SimpleByteSource("123")); //私盐,默认无
hashService.setGeneratePublicSalt(true);//是否生成公盐,默认false
hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//用于生成公盐。默认就这个
hashService.setHashIterations(1); //生成Hash值的迭代次数 HashRequest request = new HashRequest.Builder()
.setAlgorithmName("MD5").setSource(ByteSource.Util.bytes("hello"))
.setSalt(ByteSource.Util.bytes("123")).setIterations(2).build();
String hex = hashService.computeHash(request).toHex();
System.out.println(hex);
}
}
1、首先创建一个DefaultHashService,默认使用SHA-512算法;
2、可以通过hashAlgorithmName属性修改算法;
3、可以通过privateSalt设置一个私盐,其在散列时自动与用户传入的公盐混合产生一个新盐;
4、可以通过generatePublicSalt属性在用户没有传入公盐的情况下是否生成公盐;
5、可以设置randomNumberGenerator用于生成公盐;
6、可以设置hashIterations属性来修改默认加密迭代次数;
7、需要构建一个HashRequest,传入算法、数据、公盐、迭代次数。
三、加密/解密
public class TestMain {
public static void main(String[] args) { String str = "hello";
AesCipherService aesCipherService = new AesCipherService();
//设置key长度
aesCipherService.setKeySize(128);
//生成key
Key key = aesCipherService.generateNewKey(); //加密,然后吧加密结果转为Hex编码
String encrptText = aesCipherService.encrypt(str.getBytes(), key.getEncoded()).toHex(); //解密,首先要把加密的结构用Hex解码后在解密
String decrpteText=new String(aesCipherService.decrypt(Hex.decode(encrptText),key.getEncoded()).getBytes()); System.out.println(str.equals(decrpteText)); System.out.println(encrptText); }
}
to be http://jinnianshilongnian.iteye.com/blog/2021439 5.4
(七)shiro之编码/加密的更多相关文章
- shiro中编码/加密
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- 跟开涛老师学shiro -- 编码/加密
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- Shiro笔记(四)编码/加密
Shiro笔记(四)编码/加密 一.编码和解码 //base64编码.解码 @Test public void testBase64(){ String str="tang"; b ...
- [AS3]as3用ByteArray来对SWF文件编码加密实例参考
[AS3]as3用ByteArray来对SWF文件编码加密实例参考,简单来说,就是将 swf 以 binary 的方式读入,并对 ByteArray 做些改变,再重新存成 swf 档.这个作业当然也可 ...
- shiro盐值加密并验证
在数据表中存的密码不应该是123456,而应该是123456加密之后的字符串,而且还要求这个加密算法是不可逆的,即由加密后的字符串不能反推回来原来的密码,如果能反推回来那这个加密是没有意义的.著名的加 ...
- 第五章 编码/加密——《跟我学Shiro》
转发地址:https://www.iteye.com/blog/jinnianshilongnian-2021439 目录贴:跟我学Shiro目录贴 在涉及到密码存储问题上,应该加密/生成密码摘要存储 ...
- 【Shiro学习之六】shiro编码/加密
apahce shiro:1.6.0 密码存储,应该加密/生成密码摘要存储,而不是存储明文密码. 1.编码/解码Shiro 提供了 base64和 16进制字符串编码/解码的API支持, 方便一些编码 ...
- shiro教程3(加密)
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容 概念 数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理, ...
- Apach Shiro MD5密码加密过程(明文生成密码过程)详细解析
前言: 最近再项目当中使用的ApachShiro安全框架,对于权限和服务器资源的保护都有一个很好的管理.前期主要参考的文章有 项目中设计密码的加盐处理以及二次加密问题,跟着断点 一步步揭开Apach ...
随机推荐
- DELPHI搭建centos开发环境
DELPHI搭建centos7开发环境 关闭防火墙 搭建开发环境,还是直接关闭LINUX防火墙,省事. 否则,使用到的网络端口号,都要在防火墙开放,麻烦. systemctl disable fire ...
- Linux系统中rm删除命令
rm命令 1.可以删除一个目录中的一个或多个文件或目录 2.可以将某个目录及其下属的所有文件及其子目录均删除掉 3.对于链接文件,只是删除整个链接文件,而原有文件保持不变 语法 rm (选项)(参数) ...
- Kafka、RabbitMQ、RocketMQ、ActiveMQ消息中间件的对比--多年生产经验实践总结
引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,前段时间我们自家的产品 RocketMQ (MetaQ的内核) 也顺利开源,得到大家的关注. ...
- Tosca database help link
https://support.tricentis.com/community/manuals_detail.do?lang=en&version=12.0.0&url=tosca_b ...
- OS X环境下如何搭建编译Cocos2D-X v3.x的Android Studio工程
Cocos2D-X官网已经简单介绍了如何在OS X环境下搭建Cocos2D-X v2.x和v3.x的指南.具体链接为:http://www.cocos.com/doc/article/index?ty ...
- 123457123457#0#-----com.yuming.ZuiNiuChengYu--前拼后广--最牛成语
com.yuming.ZuiNiuChengYu--前拼后广--最牛成语
- spark中的cache和persist的区别
在使用中一直知其然不知其所以然的地使用RDD.cache(),系统的学习之后发现还有一个与cache功能类似看起来冗余的persist 点进去一探究竟之后发现cache()是persist()的特例, ...
- Python基础之内置函数(二)
先上一张图,python中内置函数: python官方解释在这:点我点我 继续聊内置函数: callable(object):检查对象是否可被调用,或是否可执行,结果为bool值 def f1(): ...
- python根据数组数据绘图
转载自网络,版权归原作者所有 hello3.txt文件内部数据如下 ......7,2,6,-12,-10,-7,-1,2,9,...... python脚本 import numpy as np i ...
- 【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation
problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: i ...