⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processdes.cs

📁 g729 coding ipaddressing
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleCryptographer.DES
{
    class ProcessDES : CommonProcess
    {
        public ProcessDES(Form1.ProgressEventHandler IncrementProgress, Form1.ProgressInitHandler InitProgress)
        {
            this.IncrementProgress = IncrementProgress;
            this.InitProgress = InitProgress;
        }
        
        #region Event for progress bar
        public event Form1.ProgressInitHandler InitProgress;
        public event Form1.ProgressEventHandler IncrementProgress;

        protected virtual void OnIncrementProgress(ProgressEventArgs e)
        {
            if (IncrementProgress != null)
                IncrementProgress(this, e);
        }

        protected virtual void OnInitProgress(ProgressInitArgs e)
        {
            if (InitProgress != null)
                InitProgress(this, e);
        }
        #endregion

        #region Encryption Process
        public override/*static*/ string EncryptionStart(string text, string key, bool IsTextBinary)
        {
            #region Get 16 sub-keys using key
                        
            string hex_key = this.FromTextToHex(key);
            string binary_key = this.FromHexToBinary(hex_key);
            string key_plus = this.DoPermutation(binary_key, DESData.pc_1);

            string C0 = "", D0 = "";

            C0 = this.SetLeftHalvesKey(key_plus);
            D0 = this.SetRightHalvesKey(key_plus);

            Keys keys = this.SetAllKeys(C0, D0);

            #endregion

            #region Encrypt process

            //string hex_text = this.FromTextToHex(text);
            string binaryText = "";

            if (IsTextBinary == false)
            {
                binaryText = this.FromTextToBinary(text);
            }
            else
            {
                binaryText = text;
            }

            binaryText = this.setTextMutipleOf64Bits(binaryText);

            #region Initialize Progress Bar
            OnInitProgress(new ProgressInitArgs(binaryText.Length));
            #endregion

            StringBuilder EncryptedTextBuilder = new StringBuilder(binaryText.Length);

            for (int i = 0; i < (binaryText.Length / 64); i++)
            {
                string PermutatedText = this.DoPermutation(binaryText.Substring(i * 64, 64), DESData.ip);

                string L0 = "", R0 = "";

                L0 = this.SetLeftHalvesKey(PermutatedText);
                R0 = this.SetRightHalvesKey(PermutatedText);

                string FinalText = this.FinalEncription(L0, R0, keys, false);

                EncryptedTextBuilder.Append(FinalText);

                #region Increase Progress Bar
                OnIncrementProgress(new ProgressEventArgs(FinalText.Length));
                #endregion
            }

            return EncryptedTextBuilder.ToString();

            #endregion
        }
        #endregion

        #region Decryption Process 
        public override/*static*/ string DecryptionStart(string text, string key, bool IsTextBinary)
        {
            #region Get 16 sub-keys using key

            string hex_key = this.FromTextToHex(key);
            string binary_key = this.FromHexToBinary(hex_key);
            string key_plus = this.DoPermutation(binary_key, DESData.pc_1);

            string C0 = "", D0 = "";

            C0 = this.SetLeftHalvesKey(key_plus);
            D0 = this.SetRightHalvesKey(key_plus);

            Keys keys = this.SetAllKeys(C0, D0);

            #endregion

            #region Decrypt process

            string binaryText = "";

            if (IsTextBinary == false)
            {
                binaryText = this.FromTextToBinary(text);
            }
            else
            {
                binaryText = text;
            }

            binaryText = this.setTextMutipleOf64Bits(binaryText);

            #region Initialize Progress Bar
            OnInitProgress(new ProgressInitArgs(binaryText.Length));
            #endregion

            StringBuilder DecryptedTextBuilder = new StringBuilder(binaryText.Length);

            for (int i = 0; i < (binaryText.Length / 64); i++)
            {
                string PermutatedText = this.DoPermutation(binaryText.Substring(i * 64, 64), DESData.ip);

                string L0 = "", R0 = "";

                L0 = this.SetLeftHalvesKey(PermutatedText);
                R0 = this.SetRightHalvesKey(PermutatedText);

                string FinalText = this.FinalEncription(L0, R0, keys, true);

                #region It's for correct subtracted '0' that have added for set text multiple of 64bit
                if ((i * 64 + 64) == binaryText.Length)
                {
                    StringBuilder last_text = new StringBuilder(FinalText.TrimEnd('0'));

                    int count = FinalText.Length - last_text.Length;

                    if ((count % 8) != 0)
                    {
                        count = 8 - (count % 8);
                    }

                    string append_text = "";

                    for (int k = 0; k < count; k++)
                    {
                        append_text += "0";
                    }

                    DecryptedTextBuilder.Append(last_text.ToString() + append_text);
                }
                #endregion
                else
                {
                    DecryptedTextBuilder.Append(FinalText);
                }

                //DecryptedTextBuilder.Append(FinalText);

                #region Increase Progress Bar
                OnIncrementProgress(new ProgressEventArgs(FinalText.Length));
                #endregion
            }

            return DecryptedTextBuilder.ToString();//.TrimEnd('0');

            #endregion
        }
        #endregion
        
        #region Check a string whether Korean or not. - not used in this program.
        public /*static*/ bool IsKorean(char word)
        {
            if (word >= '\xAC00' && word <= '\xD7AF')
            {
                return true;
            }

            if (word >= '\x3130' && word <= '\x318F')
            {
                return true;
            }

            return false;
        }
        #endregion

        #region Transform a text to a hex
        public string FromTextToHex(string text)
        {
            string hexstring = "";

            foreach (char word in text)
            {
                hexstring += String.Format("{0:X}", Convert.ToInt32(word));
            }

            return hexstring;
        }
        #endregion

        #region Transform a hex or a binary number to text
        public string FromHexToText(string hexstring)
        {
            StringBuilder text = new StringBuilder(hexstring.Length / 2);

            for (int i = 0; i < (hexstring.Length / 2); i++)
            {
                string word = hexstring.Substring(i * 2, 2);
                text.Append((char)Convert.ToInt32(word, 16));
            }

            return text.ToString();
        }

        public /*static*/ string FromBinaryToText(string binarystring)        
        {
            StringBuilder text = new StringBuilder(binarystring.Length / 8);

            for (int i = 0; i < (binarystring.Length / 8); i++)
            {
                string word = binarystring.Substring(i * 8, 8);
                text.Append((char)Convert.ToInt32(word, 2));
                //text += (char)Convert.ToInt32(word, 16);
            }

            return text.ToString();
        }
        #endregion
        
        #region Set a length of text to multiple of 64 bits
        public string setTextMutipleOf64Bits(string text)
        {
            if ((text.Length % 64) != 0)
            {
                int maxLength = 0;
                maxLength = ((text.Length / 64) + 1) * 64;
                text = text.PadRight(maxLength, '0');
            }

            return text;
        }
        #endregion

        #region Transform an integer to binary number
        public string FromTextToBinary(string text)
        {
            StringBuilder binarystring = new StringBuilder(text.Length * 8);

            foreach (char word in text)
            {
                int binary = (int)word;
                int factor = 128;

                for (int i = 0; i < 8; i++)
                {
                    if (binary >= factor)
                    {
                        binary -= factor;
                        binarystring.Append("1");
                    }
                    else
                    {
                        binarystring.Append("0");
                    }
                    factor /= 2;
                }
            }

            return binarystring.ToString();
        }

        public static string FromDeciamlToBinary(int binary)
        {
            if (binary < 0)
            {
                Console.WriteLine("It requires a integer greater than 0.");
                return null;
            }

            string binarystring = "";
            int factor = 128;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -