コード例 #1
0
        public static string Cyphering(string openMessageBlockDuoSS, string key)
        {
            int []        blocksAfterInputWhiting  = new int [4];
            int []        blocksAfterOutputWhiting = new int [4];
            List <string> closedMessage            = new List <string>();
            var           subkeysArr  = SubkeysGen.SubKeysGeneration(key);
            var           XDataBlocks = StringMethods.StringToList(openMessageBlockDuoSS, XBlockLength);

            for (int i = 0; i < 4; i++)
            {
                blocksAfterInputWhiting[i] = Convert.ToInt32(XDataBlocks[i], 2) ^ Convert.ToInt32(subkeysArr[i], 2);
            }

            for (int round = 0; round < 16; round++)
            {
                blocksAfterInputWhiting = OneRound(blocksAfterInputWhiting, subkeysArr, round);
            }

            for (int i = 0; i < 4; i++)
            {
                blocksAfterOutputWhiting[i] = blocksAfterInputWhiting[i] ^ Convert.ToInt32(subkeysArr[i + 36], 2);
                closedMessage.Add(StringMethods.MyConvertToString(blocksAfterOutputWhiting[i], 2, 32));
            }
            var resultStringDuoSS = StringMethods.ListToString(closedMessage);

            return(resultStringDuoSS);
        }
コード例 #2
0
        public static string Decyphering(string closedMessageBlockDuoSS, string key)
        {
            int[]         blocksBeforeInputWhiting  = new int[4];
            Int32[]       blocksBeforeOutputWhiting = new Int32[4];
            List <string> openMessage = new List <string>();
            var           subkeysArr  = SubkeysGen.SubKeysGeneration(key);
            var           XDataBlocks = StringMethods.StringToList(closedMessageBlockDuoSS, XBlockLength);

            for (int i = 0; i < 4; i++)
            {
                blocksBeforeOutputWhiting[i] = Convert.ToInt32(XDataBlocks[i], 2) ^ Convert.ToInt32(subkeysArr[i + 36], 2);
            }
            for (int round = 15; round > -1; round--)
            {
                blocksBeforeOutputWhiting = OneRoundReverse(blocksBeforeOutputWhiting, subkeysArr, round);
            }
            for (int i = 0; i < 4; i++)
            {
                blocksBeforeInputWhiting[i] = blocksBeforeOutputWhiting[i] ^ Convert.ToInt32(subkeysArr[i], 2);
                openMessage.Add(StringMethods.MyConvertToString(blocksBeforeInputWhiting[i], 2, 32));
            }
            var resultStringDuoSS = StringMethods.ListToString(openMessage);

            return(resultStringDuoSS);
        }
コード例 #3
0
        public static void LogEncrypt(string key, string openMes, string fileWay)
        {
            StringBuilder strBuild = new StringBuilder();

            int[]         blocksAfterInputWhiting  = new int[4];
            int[]         blocksAfterOutputWhiting = new int[4];
            List <string> closedMessage            = new List <string>();
            var           subkeysArr = SubkeysGen.SubKeysGeneration(key);

            strBuild.AppendLine($"Open message block: {openMes};");
            strBuild.AppendLine($"Key({key.Length.ToString()}): {key};");
            strBuild.AppendLine($"RoundKeys: {subkeysArr[0]};");
            for (int i = 1; i < 40; i++)
            {
                strBuild.AppendLine($"           {subkeysArr[i]};");
            }
            var XDataBlocks = StringMethods.StringToList(openMes, TwofishAlgo.XBlockLength);

            strBuild.AppendLine();
            strBuild.AppendLine($"**************************Input Whiting**************************");
            strBuild.AppendLine();

            for (int i = 0; i < 4; i++)
            {
                blocksAfterInputWhiting[i] = Convert.ToInt32(XDataBlocks[i], 2) ^ Convert.ToInt32(subkeysArr[i], 2);
                strBuild.AppendLine($"{XDataBlocks[i]} xor {subkeysArr[i]} = {StringMethods.MyConvertToString(blocksAfterInputWhiting[i], 2, 32)};");
            }
            strBuild.AppendLine();
            strBuild.AppendLine($"**************************The Main Cycle**************************");
            File.AppendAllText(fileWay, strBuild.ToString());

            for (int round = 0; round < 16; round++)
            {
                blocksAfterInputWhiting = OneRoundLog(blocksAfterInputWhiting, subkeysArr, round, fileWay);
            }
            strBuild.AppendLine();
            strBuild.AppendLine($"**************************Output Whiting**************************");
            strBuild.AppendLine();

            for (int i = 0; i < 4; i++)
            {
                blocksAfterOutputWhiting[i] = blocksAfterInputWhiting[i] ^ Convert.ToInt32(subkeysArr[i + 36], 2);
                closedMessage.Add(StringMethods.MyConvertToString(blocksAfterOutputWhiting[i], 2, 32));
                strBuild.AppendLine($"{StringMethods.MyConvertToString(blocksAfterInputWhiting[i], 2, 32)} xor {subkeysArr[i+36]} = {StringMethods.MyConvertToString(blocksAfterOutputWhiting[i], 2, 32)};");
            }
            var resultStringDuoSS = StringMethods.ListToString(closedMessage);

            strBuild.AppendLine($"Closed message block: {resultStringDuoSS};");
        }