BCrypt對密碼進行加密及密碼驗證


上篇文章bcrypt加密password BCrypt對密碼進行加密及密碼驗證中使用的是BCrypt,本篇使用BCrypt.Net,其實和BCrypt差不多,只不過是NuGet程序包管理器使用的一個是BCrypt,一個是BCrypt.Net.本文詳細介紹BCrypt.Net的使用

1、新建項目ConsoleBCrypt,使用NuGet程序包管理器添加BCrypt.Net

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、運行結果:

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM