简体   繁体   中英

Trouble XORing two byte arrays

So I am currently working on this challenge http://cryptopals.com/sets/1/challenges/3 and I am getting incorrect results from the xor process. Somewhere in the process I appear to have messed something up but I can't seem to figure out where.

In short the task is to XOR a hex string with a single character (the character is unknown so I just use an array of all characters) and print the result as a string, which should be human readable. At present I am only concerned with executing the XOR properly as currently none return a human readable string.

Code:

        cipher = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
        string charArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!£$%^&*()_+";
        int counter = 0;
        char xorKey = 'a';
        string final = "";

        //Convert the initial string to a byte array
        byte[] cipherBytes = new byte[cipher.Length / 2];
        for(int i = 0; i < cipher.Length; i += 2)
        {
            cipherBytes[i / 2] = Convert.ToByte(cipher.Substring(i, 2), 16);
        }

        byte[] result = new byte[cipherBytes.Length];
        for(int i = 0; i < charArray.Length; i++)
        {
            for(int j = 0; j < cipherBytes.Length; j++)
            {
                //XOR each element in the byte array against the character array 
                result[j] = (byte)(cipherBytes[j] ^ charArray[i]);
            }
            string output = BitConverter.ToString(result).Replace("-", "");
            //Print the whole thing out.
            Console.WriteLine("Output = {0}, Key = {1}", output, charArray[i]);
            int subCounter = 0;
            foreach(char letter in output)
            {
                if(letter == 'E' || letter == 'e')
                {
                    subCounter++;
                }
            }
            if(subCounter > counter)
            {
                counter = subCounter;
                xorKey = charArray[i];
                final = output;
            }
        }

        Console.ReadKey();
        return "";

The output I am getting looks like this:

