public string Decrypt(int[] encrypted)
        {
            //массив с закодированными символами
            int[] encodedText = new int[encrypted.Length];

            int counter = 0;

            //расщифровываем первый символ побитовым сложением
            encodedText[0] = (int)key ^ encrypted[0];

            int oldNum = encrypted[0];

            //Для расшифорвки последующих символов исходного текста
            foreach (int num in encrypted)
            {
                counter++;

                if (counter == 1)
                {
                    continue;
                }

                //расшифровка символа
                encodedText[counter - 1] = num ^ oldNum;

                oldNum = num;
            }

            byte[] bytes = ByteOperations.GetBytes(encodedText);

            string byteString = ByteOperations.GetString(bytes);

            return(byteString.Replace("\0", string.Empty));
        }
        void makeKardano()
        {
            //код исходного текста
            byte[] srcBytes = ByteOperations.GetBytes(textBox.Text);
            printToBox(textBox3, srcBytes);

            //побитовое представление текста
            var bitsSrc = new BitArray(srcBytes);

            printBitArray(textBox4, bitsSrc);

            //шифр
            Kardano kardano = new Kardano(kardanoKey);

            int[] encrypted = kardano.Encrypt(textBox.Text);
            printToBox(textBox1, encrypted);

            //зашифрованные символы
            byte[] encryptedString = ByteOperations.GetBytes(encrypted);
            textBox6.Clear();
            textBox6.Text = ByteOperations.GetString(encryptedString).Replace("\0", string.Empty);

            //побитовое представление шифра
            var bitsCrypted = new BitArray(encryptedString);

            printBitArray(textBox5, bitsCrypted);

            //обратный перевод
            textBox2.Clear();
            textBox2.Text = kardano.Decrypt(encrypted);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Шифрование Кардано
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public int[] Encrypt(string text)
        {
            //переводим строку в массив байтов
            byte[] textInBytes = ByteOperations.GetBytes(text);

            //массив с закодированными символами
            int[] codedText = new int[textInBytes.Length];

            //шифруем первый символ побитовым сложением
            codedText[0] = (int)key ^ textInBytes[0];

            int counter = 0;

            uint tempKey = key;

            //Для шифрования последующих символов исходного текста
            foreach (byte byt in textInBytes)
            {
                counter++;

                if (byt == 0 || byt == 4 || counter == 1)
                {
                    continue;
                }

                //модифицирование ключа
                try
                {
                    tempKey = ByteOperations.shiftLeft(tempKey, 1, BITS);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                int num = (int)textInBytes[counter - 1];

                //шифрование символа
                codedText[counter - 1] = (int)tempKey ^ num;

                //если 0
                if (codedText[counter - 1] == 0)
                {
                    codedText[counter - 1] = -1;
                }
            }

            return(codedText);
        }
Ejemplo n.º 4
0
        public string Decrypt(int[] encrypted)
        {
            //массив с закодированными символами
            int[] encodedText = new int[encrypted.Length];

            int counter = 0;

            uint tempKey = key;

            //Для расшифорвки последующих символов исходного текста
            foreach (int num in encrypted)
            {
                if (num == 0)
                {
                    continue;
                }

                //расшифровка символа
                encodedText[counter] = (int)tempKey ^ num;

                //модифицирование ключа
                try
                {
                    tempKey = ByteOperations.shiftLeft(tempKey, 1, BITS);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                counter++;
            }

            byte[] bytes = ByteOperations.GetBytes(encodedText);

            string byteString = ByteOperations.GetString(bytes);

            return(byteString.Replace("\0", string.Empty));
        }
        public int[] Encrypt(string text)
        {
            //переводим строку в массив байтов
            byte[] textInBytes = ByteOperations.GetBytes(text);

            //массив с закодированными символами
            int[] codedText = new int[textInBytes.Length];

            //шифруем первый символ побитовым сложением
            codedText[0] = (int)key ^ textInBytes[0];

            int counter = 0;

            //ключем становится первый зашифрованный символ
            uint tempKey = (uint)codedText[0];

            //Для шифрования последующих символов исходного текста
            foreach (byte byt in textInBytes)
            {
                counter++;

                if (counter == 1)
                {
                    continue;
                }

                int num = (int)textInBytes[counter - 1];

                //шифрование символа
                codedText[counter - 1] = (int)tempKey ^ num;

                //модифицирование ключа
                tempKey = (uint)codedText[counter - 1];
            }

            return(codedText);
        }