赞
踩
上篇文章bcrypt加密password BCrypt对密码进行加密及密码验证中使用的是BCrypt,本篇使用BCrypt.Net.BCrypt,其实和BCrypt差不多,只不过是NuGet程序包管理器使用的一个是BCrypt,一个是BCrypt.Net.本文详细介绍BCrypt.Net的使用
1、新建项目ConsoleBCrypt,使用NuGet程序包管理器添加BCrypt
2、Program.cs中添加如下代码:
- using DevOne.Security.Cryptography.BCrypt;
- using System;
-
- namespace ConsoleBCrypt
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("BCrypt.Net.BCrypt");
- string salt = BCrypt.Net.BCrypt.GenerateSalt(28);
- Console.WriteLine($"产生随机盐 salt:{salt}");
- salt = BCrypt.Net.BCrypt.GenerateSalt();
- Console.WriteLine($"产生随机盐 salt:{salt}");
- string password = "1234567890";
- Console.WriteLine($"明文:{password}");
- string pwd = BCrypt.Net.BCrypt.HashPassword(password);
- Console.WriteLine($"加密以后的密文:{pwd}");
- pwd = BCrypt.Net.BCrypt.HashPassword(password, 4);
- Console.WriteLine($"加密以后的密文:{pwd}");
- pwd = BCrypt.Net.BCrypt.HashPassword(password, salt);
- Console.WriteLine($"加密以后的密文:{pwd}");
- pwd = BCrypt.Net.BCrypt.HashString("密文");
- Console.WriteLine($"加密以后的密文:{pwd}");
- pwd = BCrypt.Net.BCrypt.HashString("密文", 4);
- Console.WriteLine($"加密以后的密文:{pwd}");
- bool isMatchpasswordAndpwd = BCrypt.Net.BCrypt.Verify("密文", pwd);
- Console.WriteLine($"明文和加密以后的密文是否匹配:{isMatchpasswordAndpwd}");
- Console.ReadLine();
- }
- }
- }

3、运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。