Exemplo n.º 1
0
        public string Encrypt(string plaintext, string key)
        {
            // 1. Key Expansion (derive round keys from cipher key using Rijndael's key schedule) <- done per round

            // data into blocks
            var plaintextBytes = Encoding.ASCII.GetBytes(plaintext);

            var dataBytes = new List <byte[]>();

            for (var i = 0; i < plaintextBytes.Length; i += 16)
            {
                var bytes = new byte[16];
                Buffer.BlockCopy(plaintextBytes, i, bytes, 0, 16);
                dataBytes.Add(bytes);
            }

            var keyBytes = Encoding.ASCII.GetBytes(key);
            var round    = 0;

            // 2. AddRoundKey: "XORs a round key to the internal state"
            round = 1;
            for (var i = 0; i < dataBytes.Count; i++)
            {
                dataBytes[i] = Xor.ByteArrays(dataBytes[i], keyBytes);
            }

            keyBytes = GetRoundKey(keyBytes, round);

            // 3. REPEAT Rounds - ECB => for each block
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < dataBytes.Count; j++)
                {
                    // 1. SubBytes: "Replaces each bytes with another byte according to an S-box" - Substitution
                    // "confusion to obscure the relationship of each byte"
                    dataBytes[j] = SubstituteBytes(dataBytes[j]);

                    // 2. ShiftRows: "Shifts the i-th row of i positions, for i ranging from 0 to 3" - Permutation
                    //dataBytes[j] =

                    // 3. MixColumns: "Applies the same linear transformation to each of the 4 columns of the state"  - Permutation

                    // 4. AddRoundKey
                }
            }



            // 4. Final round


            // 1. SubBytes
            // 2. ShiftRows
            // 3. AddRoundKey

            throw new NotImplementedException();
        }
Exemplo n.º 2
0
        public static Dictionary <string, string> BruteForceSingleByte(string cipherText)
        {
            var outputs = new Dictionary <string, string>();

            foreach (var key in Enumerable.Range(0, 127))
            {
                var keyAsHex    = Convert.ToString(key, 16);
                var expandedKey = ExpandKey(cipherText, keyAsHex);

                var outputHex   = Xor.HexStrings(cipherText, expandedKey);
                var outputBytes = Hex.StringToBytes(outputHex);
                outputs.Add(keyAsHex, Encoding.ASCII.GetString(outputBytes));
            }

            return(outputs);
        }
Exemplo n.º 3
0
        public static int GetHammingDistance(this byte[] x, byte[] y)
        {
            if (x.Length != y.Length)
            {
                throw new ArgumentException("values must be same length");
            }

            var distance = 0;

            for (var i = 0; i < x.Length; i++)
            {
                var value = (int)Xor.Bytes(x[i], y[i]);

                while (value != 0)
                {
                    distance++;
                    value &= value - 1;
                }
            }

            return(distance);
        }
Exemplo n.º 4
0
        private static string Challenge6(string input)
        {
            string hexString = Hex.Base64ToHex(input);

            //hexString = testString;
            byte[] inputBytes = Encoding.Default.GetBytes(Util.StringToBinary(hexString));

            var probableKeySize = ProbableKeySize(hexString);

            inputBytes = Hex.HexStringToHex(hexString);
            List <byte[]> transposed = new List <byte[]>();

            for (int i = 0; i < probableKeySize; i++)
            {
                List <byte> transposedBlock = new List <byte>();
                for (int j = i; j <= inputBytes.Length - 1; j += probableKeySize)
                {
                    transposedBlock.Add(inputBytes[j]);
                }

                transposed.Add(transposedBlock.ToArray());
            }

            string key = "";

            foreach (byte[] bytes in transposed)
            {
                string byteString = BitConverter.ToString(bytes).Replace("-", "").ToLower();
                key += Xor.singeByteXorDecode(Util.GenerateCharArray().ToCharArray(), byteString);
            }

            string result = Hex.repeatingKeyXorDecode(hexString, key);

            //Console.WriteLine(result);
            return(result);
        }