Exemplo n.º 1
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Генерация ключей
        //--------------------------------

        private static bool[][] GenerateKeys(bool[] Key, bool IsDecrypth)
        {
            var KeyC = new bool[28];
            var KeyD = new bool[28];

            for (var i = 0; i < 56; i++)
            {
                if (i < 28)
                {
                    KeyC[i] = Key[KEYGEN_C0[i] - 1];
                }
                else
                {
                    KeyD[i - 28] = Key[KEYGEN_D0[i - 28] - 1];
                }
            }
            if (EDUtility.DebugKeyGen)
            {
                EDUtility.BlockToBytes(KeyC, "C0:");  //Debug
                EDUtility.BlockToBytes(KeyD, "D0:");  //Debug
            }
            var Keys = new bool[16][];

            for (var i = 0; i < 16; i++)
            {
                if (IsDecrypth)
                {
                    EDUtility.PushToRight(ref KeyC, KEYGEN_CDSDVIG_REVERSE[i]);
                    EDUtility.PushToRight(ref KeyD, KEYGEN_CDSDVIG_REVERSE[i]);
                }
                else
                {
                    EDUtility.PushToLeft(ref KeyC, KEYGEN_CDSDVIG[i]);
                    EDUtility.PushToLeft(ref KeyD, KEYGEN_CDSDVIG[i]);
                }
                Keys[i] = new bool[48];

                var TmpKey = new bool[56];
                KeyC.CopyTo(TmpKey, 0);
                KeyD.CopyTo(TmpKey, 28);

                Keys[i] = KeyGenVibor(TmpKey);

                if (EDUtility.DebugKeyGen)
                {
                    EDUtility.MsgToLog("");
                }
                EDUtility.BlockToBytes(Keys[i], "Ключ " + Convert.ToString(i + 1) + ":"); //Debug
                if (EDUtility.DebugKeyGen)
                {
                    EDUtility.MsgToLog("");
                }
            }

            return(Keys);
        }
Exemplo n.º 2
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Конечная перестановка
        //--------------------------------

        private static bool[] LastPerest(bool[] Block)
        {
            EDUtility.BlockToBytes(Block, "Перестановка массива по обратной IP:"); //Debug
            var NewBlock = new bool[64];

            for (var i = 0; i < 64; i++)
            {
                NewBlock[i] = Block[IP_REVERSE[i] - 1];
            }
            EDUtility.BlockToBytes(NewBlock, "Результат:"); //Debug
            return(NewBlock);
        }
Exemplo n.º 3
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Функция выборки 48-бит ключа
        //--------------------------------

        private static bool[] KeyGenVibor(bool[] Block)
        {
            if (EDUtility.DebugKeyGen)
            {
                EDUtility.BlockToBytes(Block, "Выборка ключа 48-бит:");  //Debug
            }
            var NewBlock = new bool[48];

            for (var i = 0; i < 48; i++)
            {
                NewBlock[i] = Block[KEYGEN_VIBORKA[i] - 1];
            }
            if (EDUtility.DebugKeyGen)
            {
                EDUtility.BlockToBytes(NewBlock, "Результат:");  //Debug
            }
            return(NewBlock);
        }
Exemplo n.º 4
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Функция перестановки P
        //--------------------------------

        private static bool[] PFunction(bool[] Block)
        {
            if (EDUtility.DebugCycles)
            {
                EDUtility.BlockToBytes(Block, "Перестановка массива по P:"); //Debug
            }
            var NewBlock = new bool[32];

            for (var i = 0; i < 32; i++)
            {
                NewBlock[i] = Block[ENCRYPT_PERESTANOVKA[i] - 1];
            }
            if (EDUtility.DebugCycles)
            {
                EDUtility.BlockToBytes(NewBlock, "Результат:"); //Debug
            }
            return(NewBlock);
        }
