美文网首页工作生活
5.散列算法(加密算法)

5.散列算法(加密算法)

作者: __元昊__ | 来源:发表于2019-07-02 16:49 被阅读0次

1.在身份认证过程中往往会涉及加密,如果不加密那么数据信息不安全,Shiro内部实现比较多的散列算法。如MD5,SHA等。并且提供了加盐功能,如1111的md5码为b59c67bf196a4758191e42f76670ceba,这个md5码有很多破解网站上可以破解。但是如果1111+姓名,那么破解的难度会增加。

2.测试MD5算法:

import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;

public class shiroMD5_demo {
    public static void main(String[] args) {
        //使用md5加密
        Md5Hash md5 = new Md5Hash("1111");
        System.out.println(md5.toString());
        //加盐
        md5 = new Md5Hash("1111", "lyh");
        System.out.println(md5.toString());
        //迭代次数
        md5 = new Md5Hash("1111", "lyh",10);
        System.out.println(md5.toString());
        //SimpleHash类也可以实现
        SimpleHash simpleHash = new SimpleHash("md5", "1111", "lyh", 10);
        System.out.println(simpleHash.toString());
    }
}

3.在自定义Realm中使用散列算法
ini配置文件:

[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#定义你要选择的散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列迭代次数
credentialsMatcher.hashIterations=10
#将定义好的凭证器设置进自定义Realm里
UserRealm=com.lyh.UserRealm_demo
UserRealm.credentialsMatcher=$credentialsMatcher

securityManager.realms=$UserRealm

[users]

自定义Realm代码:

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class UserRealm_demo extends  AuthorizingRealm{
    @Override
    public String getName() {
        return "UserRealm";
    }

    //授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //完成身份认证(我们只是取数据),并且返回身份信息,如果失败返回null
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取身份信息,获取用户输入的用户名
        String username = (String)authenticationToken.getPrincipal();
        System.out.println("username==========="+username);
        //根据用户名到数据库查询密码
        //模拟从数据库获得1111加密后的密码,获取加盐的salt值
        String pwd="2a3d662192d00d5a906a067a665aedcc";
        String salt="lyh";
        //将从数据库中查询的信息,封装到SimpleAuthenticationInfo中
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd, ByteSource.Util.bytes(salt),getName());
        return info;
    }
}

shiro测试代码:

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

public class jiami_demo {
    public static void main(String[] args) {
        //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro.ini");

        //2、得到SecurityManager实例 并绑定给SecurityUtils
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("wangwu", "1111");

        try {
            subject.login(token);
            if(subject.isAuthenticated()){
                System.out.println("验证通过");
            }
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("验证失败");
        }
    }
}

相关文章

  • 密码安全

    本文介绍密码安全相关的加密与散列算法。 目录 散列算法 加密算法对称加密非对称加密 散列算法 Hashing 是使...

  • 5.散列算法(加密算法)

    1.在身份认证过程中往往会涉及加密,如果不加密那么数据信息不安全,Shiro内部实现比较多的散列算法。如MD5,S...

  • Node前端加密(crypto)

    一、散列算法和加密算法区别 散列算法是单向加密不可逆的 加密算法可以加密,然后通过密钥解密 二、方法 获取支持的加...

  • RSA+AES加密

    前言 对称加密算法(不可逆的,传统加密算法)Hash加密算法/散列算法- DES (数据加密标准(用的少,因为强...

  • 单项散列函数

    1.单项散列函数的特点 2.经典加密算法 3.加密算法说明

  • iOS 逆向14 -- 密码学

    加密算法的分类 Hash哈希散列函数:严格意义上来说不属于加密算法,常见的散列函数有MD5、SHA1/256/51...

  • iOS逆向--加密算法

    一、加密算法分类 哈希(散列)函数:不属于加密算法。例如MD5、SHA1/256/512 对称加密算法:DES、3...

  • iOS 中 3DES加密、MD5加密

    加密算法通常分为对称性加密算法和非对称性加密算法,以及线性散列算法,对应着比较常见的是 DES,RSA,MD5。 ...

  • IOS 加密

    加密算法 首先了解一下加密算法,常见的加密算法可以分成三类,对称密钥加密、公开密钥加密、散列函数。 对称密钥加密 ...

  • 加密算法的理解

    加密算法按类型分类: 对称加密、非对称加密、散列算法 对称加密: 加密双方都持有加密算法及密钥 非对称加密: 加密...

网友评论

    本文标题:5.散列算法(加密算法)

    本文链接:https://www.haomeiwen.com/subject/tshthctx.html