(七)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 ...
随机推荐
- REST和SOAP的区别
转自:https://www.cnblogs.com/MissQing/p/7240146.html REST似乎在一夜间兴起了,这可能引起一些争议,反对者可以说REST是WEB诞生之始甚而是HTTP ...
- jQuery Ajax calls and the Html.AntiForgeryToken()
jQuery Ajax calls and the Html.AntiForgeryToken() https://stackoverflow.com/a/4074289/3782855 I use ...
- OpenJudge计算概论-最长单词2
/*======================================================================== 最长单词2 总时间限制: 1000ms 内存限制: ...
- 几种主流浏览器内置http抓包工具软件使用方
对于学习网站的人或者相关编程人员,经常需要用到http抓包工具来跟踪网页,但主流抓包软件如httpwatch.httpanalyzerstdv都是收费的,破解版往往也不稳定.实际上现在很多浏览器都内置 ...
- 【423】COMP9024 Revision
目录: array '\0' 与 EOF 二维字符数组(字符串数组) 1. array: 参考:C 数组 参考:C 字符串 参考:C笔记之NULL和字符串结束符'\0'和EOF 总结:[个人理解,可能 ...
- 【420】链表实现Quack
quack.h // quack.h: an interface definition for a queue/stack #include <stdio.h> #include < ...
- 实现下拉弹出视图和Block的简单实现
实现效果如下: 实现代码如下: @interface ViewController ()<UIViewControllerTransitioningDelegate> { UILabel ...
- Spring Cloud(7.1):安装Kafka和Redis
Kafka安装 (1)从官方(http://kafka.apache.org/downloads)下载安装包.kafka安装包和一般安装包的命名方式不一样,我们看一个kafka包命名:kafka_2. ...
- 【Leetcode_easy】1078. Occurrences After Bigram
problem 1078. Occurrences After Bigram 题意 solution: class Solution { public: vector<string> fi ...
- 【实验】ssh私钥泄露
翻自己的笔记看到之前做过的一个实验,一个关于ssh私钥泄露的实验,贴出来与大家交流. 做这种题脑洞需要特别大,而且也需要运气. 1.实验环境准备 2.实验流程 1)探测信息 用namp进行端口扫描,扫 ...