gpt4 book ai didi

c# - 在使用 Bcrypt C# Salt 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:24 24 4
gpt4 key购买 nike

大家好,我目前正在尝试匹配我的 PHP 登录系统,它使用

define('PASSWORD_ENCRYPTION', "bcrypt"); 
define('PASSWORD_SHA512_ITERATIONS', 25000);
define('PASSWORD_BCRYPT_COST', "13");

define('PASSWORD_SALT', "/8Wncr26eAmxD1l6cAF9F8"); //22 characters to be appended on first 7 characters that will be generated using PASSWORD_ info above

我的 C#:

            string myPassword = this.password_txt.Text;
string mySalt = "$2a$13$/8Wncr26eAmxD1l6cAF9F8";
string hashed = BCrypt.HashPassword(myPassword, mySalt);

问题:我用 C# 创建的哈希密码与数据库中的登录 PHP 哈希密码不匹配...

最佳答案

你的完整功能看起来像这样

/**
* Hash given password.
* @param string $password Unhashed password.
* @return string Hashed password.
*/
public function hashPassword($password) {
//this salt will be used in both algorithms
//for bcrypt it is required to look like this,
//for sha512 it is not required but it can be used
$salt = "$2a$" . PASSWORD_BCRYPT_COST . "$" . PASSWORD_SALT;

if(PASSWORD_ENCRYPTION == "bcrypt") {
$newPassword = crypt($password, $salt);
}
else {
$newPassword = $password;
for($i=0; $i<PASSWORD_SHA512_ITERATIONS; $i++)
$newPassword = hash('sha512',$salt.$newPassword.$salt);
}

return $newPassword;
}

编辑: 更多研究表明 $2a$ 似乎是来自研究的 BlowFish Encryption。 ( http://php.net/crypt )

CRYPT_BLOWFISH - Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z". Using characters outside of this range in the salt will cause crypt() to return a zero-length string. The two digit cost parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithmeter and must be in range 04-31, values outside this range will cause crypt() to fail. Versions of PHP before 5.3.7 only support "$2a$" as the salt prefix: PHP 5.3.7 introduced the new prefixes to fix a security weakness in the Blowfish implementation. Please refer to » this document for full details of the security fix, but to summarise, developers targeting only PHP 5.3.7 and later should use "$2y$" in preference to "$2a$".

It must begin with "$2", optional "a", "$", two digits, "$", and 22 base 64 digits. The rest of the string is ignored. The presence of the optional "a" means that a NUL is to be appended to the password before it is used as a key. The two digits set the cost parameter. The 22 base 64 digits encode the salt.


$13$ 称为成本。 (循环加密多少次)。现在,盐本身以 base64 编码,您必须先对其进行解码,然后才能将其与 Eksblowfish

一起使用

Eksblowfish, states for (expensive key schedule blowfish, is a cost parameterizable and salted variation of the blowfish block cipher.)

无论如何总结它你必须使用这个
http://bcrypt.codeplex.com/

应该是这样的

int PASSWORD_BCRYPT_COST = 13;
string PASSWORD_SALT = "/8Wncr26eAmxD1l6cAF9F8";
string salt = "$2a$" + PASSWORD_BCRYPT_COST + "$" + PASSWORD_SALT;
string password "test123abc";
var hash = BCrypt.HashPassword(password, salt);
textBox1.Text = hash;

密码=test123abc
textbox1 的输出为:$2a$13$/8Wncr26eAmxD1l6cAF9FuVnazDlahXc73He5NB1GKNYG7v3mOOyS

C# test program screenshot

还在 php 中运行了您的代码 http://ideone.com/8piXMq

php 输出

echo hashPassword("test123abc");

php 的输出是:$2a$13$/8Wncr26eAmxD1l6cAF9FuVnazDlahXc73He5NB1GKNYG7v3mOOyS

C#
$2a$13$/8Wncr26eAmxD1l6cAF9FuVnazDlahXc73He5NB1GKNYG7v3mOOyS
PHP
$2a$13$/8Wncr26eAmxD1l6cAF9FuVnazDlahXc73He5NB1GKNYG7v3mOOyS

如您所见,答案都是相同


正如您从 BCrypt HashPassword 实现中看到的那样,它解码 base64 盐并使用您指定的盐重新加密您的新密码。

/// <summary>
/// Hash a password using the OpenBSD bcrypt scheme.
/// </summary>
/// <param name="password">The password to hash.</param>
/// <param name="salt">The salt to hash with (perhaps generated
/// using <c>BCrypt.GenerateSalt</c>).</param>
/// <returns>The hashed password.</returns>
public static string HashPassword(string password, string salt) {
if (password == null) {
throw new ArgumentNullException("password");
}
if (salt == null) {
throw new ArgumentNullException("salt");
}

char minor = (char)0;

if (salt[0] != '$' || salt[1] != '2') {
throw new ArgumentException("Invalid salt version");
}

int offset;
if (salt[1] != '$') {
minor = salt[2];
if (minor != 'a' || salt[3] != '$') {
throw new ArgumentException("Invalid salt revision");
}
offset = 4;
} else {
offset = 3;
}

// Extract number of rounds
if (salt[offset + 2] > '$') {
throw new ArgumentException("Missing salt rounds");
}

int rounds = Int32.Parse(salt.Substring(offset, 2), NumberFormatInfo.InvariantInfo);

byte[] passwordBytes = Encoding.UTF8.GetBytes(password + (minor >= 'a' ? "\0" : String.Empty));
byte[] saltBytes = DecodeBase64(salt.Substring(offset + 3, 22),
BCRYPT_SALT_LEN);

BCrypt bcrypt = new BCrypt();

byte[] hashed = bcrypt.CryptRaw(passwordBytes, saltBytes, rounds);

StringBuilder rs = new StringBuilder();

rs.Append("$2");
if (minor >= 'a') {
rs.Append(minor);
}
rs.Append('$');
if (rounds < 10) {
rs.Append('0');
}
rs.Append(rounds);
rs.Append('$');
rs.Append(EncodeBase64(saltBytes, saltBytes.Length));
rs.Append(EncodeBase64(hashed,
(bf_crypt_ciphertext.Length * 4) - 1));

return rs.ToString();
}

关于c# - 在使用 Bcrypt C# Salt 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21083833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com