コード例 #1
0
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
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));
        }