Trang chủ » Lập trình » Hàm mã hóa MD5 password bằng C# và VB.NET

Hàm mã hóa MD5 password bằng C# và VB.NET

2181 Lượt xem

C#:

Code:

public static byte[] encryptData(string data)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashedBytes;
        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
        hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
        return hashedBytes;
    }
public static string md5(string data)
        {
            return BitConverter.ToString(encryptData(data)).Replace("-","").ToLower();
        }

VD:

Code:
md5("123"); ///Kết quả: 202cb962ac59075b964b07152d234b70

VB.NET:

Code:

Public Shared Function encryptData(data As String) As Byte()
    Dim md5Hasher As New System.Security.Cryptography.MD5CryptoServiceProvider()
    Dim hashedBytes As Byte()
    Dim encoder As New System.Text.UTF8Encoding()
    hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data))
    Return hashedBytes
End Function
 
Public Shared Function md5(data As String) As String
    Return  BitConverter.ToString(encryptData(data)).Replace("-","").ToLower();
End Function