如何将字符串散列为十六进制而不是文本? [英] How Do I hash a string as Hex instead of Text?

查看:79
本文介绍了如何将字符串散列为十六进制而不是文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



一直在拉我的头发试图找出如何计算一串十六进制字符的MD5。我的代码将输入视为文本字符串,而不是十六进制字符串。 ?我如何获得VB考虑字符串作为HEX



这里是十六进制字符串我想要计算MD5为:464645363730433739316C2C011002FFE670C791F149CD1ABFBF8CE63278076A0A29FCE6DD55B343DC0891C5A7F0538CA42638A9CA6ABCA9A2DC117CEE331D2830E8403BEECA2B6C4D3E1B8B09E9CD7AA9517F4CBBE2CDB8DF630127DA922D072E95AEB32EF89B2F23BE29B0B6278AE4A2A44B252D902FEC3B65828ECAA6C224DD99E18021EF8C42A948F828D67E38A6B52CF7A4E49DA7610ED0C93941F27850D97F56C241A4562B5CCA2DEA8D392924976E5A95038B2754D98A86D88B807C243C9617C03232BFCAF4B107DEDC0CA99EFCCF7A2A78E5FF79E40B4F70BD3D07D9FFFCE7210736A43621724BE4F853659F24C0DC53CCC5D606AB077D212B5F1288B20F2311077AD18EE2027981497AE59CB7B1C5E7E6156FC4E48CF1CA5624BB2113FDB409B7DB9C78175F479954DD8B7A6D18F3937B1C254BC107FABD8B71FC532E17D88982DBC55E6D5A3459D849F2B2C13D6629B8668BE4F02D00D1B36CD2EFE6268B02320B3986CCD4B74A0404DC7BB765DEC58364C305F89FC9D7D21412EF8473FAEC79FDD0DAD1DB1DED6FB163E58FFA656894D2FF0EE4F4E4640010688E9C657C0EE437ACBD243CBDC84B01D1AE5302D2C184D2D E326DEDCFEE8C53120107C40D560A6871137BEEA662CF4E88C699F78807620CB4A8FE4A6F952C571FEF4CD3FA37AD75D1404E66EB5EA47A73AB76CAF1B7AD2EC9DFA8E4EFD31D2573C932F6DE130B1225711BE16F4D2EBF38E7A6F5EA2B8112F0A0326FB089A387AF9F62A0BBD653D6827114970D883C250AC69A80207F127235B32BD744B11AF6B9A13DC143A06B16A54267F5A70430E792BD5929E2BF



MD5的字符串文本= A44D49B12BD10CDAF08A034638F8141B

MD5的字符串作为十六进制= E514481E8E3344F37A3F1E2BBFC84EEF



这是我到目前为止的代码:





 私有  Sub  Button1_Click( ByVal  sender < span class =code-keyword> As  System。 Object  ByVal  e  As  System.EventArgs)句柄 Button1.Click 
hashblob = <跨度类= 代码串> 464645363730433739316C2C011002FFE670C791F149CD1ABFBF8CE63278076A0A29FCE6DD55B343DC0891C5A7F0538CA42638A9CA6ABCA9A2DC117CEE331D2830E8403BEECA2B6C4D3E1B8B09E9CD7AA9517F4CBBE2CDB8DF630127DA922D072E95AEB32EF89B2F23BE29B0B6278AE4A2A44B252D902FEC3B65828ECAA6C224DD99E18021EF8C42A948F828D67E38A6B52CF7A4E49DA7610ED0C93941F27850D97F56C241A4562B5CCA2DEA8D392924976E5A95038B2754D98A86D88B807C243C9617C03232BFCAF4B107DEDC0CA99EFCCF7A2A78E5FF79E40B4F70BD3D07D9FFFCE7210736A43621724BE4F853659F24C0DC53CCC5D606AB077D212B5F1288B20F2311077AD18EE2027981497AE59CB7B1C5E7E6156FC4E48CF1CA5624BB2113FDB409B7DB9C78175F479954DD8B7A6D18F3937B1C254BC107FABD8B71FC532E17D88982DBC55E6D5A3459D849F2B2C13D6629B8668BE4F02D00D1B36CD2EFE6268B02320B3986CCD4B74A0404DC7BB765DEC58364C305F89FC9D7D21412EF8473FAEC79FDD0DAD1DB1DED6FB163E58FFA656894D2FF0EE4F4E4640010688E9C657C0EE437ACBD243CBDC84B01D1AE5302D2C184D2DE326DEDCFEE8C53120107C40D560A6871137BEEA662CF4E88C699F78807620CB4A8FE4A6F952C571FEF4 CD3FA37AD75D1404E66EB5EA47A73AB76CAF1B7AD2EC9DFA8E4EFD31D2573C932F6DE130B1225711BE16F4D2EBF38E7A6F5EA2B8112F0A0326FB089A387AF9F62A0BBD653D6827114970D883C250AC69A80207F127235B32BD744B11AF6B9A13DC143A06B16A54267F5A70430E792BD5929E2BF

