PBKDF2加密
2024-10-20 03:39:43
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, );
GetHexString(array, );
GetHexString(array,);
GetHexString(array,); var rfc2898DeriveBytes = new Rfc2898DeriveBytes("", array, );
var result2 = rfc2898DeriveBytes.GetBytes();
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("", array, );
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 * );
foreach (var b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
最新文章
- 构建高性能的MYSQL数据库系统
- HUSTOJ搭建实录
- SQL总结(三)其他查询
- iOS从不同页面跳转回到指定控制器
- Java_SSH项目主要步骤记录
- Cmd批处理语法实例
- CSS定位深入理解 完全掌握CSS定位 相对定位和绝对定位
- Docker学习总结之Run命令介绍
- Angular基础(二)
- Delphi中打开网页连接的几种方法
- MySql主从搭建详细步骤
- Java中 try--catch-- finally、throw、throws 的用法
- syzkaller 资料集合
- RabbitMQ学习之旅(一)
- Kinect v2 记录
- SpringMVC RedirectView的使用以及源码分析
- 多态时最好将基类的析构函数设为virtual、 C++中两个类相互包含引用问题 (转载)
- layer 弹出层 不居中
- BZOJ1822 Frozen Nova 冷冻波
- gpfs 内核错误