Example #1
0
File: Form1.cs Project: 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);
        }
Example #2
0
File: Form1.cs Project: 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);
        }
Example #3
0
File: Form1.cs Project: 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
        }