Esempio n. 1
0
        private string CheckThat(string key, string cipher)
        {
            string _result = SomeInstruments.DelSym(key);

            if (cipher == "Rail_Fence_ENG" || cipher == "Rail_Fence_ALL")
            {
                _result = SomeInstruments.DelAlp(_result);
                try
                {
                    int.Parse(_result);
                }
                catch (FormatException)
                {
                    _result = "";
                }
            }
            else if (cipher == "Playfair_ENG" || cipher == "Vigenere_ENG")
            {
                _result = SomeInstruments.ForText(_result);
            }
            else
            {
                _result = SomeInstruments.ForTextRUS(_result);
            }
            return(_result.ToLower());
        }
Esempio n. 2
0
        static public string DeCipherENG(string FileText, int key)
        {
            if (key == 0 || key == 1)
            {
                return(FileText);
            }
            string text = SomeInstruments.ForText(FileText.ToLower());
            int    row, period = 2 * (key - 1), k = 0;
            var    a              = new string[key, text.Length];
            var    _DeChipherText = new char[text.Length];

            for (int j = 0; j < key; j++)
            {
                for (int i = 0; i < text.Length; i++)
                {
                    row = key - 1 - Math.Abs(key - 1 - i % period);
                    if (row == j)
                    {
                        _DeChipherText[i] = text[k++];
                    }
                }
            }
            string result = SomeInstruments.CreateText(FileText, string.Join("", _DeChipherText));

            return(result);
        }
Esempio n. 3
0
        static public string Cipher(string FileText, string key)
        {
            key = key.ToLower();
            var _text      = SomeInstruments.ForText(FileText.ToLower());
            var _bigRam    = new List <string>();
            var _resBigRam = new List <string>();

            foreach (var item in PrepString(_text))
            {
                _bigRam.Add(item);
            }

            string temp, _key = CreateKey(key);
            int    _first, _second, dif;

            foreach (var item in _bigRam)
            {
                if (_key.IndexOf(item[0]) / 5 == _key.IndexOf(item[1]) / 5) // one row
                {
                    _first  = (_key.IndexOf(item[0]) + 1) % 5 == 0 ? _key.IndexOf(item[0]) - 4 : _key.IndexOf(item[0]) + 1;
                    _second = (_key.IndexOf(item[1]) + 1) % 5 == 0 ? _key.IndexOf(item[1]) - 4 : _key.IndexOf(item[1]) + 1;
                    temp    = _key[_first].ToString() + _key[_second].ToString();
                }
                else if (_key.IndexOf(item[0]) % 5 == _key.IndexOf(item[1]) % 5) // one column
                {
                    _first  = (_key.IndexOf(item[0]) + 5) % 25;
                    _second = (_key.IndexOf(item[1]) + 5) % 25;
                    temp    = _key[_first].ToString() + _key[_second].ToString();
                }
                else // dif angle square
                {
                    dif     = _key.IndexOf(item[0]) % 5 - _key.IndexOf(item[1]) % 5;
                    _first  = _key.IndexOf(item[0]) - dif;
                    _second = _key.IndexOf(item[1]) + dif;
                    temp    = _key[_first].ToString() + _key[_second].ToString();
                }
                _resBigRam.Add(temp);
            }
            string _result = "";

            foreach (var item in _resBigRam)
            {
                _result += item;
            }
            _result = SomeInstruments.CreateText(FileText, _result);
            return(_result);
        }
Esempio n. 4
0
        static public string CipherENG(string FileText, int key)
        {
            if (key == 0 || key == 1)
            {
                return(FileText);
            }
            string text = SomeInstruments.ForText(FileText.ToLower());
            var    _CipherText = new string[key];
            int    row, period = 2 * (key - 1);

            for (int i = 0; i < text.Length; i++)
            {
                row = key - 1 - Math.Abs(key - 1 - i % period);
                _CipherText[row] += text[i];
            }
            string result = SomeInstruments.CreateText(FileText, string.Join("", _CipherText));

            return(result);
        }
Esempio n. 5
0
        static public string DeCipherRUS(string FileText, string key)
        {
            string text   = SomeInstruments.ForTextRUS(FileText.ToLower());
            string result = "";

            text.ToLower();
            // create a key
            int _priLenKey = key.Length;

            while (key.Length < text.Length)
            {
                key += alphabetRUS[(alphabetRUS.IndexOf(key[key.Length - _priLenKey]) + 1) % 26];
            }
            // create chiphertext
            int _index;

            for (int i = 0; i < text.Length; i++)
            {
                _index  = (alphabetRUS.IndexOf(text[i]) + 33 - alphabetRUS.IndexOf(key[i])) % 33;
                result += alphabetRUS[_index];
            }
            return(SomeInstruments.CreateTextRUS(FileText, result));
        }
