Ejemplo n.º 1
0
        public void decryption(string filename)
        {
            string ciphertext = ReadTextInFile.readtext(filename);
            string text       = new string("");
            string buffer     = new string("");
            int    firstlette = 0;
            bool   endletter  = false;

            for (int i = 0; i < ciphertext.Length; i++)
            {
                if (this.Isletter(ciphertext[i]))//буква или была буква
                {
                    endletter  = true;
                    firstlette = i;
                    for (int j = i + 1; j < ciphertext.Length; j++, i++)
                    {
                        if (this.Isletter(ciphertext[j]))
                        {
                            int    secondlette = j;
                            string bigram      = new string("");
                            bigram += ciphertext[firstlette];
                            bigram += ciphertext[secondlette];

                            List <int> coord = coordinatesbigram(bigram);

                            text += LetterFromNumber(coord[0]);
                            text += buffer;
                            text += LetterFromNumber(coord[1]);

                            buffer = null;
                            i      = j;
                            break;
                        }
                        else
                        {
                            buffer += ciphertext[j];
                        }
                    }
                }
                else
                {
                    text += ciphertext[i];
                }
                if (text.Length > 10000)
                {
                    WriteTextInFile.writetext("OriginalText.txt", text);
                    text = null;
                }
            }
            if (endletter)
            {
                text += ciphertext[firstlette];
                text += buffer;
            }
            WriteTextInFile.writetext("OriginalText.txt", text);
        }
Ejemplo n.º 2
0
        //private int NumberLetter(char c)
        //{
        //    switch (c)
        //    {
        //        case 'a':return 0;
        //        case 'b': return 1;
        //        case 'c': return 2;
        //        case 'd': return 3;
        //        case 'e': return 4;
        //        case 'f': return 5;
        //        case 'g': return 6;
        //        case 'h': return 7;
        //        case 'i': return 8;
        //        case 'j': return 9;
        //        case 'k': return 10;
        //        case 'l': return 11;
        //        case 'm': return 12;
        //        case 'n': return 13;
        //        case 'o':return  14;
        //        case 'p': return 15;
        //        case 'q': return 16;
        //        case 'r': return 17;
        //        case 's': return 18;
        //        case 't': return 19;
        //        case 'u': return 20;
        //        case 'v': return 21;
        //        case 'w': return 22;
        //        case 'x': return 23;
        //        case 'y': return 24;
        //        case 'z': return 25;
        //        default: return -1;
        //    }
        //}



        public void encryption(string filename)
        {
            string inputtext  = ReadTextInFile.readtext(filename);
            string cryptogram = new string("");
            string buffer     = new string("");
            int    firstlette = 0;
            bool   endletter  = false;

            for (int i = 0; i < inputtext.Length; i++)
            {
                if (this.Isletter(inputtext[i])) //буква или была буква
                {
                    endletter  = true;
                    firstlette = i;
                    for (int j = i + 1; j < inputtext.Length; j++, i++)
                    {
                        if (this.Isletter(inputtext[j]))
                        {
                            int secondlette     = j;
                            int horizontalIndex = this.NumberLetter(inputtext[firstlette]);
                            int verticalIndex   = this.NumberLetter(inputtext[secondlette]);

                            cryptogram += this.table[horizontalIndex, verticalIndex][0];
                            cryptogram += buffer;
                            cryptogram += this.table[horizontalIndex, verticalIndex][1];
                            buffer      = null;
                            endletter   = false;
                            i           = j;
                            break;
                        }
                        else
                        {
                            buffer += inputtext[j];
                        }
                    }
                }
                else
                {
                    cryptogram += inputtext[i];
                }
                if (cryptogram.Length > 10000)
                {
                    WriteTextInFile.writetext("cryptogram.txt", cryptogram);
                    cryptogram = null;
                }
            }


            if (endletter)
            {
                cryptogram += inputtext[firstlette];
                cryptogram += buffer;
            }
            WriteTextInFile.writetext("cryptogram.txt", cryptogram);
        }