Exemple #1
0
        public static void Challenge2()
        {
            const string plaintext = "1c0111001f010100061a024b53535009181c";
            const string cipher    = "686974207468652062756c6c277320657965";

            Console.WriteLine(StringConverters.BitsToHex(StringConverters.HexToBits(plaintext).Xor(StringConverters.HexToBits(cipher))));
        }
        //Hamming dist
        public static int hammingDist(string s1, string s2)
        {
            int dist = 0;


            int[] s1Ints = StringConverters.stringToInts(s1);
            int[] s2Ints = StringConverters.stringToInts(s2);


            if (s1Ints.Length != s2Ints.Length)
            {
                throw new Exception("Different lengths");
            }

            for (int i = 0; i < s1Ints.Length; i++)
            {
                int temp1 = s1Ints[i];
                int temp2 = s2Ints[i];


                for (int j = 0; j < 8; j++)
                {
                    if ((temp1 % 2) != (temp2 % 2))
                    {
                        dist++;
                    }

                    temp1 /= 2;
                    temp2 /= 2;
                }
            }


            return(dist);
        }
Exemple #3
0
        public static string decipherCText(string cText, char cipher)
        {
            BitArray cTextBits = StringConverters.HexToBits(cText);

            BitArray cipherBits = StringConverters.CharToBits(cipher, cText.Length / 2);

            cTextBits.Xor(cipherBits);


            return(StringConverters.BitsToString(cTextBits));
        }
Exemple #4
0
        public static void Challenge7()
        {
            StreamReader sr = new StreamReader(@"C:\Users\User\Documents\Visual Studio 2015\Projects\cryptopals\cryptopals\set 1 challenge 7.txt");

            string base64 = "";
            string temp   = "";

            while ((temp = sr.ReadLine()) != null)
            {
                base64 = base64 + temp;
            }
            BitArray bits = StringConverters.Base64ToBits(base64);
            string   hex  = StringConverters.BitsToHex(bits);


            string keyS = "YELLOW SUBMARINE";

            byte[] key = new byte[16];
            byte[] iv  = new byte[16];
            for (int i = 0; i < 16; i++)
            {
                key[i] = Convert.ToByte(keyS[i]);
                iv[i]  = Convert.ToByte(0);
            }

            int[]  cText  = IntConversion.hexToInts(hex);
            byte[] cTextB = new byte[cText.Length];
            for (int i = 0; i < cText.Length; i++)
            {
                cTextB[i] = (byte)cText[i];
            }
            byte[] pTextB = new byte[cText.Length];
            int[]  pText  = new int[cText.Length];

            AesManaged alg = new AesManaged {
                KeySize = 128, Key = key, BlockSize = 128, Mode = CipherMode.ECB, Padding = PaddingMode.Zeros, IV = iv
            };

            ICryptoTransform dec = alg.CreateDecryptor(alg.Key, alg.IV);

            dec.TransformBlock(cTextB, 0, cText.Length, pTextB, 0);

            for (int i = 0; i < cText.Length; i++)
            {
                pText[i] = pTextB[i];
            }

            Console.WriteLine(IntConversion.intsToString(pText));
        }
Exemple #5
0
        public static void Challenge6()
        {
            StreamReader sr = new StreamReader(@"C:\Users\User\Documents\Visual Studio 2015\Projects\cryptopals\cryptopals\set 1 challenge 6.txt");

            string base64 = "";
            string temp   = "";

            while ((temp = sr.ReadLine()) != null)
            {
                base64 = base64 + temp;
            }

            BitArray bits = StringConverters.Base64ToBits(base64);
            string   hex  = StringConverters.BitsToHex(bits);



            Console.WriteLine(IntMultiVigenere.crackMV(hex));
        }
        public static string crackMV(string hex)
        {
            string pText = "";

            int[] hexBytes = IntConversion.hexToInts(hex);

            int likelyKSize = findKeySize(hexBytes);

            List <int[]> transposedBlocks = transposeBlocks(hexBytes, likelyKSize);
            List <int[]> decipheredBlocks = new List <int[]>();

            for (int i = 0; i < transposedBlocks.Count; i++)
            {
                decipheredBlocks.Add(StringConverters.stringToInts(IntSingleVigenere.decipherCaesar(IntConversion.intsToHex(transposedBlocks[i])).Item3));
            }

            pText = IntConversion.intsToString(undoTranspose(decipheredBlocks));
            //Console.WriteLine(likelyKSize + " : " + pText);

            return(pText);
        }
Exemple #7
0
        public static void Challenge1()
        {
            const string hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";

            Console.WriteLine(StringConverters.BitsToBase64(StringConverters.HexToBits(hex)));
        }