我想验证用户表单asp.net Web应用程序。应用程序使用的数据库是MySQL,db中存储的密码是从word press应用程序生成的加密格式。我需要加密密码,以便可以将加密密码与db密码进行比较。
我的密码:Push @ 123加密的密码:$ P $ BGW0cKLlkN6VlZ7OqRUvIY1Uvo / Bh9 /
如何在C#中生成此加密密码
小智 7
我花了一段时间,但在这里您几乎可以 1:1 将 php 转换为 C#:
using System;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
namespace WordpressHash {
public class Program {
private static string itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static void Main(string[]args) {
string StrPassword = "Push@123";
string expected = "$P$BGW0cKLlkN6VlZ7OqRUvIY1Uvo/Bh9/";
string computed = MD5Encode(StrPassword, expected);
Console.WriteLine(StrPassword);
Console.WriteLine(computed);
Console.WriteLine("Are equal? " + expected.Equals(computed));
}
static string MD5Encode(string password, string hash) {
string output = "*0";
if (hash == null) {
return output;
}
if (hash.StartsWith(output))
output = "*1";
string id = hash.Substring(0, 3);
// We use "$P$", phpBB3 uses "$H$" for the same thing
if (id != "$P$" && id != "$H$")
return output;
// get who many times will generate the hash
int count_log2 = itoa64.IndexOf(hash[3]);
if (count_log2 < 7 || count_log2 > 30)
return output;
int count = 1 << count_log2;
string salt = hash.Substring(4, 8);
if (salt.Length != 8)
return output;
byte[]hashBytes = {};
using(MD5 md5Hash = MD5.Create()) {
hashBytes = md5Hash.ComputeHash(Encoding.ASCII.GetBytes(salt + password));
byte[]passBytes = Encoding.ASCII.GetBytes(password);
do {
hashBytes = md5Hash.ComputeHash(hashBytes.Concat(passBytes).ToArray());
} while (--count > 0);
}
output = hash.Substring(0, 12);
string newHash = Encode64(hashBytes, 16);
return output + newHash;
}
static string Encode64(byte[]input, int count) {
StringBuilder sb = new StringBuilder();
int i = 0;
do {
int value = (int)input[i++];
sb.Append(itoa64[value & 0x3f]); // to uppercase
if (i < count)
value = value | ((int)input[i] << 8);
sb.Append(itoa64[(value >> 6) & 0x3f]);
if (i++ >= count)
break;
if (i < count)
value = value | ((int)input[i] << 16);
sb.Append(itoa64[(value >> 12) & 0x3f]);
if (i++ >= count)
break;
sb.Append(itoa64[(value >> 18) & 0x3f]);
} while (i < count);
return sb.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据库中的每个散列都使用盐和nmd5 的迭代进行编码。可以在这里找到简要说明:https : //codex.wordpress.org/Function_Reference/wp_hash_password
我故意省略了盐的生成。但是如果您将来需要它,它应该$P$以至少 12 个字符开头。借助这个额外的方法,您还可以对新密码进行散列,而不仅仅是检查散列是否正确。
也许这对你有用
using System.Security.Cryptography;
class Program
{
static void Main(string[] args)
{
string StrPassword = "Push@123";
using (MD5 md5Hash = MD5.Create())
{
string hashPassword = GetMd5Hash(md5Hash, StrPassword);
Console.WriteLine(hashPassword);
}
}
static string GetMd5Hash(MD5 md5Hash, string input)
{
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
哈希函数将任意长度的二进制字符串映射为固定长度的小二进制字符串。加密哈希函数具有以下属性:在计算上无法找到哈希为相同值的两个不同输入;也就是说,如果对应的数据也匹配,则两组数据的哈希值应该匹配。数据的微小变化会导致哈希值发生巨大的、不可预测的变化。
MD5 算法的哈希大小为 128 位。
MD5 类的 ComputeHash 方法以 16 字节数组的形式返回哈希值。请注意,某些 MD5 实现会生成 32 个字符的十六进制格式的哈希值。要与此类实现进行互操作,请将 ComputeHash 方法的返回值格式化为十六进制值。