加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

行使.NET或MS SQL模仿MySql的暗码()加密

发布时间:2021-01-13 18:51:55 所属栏目:编程 来源:网络整理
导读:我正在将旧的ASP / MySql webapp更新为ASP.NET / MS SQL. 我们但愿保持旧网站的登录在新应用中运行. 不幸的是,暗码行使MySql的password()函数存储在MySql DB中. 是否可以在.NET或.NET中模仿MySql的password()函数 MS SQL? 任何辅佐/链接暗示赞赏. 办理要领

我正在将旧的ASP / MySql webapp更新为ASP.NET / MS SQL.

我们但愿保持旧网站的登录在新应用中运行.

不幸的是,暗码行使MySql的password()函数存储在MySql DB中.

是否可以在.NET或.NET中模仿MySql的password()函数
MS SQL?

任何辅佐/链接暗示赞赏.

办理要领

按照MySQL文档,该算法是双SHA1哈希.在搜查MySQL源代码时,您会在libmysql / password.c中找到一个名为make_scrambled_pa??ssword()的函数.该成果界说如下:
/*
    MySQL 4.1.1 password hashing: SHA conversion (see RFC 2289,3174) twice
    applied to the password string,and then produced octet sequence is
    converted to hex string.
    The result of this function is used as return value from PASSWORD() and
    is stored in the database.
  SYNOPSIS
    make_scrambled_password()
    buf       OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string
    password  IN  NULL-terminated password string
*/

void
make_scrambled_password(char *to,const char *password)
{
  SHA1_CONTEXT sha1_context;
  uint8 hash_stage2[SHA1_HASH_SIZE];

  mysql_sha1_reset(&sha1_context);
  /* stage 1: hash password */
  mysql_sha1_input(&sha1_context,(uint8 *) password,(uint) strlen(password));
  mysql_sha1_result(&sha1_context,(uint8 *) to);
  /* stage 2: hash stage1 output */
  mysql_sha1_reset(&sha1_context);
  mysql_sha1_input(&sha1_context,(uint8 *) to,SHA1_HASH_SIZE);
  /* separate buffer is used to pass 'to' in octet2hex */
  mysql_sha1_result(&sha1_context,hash_stage2);
  /* convert hash_stage2 to hex string */
  *to++= PVERSION41_CHAR;
  octet2hex(to,(const char*) hash_stage2,SHA1_HASH_SIZE);
}

有了这个要领,你可以建设一个根基上做同样工作的.NET对应物.这就是我想出来的.当我运行SELECT PASSWORD(‘test’);对我的当地MySQL副本,返回的值是:

* 94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29

按照源代码(再次在password.c中),开头的星号暗示这是加密暗码的MySQL 4.1后要领.譬喻,当我在VB.Net中模仿成果时,这就是我想出的:

Public Function GenerateMySQLHash(ByVal strKey As String) As String
    Dim keyArray As Byte() = Encoding.UTF8.GetBytes(strKey)
    Dim enc = New SHA1Managed()
    Dim encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray))
    Dim myBuilder As New StringBuilder(encodedKey.Length)

    For Each b As Byte In encodedKey
        myBuilder.Append(b.ToString("X2"))
    Next

    Return "*" & myBuilder.ToString()
End Function

请记着,SHA1Managed()位于System.Security.Cryptography定名空间中.此要领返回与MySQL中的PASSWORD()挪用沟通的输出.我但愿这对你有所辅佐.

编辑:这是C#中的沟通代码

public string GenerateMySQLHash(string key)
{
    byte[] keyArray = Encoding.UTF8.GetBytes(key);
    SHA1Managed enc = new SHA1Managed();
    byte[] encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray));
    StringBuilder myBuilder = new StringBuilder(encodedKey.Length);

    foreach (byte b in encodedKey)
        myBuilder.Append(b.ToString("X2"));

    return "*" + myBuilder.ToString();
}

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读