Exemplo n.º 5
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Функция расширения E
        //--------------------------------

        private static bool[] EFunction(bool[] Block)
        {
            if (EDUtility.DebugCycles)
            {
                EDUtility.BlockToBytes(Block, "Расширение массива по E:"); //Debug
            }
            var NewBlock = new bool[48];

            for (var i = 0; i < 48; i++)
            {
                NewBlock[i] = Block[ENCRYPT_RASHIRENIE[i] - 1];
            }
            if (EDUtility.DebugCycles)
            {
                EDUtility.BlockToBytes(NewBlock, "Результат:"); //Debug
            }
            return(NewBlock);
        }
Exemplo n.º 6
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Функция F
        //--------------------------------

        private static bool[] FFunction(bool[] Block, bool[] Key)
        {
            var NewBlock = EFunction(Block);

            for (var i = 0; i < 48; i++)
            {
                NewBlock[i] = NewBlock[i] ^ Key[i];
            }
            if (EDUtility.DebugCycles)
            {
                EDUtility.BlockToBytes(NewBlock, "Block XOR Key:"); //Debug
            }
            var ResultBlock = SixToFourFunction(NewBlock);

            ResultBlock = PFunction(ResultBlock);

            return(ResultBlock);
        }
Exemplo n.º 7
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Функция преобразования 6-бит блоков в 4-бит
        //--------------------------------

        private static bool[] SixToFourFunction(bool[] Block)
        {
            if (EDUtility.DebugCycles)
            {
                EDUtility.BlockToBytes(Block, "Преобразование 6-бит блоков в 4-бит по S:"); //Debug
            }
            var BBlocks = new bool[8][];

            for (var i = 0; i < 8; i++)
            {
                BBlocks[i] = new bool[4];
            }

            for (var i = 0; i < 8; i++)
            {
                var row = "";
                var col = "";
                for (var j = 0; j < 6; j++)
                {
                    if (j == 0 || j == 5)
                    {
                        if (Block[i * 6 + j])
                        {
                            row += '1';
                        }
                        else
                        {
                            row += '0';
                        }
                    }
                    else
                    {
                        if (Block[i * 6 + j])
                        {
                            col += '1';
                        }
                        else
                        {
                            col += '0';
                        }
                    }
                }

                var Row     = Convert.ToInt32(row, 2);
                var Col     = Convert.ToInt32(col, 2);
                var NewByte = "0000";
                NewByte += Convert.ToString(ENCRYPT_S[i, Row, Col], 2);
                NewByte  = NewByte.Remove(0, NewByte.Length - 4);

                var a = 0;
                foreach (var c in NewByte)
                {
                    if (c == '0')
                    {
                        BBlocks[i][a] = false;
                    }
                    else
                    {
                        BBlocks[i][a] = true;
                    }
                    a++;
                }
            }

            var NewBlock = new bool[32];

            for (var i = 0; i < 8; i++)
            {
                BBlocks[i].CopyTo(NewBlock, i * 4);
            }
            if (EDUtility.DebugCycles)
            {
                EDUtility.BlockToBytes(NewBlock, "Результат:"); //Debug
            }
            return(NewBlock);
        }
