一、编码/解码

  • 使用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

最新文章

  1. IT基础架构规划方案一(网络系统规划)
  2. 《连载 | 物联网框架ServerSuperIO教程》- 7.自控通讯模式开发及注意事项
  3. C# 获取时间差状态
  4. mybatis Generator配置文件详解
  5. Spring浅探
  6. WP8.1 Study18:动态磁贴
  7. i++和++i
  8. 第一次sprint团队贡献分改
  9. until与till的用法归纳
  10. PHP中各种Hash算法性能比较
  11. 九度OJ 1042 Coincidence -- 动态规划(最长公共子序列)
  12. JavaScript 实现数组的foreach
  13. java decompiler如何去掉行号
  14. [Swust OJ 801]--Ordered Fractions
  15. php 知识点 --个人笔记
  16. SqlServer跨集群升级
  17. Matplotlib学习---用matplotlib画误差线(errorbar)
  18. Elasticsearch集成HanLP分词器-个人学习
  19. greenplum 安装笔记
  20. python框架之Django(5)-O/RM

热门文章

  1. mysql 实现同一个sql查询 分页数据 和总记录数
  2. Oracle数据库-primary key/foreign key和references关系
  3. JPG:文件格式系列科普之.JPEG/.JPG(转)
  4. c语言 GPS nmealib学习笔记
  5. CentOS7下搭建zabbix监控(二)——Zabbix被监控端配置
  6. (一)UML概览
  7. Python3之切片及内置切片函数slice
  8. netcore部署
  9. 【搬运】NumPy_for_Matlab_Users
  10. windows服务器入门 安装配置IIS和ASP