<跨度类= 代码关键字 >使用 md5Hash <跨度类= 代码关键字>为 MD5 = MD5.Create ()

Dim hash As String = GetMd5Hash(md5Hash,hashblob2)
Debug.Print(hash)
End 使用

结束 Sub

共享 功能 GetMd5Hash( ByVal md5Hash 作为 MD5, ByVal inp ut 作为 字符串作为 字符串

' 将输入字符串转换为一个字节数组并计算哈希。
Dim data = md5Hash.ComputeHash(编码。默认值 .GetBytes(输入))

' 创建一个新的Stringbuilder来收集bytes
' 并创建一个字符串。
Dim sBuilder As StringBuilder()

' 遍历散列数据的每个字节
' '并将每个格式化为十六进制字符串。
Dim i As 整数
对于 i = 0 data.Length - 1
sBuilder.Append(data(i).ToString( X2))
下一步 i

' 返回十六进制字符串。
返回 sBuilder.ToString()

< span class =code-keyword>结束
功能 ' GetMd5Hash


任何人都可以告诉 如何将我的代码更改为 string as 十六进制 文本?

感谢任何帮助指导!

解决方案

< blockquote>没有整洁的方法来执行此操作:.NET框架不包含将十六进制字符串转换为字节数组!



但它不是过于复杂:

 私人 静态 < span class =code-keyword> byte  [] ConvertHex( string  hex)
{
if (hex.Length% 2 == 1 throw new ArgumentException( 十六进制数据不能有奇数个数字);

int bytes = hex.Length / 2 ;
byte [] result = new byte < /跨度> [字节];
int inputIndex = 0 ;
for int i = 0 ; i < bytes; i ++)
{
result [i] =( byte )((GetHexNibble(hex [inputIndex ++])<< 4 )+(GetHexNibble(hex [inputIndex ++])));
}
返回结果;
}

private static int GetHexNibble( char hex)
{
if (hex < = ' 9' return hex - ' 0' ;
return (十六进制 - ' A')+ 10 ;
}









我是只是一个没有编程技巧的技术人员,根本不知道C#...你能用VB发送代码吗?





 私有 共享 函数 ConvertHex(hex  As   String  As  字节()
如果 hex.Length Mod 2 = 1 然后
抛出 ArgumentException( 十六进制数据不能有奇数位
结束 如果

Dim bytes 正如 整数 = hex.Length \ 2
Dim 结果作为 字节()= 字节(字节 - 1 ){}
Dim inputIndex As Integer = 0
对于 i 作为 整数 = 0 字节 - 1
Dim hi As < span class =code-keyword> Integer = GetHexNibble(hex(inputIndex))
inputIndex + = 1
Dim lo As Integer = GetHexNibble(hex(inputIndex) ))
inputIndex + = 1
result(i)= CByte (hi * 4 + lo)
下一步
返回结果
结束 功能

私有 共享 功能 GetHexNibble(hex 作为 字符作为 整数
如果十六进制< = 9 C 然后
返回十六进制 - 0 C
结束 如果
返回(十六进制 - A C )+ 10
结束 功能


为了防止其他一些糟糕的汁液在VB中需要帮助,我在好朋友的帮助下找到了解决方案,并从OriginalGriff获得了积极的反馈!



我必须承认我收到的最初的ARROGANT回复是非常令人沮丧和不必要的......



VB代码:



公共函数HexDecode(ByVal s As String)As Byte()

返回HexDecode(s,0)

结束函数



公共函数HexDecode(ByVal s As String,ByVal paddingBytes As Integer)As Byte()

如果s什么都没有呢

抛出新的ArgumentNullException(s)

结束如果



如果是s.IndexOf( :c)> -1然后

s = s.Replace(:,)

结束如果



如果(s.Length Mod 2)<> 0然后

抛出新的FormatException(参数's'必须有偶数个十六进制字符)

结束如果



Dim result As Byte()= New Byte(s.Length \ 2 +(paddingBytes - 1)){}

For i As Integer = 0 to result.Length - paddingBytes - 1

结果(i)= Byte.Parse(s.Substring(i * 2,2),NumberStyles.AllowHexSpecifier)

下一页

返回结果

结束功能



感谢大家指出我正确的方向!



干杯


十六进制代码到字节数组

------------------- -------------------

  public   static   byte  [] DecodeHex( string  hextext )
{
String [] arr = hextext.Split(' - ');
byte [] array = new byte [arr.Length];
for int i = 0 ; i < arr.Length; i ++)
array [i] = Convert.ToByte(arr [i], 16 );
return 数组;
}



它指导你。

用于加密的MD5示例代码

----- --------------------------------------

 public static string Encrypt(string toEncrypt,bool useHashing,string imkey)
{
byte [] keyArray;
byte [] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

string key = imkey;
if(useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();
byte [] resultArray = cTransform.TransformFinalBlock(toEncryptArray,0,toEncryptArray.Length);
tdes.Clear();
返回Convert.ToBase64String(resultArray,0,resultArray.Length);
}


Hello,

Been pulling my hair out trying figure out how to calculate the MD5 for a string of HEX characters. My code considers the input as text string, not hex string. How do I get VB to consider the string as HEX?

Here is the HEX string I want to calculate MD5 for: "464645363730433739316C2C011002FFE670C791F149CD1ABFBF8CE63278076A0A29FCE6DD55B343DC0891C5A7F0538CA42638A9CA6ABCA9A2DC117CEE331D2830E8403BEECA2B6C4D3E1B8B09E9CD7AA9517F4CBBE2CDB8DF630127DA922D072E95AEB32EF89B2F23BE29B0B6278AE4A2A44B252D902FEC3B65828ECAA6C224DD99E18021EF8C42A948F828D67E38A6B52CF7A4E49DA7610ED0C93941F27850D97F56C241A4562B5CCA2DEA8D392924976E5A95038B2754D98A86D88B807C243C9617C03232BFCAF4B107DEDC0CA99EFCCF7A2A78E5FF79E40B4F70BD3D07D9FFFCE7210736A43621724BE4F853659F24C0DC53CCC5D606AB077D212B5F1288B20F2311077AD18EE2027981497AE59CB7B1C5E7E6156FC4E48CF1CA5624BB2113FDB409B7DB9C78175F479954DD8B7A6D18F3937B1C254BC107FABD8B71FC532E17D88982DBC55E6D5A3459D849F2B2C13D6629B8668BE4F02D00D1B36CD2EFE6268B02320B3986CCD4B74A0404DC7BB765DEC58364C305F89FC9D7D21412EF8473FAEC79FDD0DAD1DB1DED6FB163E58FFA656894D2FF0EE4F4E4640010688E9C657C0EE437ACBD243CBDC84B01D1AE5302D2C184D2DE326DEDCFEE8C53120107C40D560A6871137BEEA662CF4E88C699F78807620CB4A8FE4A6F952C571FEF4CD3FA37AD75D1404E66EB5EA47A73AB76CAF1B7AD2EC9DFA8E4EFD31D2573C932F6DE130B1225711BE16F4D2EBF38E7A6F5EA2B8112F0A0326FB089A387AF9F62A0BBD653D6827114970D883C250AC69A80207F127235B32BD744B11AF6B9A13DC143A06B16A54267F5A70430E792BD5929E2BF"

MD5 for string as text = "A44D49B12BD10CDAF08A034638F8141B"
MD5 for string as hex = "E514481E8E3344F37A3F1E2BBFC84EEF"

Here is the code I have so far:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        hashblob = "464645363730433739316C2C011002FFE670C791F149CD1ABFBF8CE63278076A0A29FCE6DD55B343DC0891C5A7F0538CA42638A9CA6ABCA9A2DC117CEE331D2830E8403BEECA2B6C4D3E1B8B09E9CD7AA9517F4CBBE2CDB8DF630127DA922D072E95AEB32EF89B2F23BE29B0B6278AE4A2A44B252D902FEC3B65828ECAA6C224DD99E18021EF8C42A948F828D67E38A6B52CF7A4E49DA7610ED0C93941F27850D97F56C241A4562B5CCA2DEA8D392924976E5A95038B2754D98A86D88B807C243C9617C03232BFCAF4B107DEDC0CA99EFCCF7A2A78E5FF79E40B4F70BD3D07D9FFFCE7210736A43621724BE4F853659F24C0DC53CCC5D606AB077D212B5F1288B20F2311077AD18EE2027981497AE59CB7B1C5E7E6156FC4E48CF1CA5624BB2113FDB409B7DB9C78175F479954DD8B7A6D18F3937B1C254BC107FABD8B71FC532E17D88982DBC55E6D5A3459D849F2B2C13D6629B8668BE4F02D00D1B36CD2EFE6268B02320B3986CCD4B74A0404DC7BB765DEC58364C305F89FC9D7D21412EF8473FAEC79FDD0DAD1DB1DED6FB163E58FFA656894D2FF0EE4F4E4640010688E9C657C0EE437ACBD243CBDC84B01D1AE5302D2C184D2DE326DEDCFEE8C53120107C40D560A6871137BEEA662CF4E88C699F78807620CB4A8FE4A6F952C571FEF4CD3FA37AD75D1404E66EB5EA47A73AB76CAF1B7AD2EC9DFA8E4EFD31D2573C932F6DE130B1225711BE16F4D2EBF38E7A6F5EA2B8112F0A0326FB089A387AF9F62A0BBD653D6827114970D883C250AC69A80207F127235B32BD744B11AF6B9A13DC143A06B16A54267F5A70430E792BD5929E2BF"

        Using md5Hash As MD5 = MD5.Create()

            Dim hash As String = GetMd5Hash(md5Hash, hashblob2)
            Debug.Print(hash)
        End Using

    End Sub

    Shared Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String

        'Convert the input string to a byte array and compute the hash.
        Dim data = md5Hash.ComputeHash(Encoding.Default.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

       ' Loop through each byte of the hashed data
        '' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("X2"))
        Next i

       ' Return the hexadecimal string.
        Return sBuilder.ToString()

    End Function 'GetMd5Hash


Can anyone please tell me how to change my code to treat string as hex instead of text?

Appreciate any help or guidance!

解决方案

There is no "tidy" way to do this: converting a hex string to a byte array isn't covered by the .NET framwork!

But it's not too complex:

private static byte[] ConvertHex(string hex)
    {
    if (hex.Length % 2 == 1) throw new ArgumentException("The hexadecimal data cannot have an odd number of digits");

    int bytes = hex.Length / 2;
    byte[] result = new byte[bytes];
    int inputIndex = 0;
    for (int i = 0; i < bytes; i++)
        {
        result[i] = (byte)((GetHexNibble(hex[inputIndex++]) << 4) + (GetHexNibble(hex[inputIndex++])));
        }
    return result;
    }

private static int GetHexNibble(char hex)
    {
    if (hex <= '9') return hex - '0';
    return (hex - 'A') + 10;
    }





"I am just a tech with little programming skills, do not know C# at all... Can you send code in VB?


Private Shared Function ConvertHex(hex As String) As Byte()
    If hex.Length Mod 2 = 1 Then
        Throw New ArgumentException("The hexadecimal data cannot have an odd number of digits")
    End If

    Dim bytes As Integer = hex.Length \ 2
    Dim result As Byte() = New Byte(bytes - 1) {}
    Dim inputIndex As Integer = 0
    For i As Integer = 0 To bytes - 1
        Dim hi As Integer = GetHexNibble(hex(inputIndex))
        inputIndex += 1
        Dim lo As Integer = GetHexNibble(hex(inputIndex))
        inputIndex += 1
        result(i) = CByte(hi * 4 + lo)
    Next
    Return result
End Function

Private Shared Function GetHexNibble(hex As Char) As Integer
    If hex <= "9"C Then
        Return hex - "0"C
    End If
    Return (hex - "A"C) + 10
End Function


Just in case some other poor sap needs help doing this in VB, I figured out the solution with the help of a good friend and positive feedback from OriginalGriff!

I must admit the initial ARROGANT responses I received were extremely discouraging and unnecessary...

VB CODE:

Public Function HexDecode(ByVal s As String) As Byte()
Return HexDecode(s, 0)
End Function

Public Function HexDecode(ByVal s As String, ByVal paddingBytes As Integer) As Byte()
If s Is Nothing Then
Throw New ArgumentNullException("s")
End If

If s.IndexOf(":"c) > -1 Then
s = s.Replace(":", "")
End If

If (s.Length Mod 2) <> 0 Then
Throw New FormatException("parameter 's' must have an even number of hex characters")
End If

Dim result As Byte() = New Byte(s.Length \ 2 + (paddingBytes - 1)) {}
For i As Integer = 0 To result.Length - paddingBytes - 1
result(i) = Byte.Parse(s.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier)
Next
Return result
End Function

Thanks to everyone who pointed me in the right direction!

Cheers


Hex code to byte array
--------------------------------------

public static byte[] DecodeHex(string hextext)
{
   String[] arr = hextext.Split('-');
   byte[] array = new byte[arr.Length];
   for (int i = 0; i < arr.Length; i++)
         array[i] = Convert.ToByte(arr[i], 16);
   return array;
}


It guide you.
MD5 sample code for encrypt
-------------------------------------------

public static string Encrypt(string toEncrypt, bool useHashing, string imkey)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

    string key = imkey;
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        hashmd5.Clear();
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateEncryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    tdes.Clear();
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}


这篇关于如何将字符串散列为十六进制而不是文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