Esempio n. 6
0
        static public string DeCipher(string FileText, string key)
        {
            key = key.ToLower();
            var _text      = SomeInstruments.ForText(FileText.ToLower());
            var _bigRam    = new List <string>();
            var _resBigRam = new List <string>();

            foreach (var item in PrepString(_text))
            {
                _bigRam.Add(item);
            }

            string temp, _key = CreateKey(key);
            int    _first, _second, dif;

            foreach (var item in _bigRam)
            {
                if (_key.IndexOf(item[0]) / 5 == _key.IndexOf(item[1]) / 5) // one row
                {
                    _first  = _key.IndexOf(item[0]) % 5 == 0 ? _key.IndexOf(item[0]) + 4 : _key.IndexOf(item[0]) - 1;
                    _second = _key.IndexOf(item[1]) % 5 == 0 ? _key.IndexOf(item[1]) + 4 : _key.IndexOf(item[1]) - 1;
                    temp    = _key[_first].ToString() + _key[_second].ToString();
                }
                else if (_key.IndexOf(item[0]) % 5 == _key.IndexOf(item[1]) % 5) // one column
                {
                    _first  = _key.IndexOf(item[0]) - 5 < 0 ? 25 - 5 + _key.IndexOf(item[0]) : _key.IndexOf(item[0]) - 5;
                    _second = _key.IndexOf(item[1]) - 5 < 0 ? 25 - 5 + _key.IndexOf(item[1]) : _key.IndexOf(item[1]) - 5;
                    temp    = _key[_first].ToString() + _key[_second].ToString();
                }
                else // dif angle square
                {
                    dif     = _key.IndexOf(item[0]) % 5 - _key.IndexOf(item[1]) % 5;
                    _first  = _key.IndexOf(item[0]) - dif;
                    _second = _key.IndexOf(item[1]) + dif;
                    temp    = _key[_first].ToString() + _key[_second].ToString();
                }
                _resBigRam.Add(temp);
            }
            string _result = "";

            foreach (var item in _resBigRam)
            {
                _result += item;
            }

            if (_result[_result.Length - 1] == 'x')
            {
                _result = _result.Substring(0, _result.Length - 1);
            }

            string _temp = "";

            for (int i = 0; i < _result.Length; i++)
            {
                if (!(_result[i] == 'x' && _result[i - 1] == _result[i + 1]))
                {
                    _temp += _result[i];
                }
            }

            _result = SomeInstruments.CreateText(FileText, _temp);
            return(_result);
        }
Esempio n. 7
0
        private void button1_Click(object sender, EventArgs e)
        {
            string key = _textBox.Text;

            key = CheckThat(key, comboBox1.Text);

            if (key == "" || savePath == "" || loadPath == "" || key == "")
            {
                lamp.BackColor = Color.DarkRed;
                return;
            }
            string text = SomeInstruments.TakeFromFile(loadPath);
            string code = "";

            if (checkBox1.Checked)
            {
                if (comboBox1.Text == "Vigenere_ENG")
                {
                    code = Vigenere.Cipher(text, key);
                }
                else if (comboBox1.Text == "Rail_Fence_ENG")
                {
                    code = RailFence.CipherENG(text, int.Parse(key));
                }
                else if (comboBox1.Text == "Rail_Fence_ALL")
                {
                    code = RailFence.CipherALL(text, int.Parse(key));
                }
                else if (comboBox1.Text == "Playfair_ENG")
                {
                    code = Playfair.Cipher(text, key);
                }
                else if (comboBox1.Text == "Vigenere_RUS")
                {
                    code = Vigenere.CipherRUS(text, key);
                }
            }
            else
            {
                if (comboBox1.Text == "Vigenere_ENG")
                {
                    code = Vigenere.DeCipher(text, key);
                }
                else if (comboBox1.Text == "Rail_Fence_ENG")
                {
                    code = RailFence.DeCipherENG(text, int.Parse(key));
                }
                else if (comboBox1.Text == "Playfair_ENG")
                {
                    code = Playfair.DeCipher(text, key);
                }
                else if (comboBox1.Text == "Rail_Fence_ALL")
                {
                    code = RailFence.DeCipherALL(text, int.Parse(key));
                }
                else if (comboBox1.Text == "Vigenere_RUS")
                {
                    code = Vigenere.DeCipherRUS(text, key);
                }
            }
            SomeInstruments.SaveInFile(savePath, code.Trim());
            savePath            = null;
            loadPath            = null;
            lamp.BackColor      = Color.DarkGreen;
            lamp_Load.BackColor = Color.Gray;
            lamp_Save.BackColor = Color.Gray;
        }