简体   繁体   中英

How to XOR a MD5 hash and return a 32 character string?

How do I further encrypt a MD5 hash by XOR'ing it with a string of variable size (not bigger than 32 characters) ?

I would like the result of the XOR to be a 32 character string as well.

What i have tried so far is:

  • convert the md5 string to binary
  • convert second string to binary
  • pad second binary with 0's (to the left) until both binaries are of equal length
  • iterate the binary representations and XOR them
  • convert the XOR'ed result to a string

The approach may be wrong, im not sure how to do it. My problem is, when converting the result of the XOR, it is not a 32 character long string, as I would like it to be.

Sample code (equal length strings in this case):

class Program
{
    static void Main(string[] args)
    {

        var md51 = ToBinary(ConvertToByteArray(CalculateMD5Hash("Maaa"), Encoding.ASCII));
        var md52 = ToBinary(ConvertToByteArray(CalculateMD5Hash("Moo"), Encoding.ASCII));

        List<int> xoredResult = new List<int>();

        for (int i = 0; i < md51.Length; i++)
        {
            var string1 = md51[i];
            var string2 = md52[i];
            var xor = string1 ^ string2;
            xoredResult.Add(xor);
        }

        var resultingString = string.Join("", xoredResult);
        Console.WriteLine(resultingString.Length);

        var data = GetBytesFromBinaryString(resultingString);
        var text = Encoding.ASCII.GetString(data);

    }

    public static byte[] ConvertToByteArray(string str, Encoding encoding)
    {
        return encoding.GetBytes(str);
    }

    public static String ToBinary(Byte[] data)
    {
        return string.Join("", data.Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0')));
    }

    public static Byte[] GetBytesFromBinaryString(String binary)
    {
        var list = new List<Byte>();

        for (int i = 0; i < binary.Length; i += 8)
        {
            String t = binary.Substring(i, 8);

            list.Add(Convert.ToByte(t, 2));
        }

        return list.ToArray();
    }

    public static string CalculateMD5Hash(string input)
    {

        // step 1, calculate MD5 hash from input

        MD5 md5 = System.Security.Cryptography.MD5.Create();

        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);

        byte[] hash = md5.ComputeHash(inputBytes);

        // step 2, convert byte array to hex string

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < hash.Length; i++)

        {

            sb.Append(hash[i].ToString("X2"));

        }

        return sb.ToString();

    }
}

xoring a string with what is essentially random bytes is not guaranteed to give you a valid string as a output. Your var text = Encoding.ASCII.GetString(data); is likely failing because you are passing it a non valid string in byte form. You must use something like var text = Convert.ToBase64String(data) to be able to represent the random data without loss of information in the process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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