c#에서의 md5(wordpress) 비밀번호 암호화
asp.net 웹 어플리케이션에서 사용자 폼을 인증하고 싶습니다. 어플리케이션에 사용되는 데이터베이스는 MySQL이며, db에 저장된 비밀번호는 워드프레스 어플리케이션에서 생성된 암호화 형식입니다.암호화된 비밀번호와 db 비밀번호를 비교하기 위해 암호화된 비밀번호가 필요합니다.
내 비밀번호 : Push@123 암호화 비밀번호 : $P$BGW0cKLKN6VlZ7OqRUvIY1Uvo/Bh9/
c#에서 이 암호화된 비밀번호를 생성하는 방법
시간이 좀 걸렸지만, 여기서는 거의 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();
}
}
}
내의 및 salt salt를 됩니다.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();
}
}
해시 함수는 임의의 길이의 이진 문자열을 고정 길이의 작은 이진 문자열에 매핑합니다.암호화 해시 함수는 동일한 값으로 해시되는 두 개의 개별 입력을 찾는 것이 계산상 불가능하다는 특성을 가지고 있습니다. 즉, 대응하는 데이터도 일치하는 경우 두 세트의 해시가 일치해야 합니다.데이터에 작은 변경을 가하면 해시에 예측할 수 없는 큰 변경이 발생합니다.
MD5 알고리즘의 해시 사이즈는 128비트입니다.
MD5 클래스의 ComputeHash 메서드는 해시를 16바이트 배열로 반환합니다.MD5 구현 중에는 32자의 16진수 형식의 해시가 생성되는 것도 있습니다.이러한 구현과 상호 운용하려면 ComputeHash 메서드의 반환 값을 16진수 값으로 포맷하십시오.
class-phpass.php에서 crypt_private php 메서드를 다시 썼습니다(/wp-includes/class-phpass 참조).php)를 사용하여 c#에서 사용합니다.
비밀번호는 사용자가 입력한 문자열이며, wp_users 행의 wp 데이터베이스에서 설정은 user_pass 값입니다.
crypt_private는 패스워드의 해시를 반환합니다.따라서 crypt_private returned 값이 setting value와 같다면 비밀번호는 올바른 것입니다.
이 기능은 워드프레스가 있는 서버에서 php5 이상을 사용하는 경우에 사용할 수 있습니다.
private const string itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public bool SignIn(string password)
{
string foundUserHash = "hash from database (saved password of a user)";
string hash = Crypt(password, foundUserHash);
return foundUserHash == hash;
}
private string Crypt(string password, string setting)
{
string output = "*0";
if (setting.Substring(0, 2) == output)
output = "*1";
string id = setting.Substring(0, 3);
if (id != "$P$" && id != "$H$")
return output;
int count_log2 = itoa64.IndexOf(setting[3]);
if (count_log2 < 7 || count_log2 > 30)
return output;
var count = 1 << count_log2;
string salt = setting.Substring(4, 8);
if (salt.Length != 8)
return output;
var hash = GetHash(
GetByteArraysAppended(
Encoding.UTF7.GetBytes(salt),
Encoding.UTF7.GetBytes(password)
));
do
{
hash = GetHash(
GetByteArraysAppended(
hash,
Encoding.UTF7.GetBytes(password)
));
}
while (--count!=0);
output = setting.Substring(0, 12);
output += encode64(hash, 16);
return output;
}
private string encode64(byte [] input, int count)
{
string output = "";
int i = 0;
do
{
Int32 value = input[i++];
output += itoa64[value & 0x3f];
if (i < count)
value |= input[i] << 8;
output += itoa64[(value >> 6) & 0x3f];
if (i++ >= count)
break;
if (i < count)
value |= input[i] << 16;
output += itoa64[(value >> 12) & 0x3f];
if (i++ >= count)
break;
output += itoa64[(value >> 18) & 0x3f];
} while (i < count);
return output;
}
private byte[] GetByteArraysAppended(byte[] partOne, byte[] partTwo)
{
var parts = partOne.ToList();
parts.AddRange(partTwo);
var result = parts.ToArray();
return result;
}
private byte[] GetHash(byte [] bytesToHash)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
var hash = md5.ComputeHash(bytesToHash);
return hash;
}
언급URL : https://stackoverflow.com/questions/34127718/md5wordpress-password-encryption-in-c-sharp
'programing' 카테고리의 다른 글
| grunt 서버를 사용하여 모든 요청을 루트 URL로 리다이렉트하려면 어떻게 해야 합니까? (0) | 2023.02.23 |
|---|---|
| Wordpress - 쿼리 문자열을 슬래시 기반 URL로 전달 (0) | 2023.02.23 |
| 비동기 대기가 React setState와 연동되는 이유는 무엇입니까? (0) | 2023.02.23 |
| 각도 소독/ng-bind-html이 작동하지 않나요? (0) | 2023.02.23 |
| Typescript - 확장 오류 클래스 (0) | 2023.02.23 |