Exemplo n.º 8
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        //--------------------------------
        //     Цикл расшифровки
        //--------------------------------

        private static bool[] DecrypthCycle(bool[] Block, bool[] Key)
        {
            var NewKeys = GenerateKeys(Key, true);
            var LPred   = new bool[32];
            var RPred   = new bool[32];
            var L       = new bool[32];
            var R       = new bool[32];

            for (var i = 0; i < 64; i++)
            {
                if (i < 32)
                {
                    LPred[i] = Block[i];
                }
                else
                {
                    RPred[i - 32] = Block[i];
                }
            }

            if (EDUtility.DebugCycles)
            {
                EDUtility.MsgToLog("");
            }
            EDUtility.BlockToBytes(LPred, "Исходный L:");  //Debug
            EDUtility.BlockToBytes(RPred, "Исходный R:");  //Debug
            if (EDUtility.DebugCycles)
            {
                EDUtility.MsgToLog("");
            }

            for (var i = 1; i <= 16; i++)
            {
                var f = FFunction(LPred, NewKeys[i - 1]);
                LPred.CopyTo(R, 0);
                for (var j = 0; j < 32; j++)
                {
                    L[j] = RPred[j] ^ f[j];
                }

                L.CopyTo(LPred, 0);
                R.CopyTo(RPred, 0);

                if (EDUtility.DebugCycles)
                {
                    EDUtility.MsgToLog("");
                }
                EDUtility.BlockToBytes(LPred, "L" + Convert.ToString(i) + ":");  //Debug
                EDUtility.BlockToBytes(RPred, "R" + Convert.ToString(i) + ":");  //Debug
                if (EDUtility.DebugCycles)
                {
                    EDUtility.MsgToLog("");
                }
            }

            var NewBlock = new bool[64];

            L.CopyTo(NewBlock, 0);
            R.CopyTo(NewBlock, L.Length);

            return(NewBlock);
        }
Exemplo n.º 9
0
Arquivo: Form1.cs Projeto: 0leXis/DES
        private void buttonEncr_Click(object sender, EventArgs e)
        {
            textBoxLog.Clear();
            #region DESEncr
            {
                EDUtility.MsgToLog("--------------------------");
                EDUtility.MsgToLog("Шифрование DES");
                EDUtility.MsgToLog("--------------------------");
                if (textBoxKey.Text.Length != 8)
                {
                    MessageBox.Show("Ключ должен содержать 8 символов!");
                }
                else
                {
                    while (textBoxMsg.Text.Length % 8 != 0)
                    {
                        textBoxMsg.Text += " ";
                    }

                    var MsgByte = new byte[textBoxMsg.Text.Length / 8][];
                    for (var i = 0; i < MsgByte.Length; i++)
                    {
                        MsgByte[i] = new byte[8];
                        for (var j = 0; j < 8; j++)
                        {
                            MsgByte[i][j] = Convert.ToByte(textBoxMsg.Text[8 * i + j]);
                        }
                    }

                    var Key = new byte[8];
                    for (var i = 0; i < 8; i++)
                    {
                        Key[i] = Convert.ToByte(textBoxKey.Text[i]);
                    }
                    var KeyBlock = EDUtility.ByteToBlocks(Key);

                    textBoxRes.Clear();

                    foreach (var a in MsgByte)
                    {
                        EDUtility.MsgToLog("**************************");
                        EDUtility.MsgToLog("Шифрование блока");
                        EDUtility.MsgToLog("**************************");
                        var bytestolog = "Исходные байты: ";
                        foreach (var b in a)
                        {
                            bytestolog += Convert.ToString(b) + " ";
                        }
                        EDUtility.MsgToLog(bytestolog);
                        EDUtility.MsgToLog("");

                        var Block = EDUtility.ByteToBlocks(a);
                        Block = DESEncryptDecrypt.Encrypth(Block, KeyBlock);
                        var EncrBlock = EDUtility.BlockToBytes(Block);

                        foreach (var b in EncrBlock)
                        {
                            textBoxRes.Text += Convert.ToChar(b);
                        }

                        EDUtility.MsgToLog("");
                        bytestolog = "Зашифрованные байты: ";
                        foreach (var b in EncrBlock)
                        {
                            bytestolog += Convert.ToString(b) + " ";
                        }
                        EDUtility.MsgToLog(bytestolog);
                        EDUtility.MsgToLog("**************************");
                        EDUtility.MsgToLog("Шифрование блока завершено");
                        EDUtility.MsgToLog("**************************");
                    }
                    EDUtility.MsgToLog("--------------------------");
                    EDUtility.MsgToLog("Шифрование DES завершено");
                    EDUtility.MsgToLog("--------------------------");
                }
            }
            #endregion DesEncr
        }