简体   繁体   English

从字符串计算 MD5 hash

[英]Calculate a MD5 hash from a string

I use the following C# code to calculate a MD5 hash from a string.我使用以下 C# 代码从字符串计算 MD5 hash。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运行良好并生成一个 32 个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码生成 16 个字符的十六进制字符串(或 12 个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code!一个 32 个字符的十六进制字符串很好,但我认为客户输入代码会很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

    public static string GetMD5(string encryptString)
    {
        var passByteCrypt = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(encryptString));

        return ByteArrayToString(passByteCrypt);
    }
    public static string ByteArrayToString(byte[] bytes)
    {
        var output = new StringBuilder(bytes.Length);

        foreach (var t in bytes)
        {
            output.Append(t.ToString("X2"));
        }

        return output.ToString().ToLower();
    }

this is simple md5 ByteCrypt这是简单的 md5 ByteCrypt

Here is a condensed version.这是一个精简版。

private string CreateMD5(string myText)
{
    var hash = System.Security.Cryptography.MD5.Create()
        .ComputeHash(System.Text.Encoding.ASCII.GetBytes(myText ?? ""));
    return string.Join("", Enumerable.Range(0, hash.Length).Select(i => hash[i].ToString("x2")));
}

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

I use the following C# code to calculate a MD5 hash from a string.我使用以下C#代码从字符串中计算MD5哈希值。 It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72它运作良好,并生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

Is there a way to use code like this to generate a 16-character hex string (or 12-character string)?有没有办法使用这样的代码来生成16个字符的十六进制字符串(或12个字符的字符串)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code! 32个字符的十六进制字符串很好,但是我认为让客户输入代码很无聊!

If you are using a version lower than .NET5 this is a neat way to write it如果您使用的版本低于 .NET5,这是一种简洁的编写方式

string.Concat(yourHashBytes.Select(x => x.ToString("X2")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM