Example #1
0
        public static string RepeatingKeyXor(string key, string source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var xorBytes = RepeatingKeyXor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(source));

            return(Challenge2.ToHex(xorBytes));
        }
Example #2
0
        public static byte[] RepeatingKeyXor(byte[] key, byte[] source)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var keyLength = key.Length;
            var result    = new byte[source.Length];

            using (var sourceMs = new MemoryStream(source))
            {
                using (var destinationMs = new MemoryStream(result))
                {
                    var buffer = new byte[keyLength];
                    int read;

                    while ((read = sourceMs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        byte[] keyBytesToUse;
                        byte[] bufferToUse;
                        if (read != keyLength)
                        {
                            keyBytesToUse = new byte[read];
                            Array.Copy(key, keyBytesToUse, read);
                            bufferToUse = new byte[read];
                            Array.Copy(buffer, bufferToUse, read);
                        }
                        else
                        {
                            keyBytesToUse = key;
                            bufferToUse   = buffer;
                        }

                        var currentChunk = Challenge2.Xor(keyBytesToUse, bufferToUse);
                        destinationMs.Write(currentChunk, 0, read);
                    }
                }
            }
            return(result);
        }
        public static int HammingDistance(byte[] valueA, byte[] valueB)
        {
            if (valueA == null)
            {
                throw new ArgumentNullException("valueA");
            }
            if (valueB == null)
            {
                throw new ArgumentNullException("valueB");
            }
            if (valueA.Length != valueB.Length)
            {
                throw new ArgumentOutOfRangeException("valueA", "Both values must be the same length.");
            }
            var xorValue = Challenge2.Xor(valueA, valueB);

            return(xorValue.Sum(x => SetBitCountMap[x]));
        }