Пример #1
0
        public static string decipherCText(string Text, char[] key)
        {
            int[] cText = new int[Text.Length];

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

            int[] plainText = new int[cText.Length];

            if (key.Length == 0)
            {
                throw new Exception("Empty key");
            }

            for (int i = 0; i <= plainText.Length / key.Length; i++)
            {
                for (int j = 0; j < key.Length; j++)
                {
                    if ((key.Length * i + j) < cText.Length)
                    {
                        int xor = cText[key.Length * i + j] ^ key[j];
                        plainText[key.Length * i + j] = xor;
                    }
                }
            }

            return(IntConversion.intsToHex(plainText));
        }
Пример #2
0
        public static string decipherCTextHex(string hexText, char[] key)
        {
            int[] cText = IntConversion.hexToInts(hexText);

            int[] plainText = new int[cText.Length];

            if (key.Length == 0)
            {
                throw new Exception("Empty key");
            }
            if (hexText.Length % key.Length != 0)
            {
                throw new Exception("Key doesn't fit");
            }

            for (int i = 0; i < plainText.Length / key.Length; i++)
            {
                for (int j = 0; j < key.Length; j++)
                {
                    plainText[i] = cText[key.Length * i + j] ^ key[j];
                }
            }

            return(IntConversion.intsToString(plainText));
        }
Пример #3
0
        public static string decipherCText(string hexText, char key)
        {
            int[] cText = IntConversion.hexToInts(hexText);

            int[] plainText = new int[cText.Length];

            for (int i = 0; i < plainText.Length; i++)
            {
                plainText[i] = cText[i] ^ key;
            }

            return(IntConversion.intsToString(plainText));
        }
Пример #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));
        }
Пример #5
0
        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);
        }