Output = 7A56565250575E19747A1E4A195550525C19581949564C575D19565F195B585A5657, Key = a
Output = 7955555153545D1A77791D491A5653515F1A5B1A4A554F545E1A555C1A585B595554, Key = b
Output = 7854545052555C1B76781C481B5752505E1B5A1B4B544E555F1B545D1B595A585455, Key = c
Output = 7F53535755525B1C717F1B4F1C505557591C5D1C4C534952581C535A1C5E5D5F5352, Key = d
Output = 7E52525654535A1D707E1A4E1D515456581D5C1D4D524853591D525B1D5F5C5E5253, Key = e
Output = 7D5151555750591E737D194D1E5257555B1E5F1E4E514B505A1E51581E5C5F5D5150, Key = f
Output = 7C5050545651581F727C184C1F5356545A1F5E1F4F504A515B1F50591F5D5E5C5051, Key = g
Output = 735F5F5B595E57107D731743105C595B55105110405F455E54105F56105251535F5E, Key = h
Output = 725E5E5A585F56117C721642115D585A54115011415E445F55115E57115350525E5F, Key = i
Output = 715D5D595B5C55127F711541125E5B5957125312425D475C56125D54125053515D5C, Key = j
Output = 705C5C585A5D54137E701440135F5A5856135213435C465D57135C55135152505C5D, Key = k
Output = 775B5B5F5D5A53147977134714585D5F51145514445B415A50145B52145655575B5A, Key = l
Output = 765A5A5E5C5B52157876124615595C5E50155415455A405B51155A53155754565A5B, Key = m
Output = 7559595D5F5851167B751145165A5F5D531657164659435852165950165457555958, Key = n
Output = 7458585C5E5950177A741044175B5E5C521756174758425953175851175556545859, Key = o
Output = 6B47474341464F08656B0F5B084441434D08490858475D464C08474E084A494B4746, Key = p
Output = 6A46464240474E09646A0E5A094540424C09480959465C474D09464F094B484A4647, Key = q
Output = 6945454143444D0A67690D590A4643414F0A4B0A5A455F444E0A454C0A484B494544, Key = r
Output = 6844444042454C0B66680C580B4742404E0B4A0B5B445E454F0B444D0B494A484445, Key = s
Output = 6F43434745424B0C616F0B5F0C404547490C4D0C5C435942480C434A0C4E4D4F4342, Key = t
Output = 6E42424644434A0D606E0A5E0D414446480D4C0D5D425843490D424B0D4F4C4E4243, Key = u
Output = 6D4141454740490E636D095D0E4247454B0E4F0E5E415B404A0E41480E4C4F4D4140, Key = v
Output = 6C4040444641480F626C085C0F4346444A0F4E0F5F405A414B0F40490F4D4E4C4041, Key = w
Output = 634F4F4B494E47006D630753004C494B45004100504F554E44004F46004241434F4E, Key = x
Output = 624E4E4A484F46016C620652014D484A44014001514E544F45014E47014340424E4F, Key = y
Output = 614D4D494B4C45026F610551024E4B4947024302524D574C46024D44024043414D4C, Key = z
Output = 5A76767270777E39545A3E6A397570727C39783969766C777D39767F397B787A7677, Key = A
Output = 5975757173747D3A57593D693A7673717F3A7B3A6A756F747E3A757C3A787B797574, Key = B
Output = 5874747072757C3B56583C683B7772707E3B7A3B6B746E757F3B747D3B797A787475, Key = C
Output = 5F73737775727B3C515F3B6F3C707577793C7D3C6C736972783C737A3C7E7D7F7372, Key = D
Output = 5E72727674737A3D505E3A6E3D717476783D7C3D6D726873793D727B3D7F7C7E7273, Key = E
Output = 5D7171757770793E535D396D3E7277757B3E7F3E6E716B707A3E71783E7C7F7D7170, Key = F
Output = 5C7070747671783F525C386C3F7376747A3F7E3F6F706A717B3F70793F7D7E7C7071, Key = G
Output = 537F7F7B797E77305D533763307C797B75307130607F657E74307F76307271737F7E, Key = H
Output = 527E7E7A787F76315C523662317D787A74317031617E647F75317E77317370727E7F, Key = I
Output = 517D7D797B7C75325F513561327E7B7977327332627D677C76327D74327073717D7C, Key = J
Output = 507C7C787A7D74335E503460337F7A7876337233637C667D77337C75337172707C7D, Key = K
Output = 577B7B7F7D7A73345957336734787D7F71347534647B617A70347B72347675777B7A, Key = L
Output = 567A7A7E7C7B72355856326635797C7E70357435657A607B71357A73357774767A7B, Key = M
Output = 5579797D7F7871365B553165367A7F7D733677366679637872367970367477757978, Key = N
Output = 5478787C7E7970375A543064377B7E7C723776376778627973377871377576747879, Key = O
Output = 4B67676361666F28454B2F7B286461636D28692878677D666C28676E286A696B6766, Key = P
Output = 4A66666260676E29444A2E7A296560626C29682979667C676D29666F296B686A6667, Key = Q
Output = 4965656163646D2A47492D792A6663616F2A6B2A7A657F646E2A656C2A686B696564, Key = R
Output = 4864646062656C2B46482C782B6762606E2B6A2B7B647E656F2B646D2B696A686465, Key = S
Output = 4F63636765626B2C414F2B7F2C606567692C6D2C7C637962682C636A2C6E6D6F6362, Key = T
Output = 4E62626664636A2D404E2A7E2D616466682D6C2D7D627863692D626B2D6F6C6E6263, Key = U
Output = 4D6161656760692E434D297D2E6267656B2E6F2E7E617B606A2E61682E6C6F6D6160, Key = V
Output = 4C6060646661682F424C287C2F6366646A2F6E2F7F607A616B2F60692F6D6E6C6061, Key = W
Output = 436F6F6B696E67204D432773206C696B65206120706F756E64206F66206261636F6E, Key = X
Output = 426E6E6A686F66214C422672216D686A64216021716E746F65216E67216360626E6F, Key = Y
Output = 416D6D696B6C65224F412571226E6B6967226322726D776C66226D64226063616D6C, Key = Z
Output = 2A06060200070E49242A4E1A490500020C49084919061C070D49060F490B080A0607, Key = 1
Output = 2905050103040D4A27294D194A0603010F4A0B4A1A051F040E4A050C4A080B090504, Key = 2
Output = 2804040002050C4B26284C184B0702000E4B0A4B1B041E050F4B040D4B090A080405, Key = 3
Output = 2F03030705020B4C212F4B1F4C000507094C0D4C1C031902084C030A4C0E0D0F0302, Key = 4
Output = 2E02020604030A4D202E4A1E4D010406084D0C4D1D021803094D020B4D0F0C0E0203, Key = 5
Output = 2D0101050700094E232D491D4E0207050B4E0F4E1E011B000A4E01084E0C0F0D0100, Key = 6
Output = 2C0000040601084F222C481C4F0306040A4F0E4F1F001A010B4F00094F0D0E0C0001, Key = 7
Output = 230F0F0B090E07402D234713400C090B05400140100F150E04400F06400201030F0E, Key = 8
Output = 220E0E0A080F06412C224612410D080A04410041110E140F05410E07410300020E0F, Key = 9
Output = 3A16161210171E59343A5E0A591510121C59185909160C171D59161F591B181A1617, Key = !
Output = B894949092959CDBB6B8DC88DB9792909EDB9ADB8B948E959FDB949DDB999A989495, Key = £
Output = 3F13131715121B5C313F5B0F5C101517195C1D5C0C130912185C131A5C1E1D1F1312, Key = $
Output = 3E12121614131A5D303E5A0E5D111416185D1C5D0D120813195D121B5D1F1C1E1213, Key = %
Output = 4569696D6F6861264B452175266A6F6D632667267669736862266960266467656968, Key = ^
Output = 3D1111151710195E333D590D5E1217151B5E1F5E0E110B101A5E11185E1C1F1D1110, Key = &
Output = 311D1D191B1C15523F315501521E1B1917521352021D071C16521D14521013111D1C, Key = *
Output = 331F1F1B191E17503D335703501C191B15501150001F051E14501F16501211131F1E, Key = (
Output = 321E1E1A181F16513C325602511D181A14511051011E041F15511E17511310121E1F, Key = )
Output = 4468686C6E6960274A442074276B6E6C622766277768726963276861276566646869, Key = _
Output = 301C1C181A1D14533E305400531F1A1816531253031C061D17531C15531112101C1D, Key = +

Obviously none of which is a human readable string. I've had a look around and found a few solutions but no descriptions as to how those solutions achieve the intended results.

To summarise, what is wrong with this XOR implementation to make it not work in the intended way?

The problem is this line:

string output = BitConverter.ToString(result).Replace("-", "");

You make hex representation of the characters instead of the characters themself. Try this instead:

string output = string.Concat(result.Select(c => (char)c));

The following code works:

void Main()
{
    var cipher = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
    var charArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!£$%^&*()_+";
    var decoded = FromHex(cipher);
    foreach(var c in charArray)
    {
        Console.WriteLine("{0}: {1}", c.ToString(), new string(decoded.Select(x => x^c).Select(x => (char)x).ToArray()));
    }
}

public static byte[] FromHex(string hex)
{
    hex = hex.Replace("-", "");
    byte[] raw = new byte[hex.Length / 2];
    for (int i = 0; i < raw.Length; i++)
    {
        raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
    }
    return raw;
}

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