zoukankan      html  css  js  c++  java
  • PBKDF2加密

    password - the password that needs to be hashed. This should be converted
    into a char array before passing.

    salt- salt value that should append to the password.

    iterations- no. of iterations to be done. This value can be used to adjust the speed of the algorithm.

    keyLength- This is the required output length of the hashed function.

    This function returns a byte array that needs to be converted into a string using a suitable hex encoder.

     

    需要注意的是,加密的结果是字节数组。在存储到数据库的时候,可以转换为十六进制的字符串或者Base64字符串

            [Test]
            public void Pbkdf2Test()
            {
    
                string saltString = "8291f825-5772-4b3b-a28c-18887099f6d4";
                var array = Encoding.UTF8.GetBytes(saltString);
                GetHexString(array, 16);
                GetHexString(array, 128);
                GetHexString(array,256);
                GetHexString(array,512);
    
                var rfc2898DeriveBytes = new Rfc2898DeriveBytes("123", array, 4000);
                var result2 = rfc2898DeriveBytes.GetBytes(40);
                Console.WriteLine($"加密结果数组长度{result2.Length}");
                var string2 = Convert.ToBase64String(result2);
                Console.WriteLine(string2);
                Console.WriteLine(string2.Length);
            }
    
            public void GetHexString(byte[] array, int keyLength)
            {
                var rfc2898DeriveBytes = new Rfc2898DeriveBytes("123", array, 4000);
                var result = rfc2898DeriveBytes.GetBytes(keyLength);
                Console.WriteLine($"加密结果数组长度{result.Length}");
    
                var hexString = ByteArrayToString(result);
                Console.WriteLine(hexString);
                Console.WriteLine("========Split========");
            }
    
            public static string ByteArrayToString(byte[] ba)
            {
                var hex = new StringBuilder(ba.Length * 2);
                foreach (var b in ba)
                    hex.AppendFormat("{0:x2}", b);
                return hex.ToString();
            }

     

  • 相关阅读:
    C# 数据为空,不能对NULL调用此方法或属性的解决办法
    Hadoop开启后jps显示只有jps
    Ubuntu中eclipse端口被占
    Ubuntu在终端执行命令时出现的错误
    sudo passwd root输入普通用户密码后显示用户不再sudoers文件中
    周总结(4.4)
    《构建之法》读后感(三)
    周总结(3.28)
    软件工程团队项目介绍
    解决phpstudy中nginx服务器运行项目报错404问题
  • 原文地址:https://www.cnblogs.com/chucklu/p/10240417.html
Copyright © 2011-2022 走看看