Simulating MySql () password encryption using .NET or MS SQL

I am updating an old ASP / MySql webapp for ASP.NET/MS SQL.

We would like to save logins from the old website working in the new application.

Unfortunately, the passwords were saved in the MySql database using the MySql password () function.

Is it possible to simulate the MySql password () function in .NET or MS SQL?

Any help / links are appreciated.

+5
source share
7 answers

MySQL, SHA1-. MySQL , make_scrambled_password() libmysql/password.c. :

/*
    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), , post-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. , PASSWORD() MySQL. , .

: #

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();
}
+12

MD5 SHA1 .Net, , MySQL, , . , - "" , .

, , MySQL , , .

http://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html#function_password

1: , - SHA1 "" ( ).

+1

" , , "

erm... - , MySQL OPEN SOURCE... "" -, - , (, ++) ... SHA1 - , 20% , , AES SHA2 (, MySQL-PASSWORD-func)

+1

# .
      [ ]       public void GenerateMySQLHashX2()       {           string password = "20iva05";

        byte[] keyArray = Encoding.UTF8.GetBytes(password);
        SHA1Managed enc = new SHA1Managed();
        byte[] encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray));
        StringBuilder myBuilder = new StringBuilder(encodedKey.Length);

        foreach (byte b in encodedKey)
        {
            log.Debug(b.ToString() + "    ->   " +  b.ToString("X2"));
            myBuilder.Append(b.ToString("X2"));
        }

        Assert.AreEqual("*8C2029A96507478FD1720F6B713ADD57C66EED49", "*" + myBuilder.ToString());
    } 
+1

VB net 0 , , [] 'E', '0E' . , , "*", 41 , MySQL

Public Function GenerateMySQLHash(ByVal strKey As String) As String

    Dim keyArray As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(strKey)
    Dim enc = New Security.Cryptography.SHA1Managed()
    Dim encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray))
    Dim myBuilder As New System.Text.StringBuilder(encodedKey.ToString.Length)

    For Each b As Byte In encodedKey
        myBuilder.Append(Strings.Right("0" & b.ToString("X"), 2))
    Next

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

mysql (, ) , , , - 1: / mysql password() s 2: mysql .

Else - , , (/ ) . thay way, , . , (, google 446a12100c856ce9, password() )

0

MySql password(). , . MySql , , ; , , ..

:

, MySql. . ; MySql password(). ; reset, , ; MS Sql DB.

I know this sounds like pain, but it is the best you can do without trying to crack your way in decrypting the MySql encryption algorithm.

Good luck

-3
source