Beispiel #1
0
        public bool Encrypt()
        {
            long totLength = reader.BaseStream.Length, currentLength = 0;

            // 先输出加密后的IV,以供解密
            writer.Write(DES.Encrypt(IV, key).ToByteArray());
            lastAnswer = IV;
            long roundCount = 0;

            while (EncryptBlock() == 8)
            {
                currentLength += 8;
                roundCount++;
                if (roundCount % 100 == 0)
                {
                    Program.Form.progressBar.Value = (int)(currentLength * 100.0 / totLength);
                    Application.DoEvents();
                }
            }
            Program.Form.progressBar.Value = 100;
            return(true);
        }
Beispiel #2
0
 private void LoadKeyButton_Click(object sender, EventArgs e)
 {
     if (busy)
     {
         return;
     }
     if (keyFileDialog.ShowDialog() == DialogResult.OK)
     {
         Bitset newKey = LoadKey(keyFileDialog.FileName);
         if (newKey != null)
         {
             if (newKey.ToBigIntegerValue() == 0)
             {
                 MessageBox.Show("0 不是一个合法的密钥。");
                 return;
             }
             Key                  = newKey;
             textBoxKey.Text      = Key.ToString();
             textBoxRSAInput.Text = Key.ToBigIntegerValue().ToString();
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// 轮函数的主处理函数
        /// </summary>
        /// <param name="input">输入的64比特(包括Li-1与Ri-1)</param>
        /// <param name="key">输入的子密钥</param>
        /// <returns>输出的64比特(包括Li与Ri)</returns>
        public Bitset Process(Bitset input, Bitset key)
        {
            //判断输入是否合法
            if (input.Length != 64 || key.Length != 48)
            {
                throw new ArgumentException("输入不合法");
            }

            // 拆成两个32比特,Ri不动,Li进行处理
            Bitset left  = input.LeftHalf();
            Bitset right = input.RightHalf();

            // 计算轮函数f值,先R进行E矩阵置换,从32比特变为48比特:
            Bitset f = EMatrix.Perform(right);

            // 然后将f值与密钥相异或
            f = f.Xor(key);

            // 将值拆成8个6bit串,输入S盒
            Bitset[] groups = f.SplitByLength(6);
            for (int i = 0; i < 8; ++i)
            {
                // 进行S盒变换
                groups[i] = S[i].Perform(groups[i]);
            }
            // 将f和成一个01串
            f = Bitset.Join(groups);

            // 再经过P盒坐标置换
            f = PBox.Perform(f);

            // 与L_(i-1)异或
            f = f.Xor(left);

            //输出结果,注意没有经过左右交换SW
            return(Bitset.Join(f, right));
        }
Beispiel #4
0
        public int EncryptBlock()
        {
            byte[] readByte = reader.ReadBytes(8);
            byte[] newByte;
            if (readByte.Length != 8)
            {
                newByte = new byte[8];
                for (int i = 0; i < readByte.Length; ++i)
                {
                    newByte[i] = readByte[i];
                }
                newByte[7] = (byte)((7 - readByte.Length) * 8);
            }
            else
            {
                newByte = readByte;
            }
            Bitset processData = new Bitset(newByte);

            lastAnswer = DES.Encrypt(processData.Xor(lastAnswer), key);
            byte[] writeByte = lastAnswer.ToByteArray();
            writer.Write(writeByte);
            return(readByte.Length);
        }
Beispiel #5
0
 private void RandomIVButton_Click(object sender, EventArgs e)
 {
     Log("生成随机初始向量:IV(64 bit)");
     IV          = Bitset.RandomGenerate(64);
     IVText.Text = IV.ToString();
 }
Beispiel #6
0
 /// <summary>
 /// 构造新的DES流
 /// </summary>
 /// <param name="inputReader">待加密/解密输入流</param>
 /// <param name="inputKey">密钥</param>
 /// <param name="inputWriter">结果输出流</param>
 /// <param name="inputIV">初始向量,解密时不需要提供</param>
 public DESStream(BinaryReader inputReader, Bitset inputKey, BinaryWriter inputWriter, Bitset inputIV = null)
 {
     reader = inputReader;
     writer = inputWriter;
     key    = inputKey;
     DES    = new DESAlgorithm();
     if (inputIV != null)
     {
         IV = inputIV;
     }
     else
     {
         IV = new Bitset(64);
     }
 }
Beispiel #7
0
 /// <summary>
 /// 解密方法
 /// </summary>
 /// <param name="data">密文</param>
 /// <param name="key">密钥</param>
 /// <returns>明文</returns>
 public Bitset Decrypt(Bitset data, Bitset key)
 {
     return(Process(data, key, false));
 }
Beispiel #8
0
 /// <summary>
 /// 加密方法
 /// </summary>
 /// <param name="data">明文</param>
 /// <param name="key">密钥</param>
 /// <returns>密文</returns>
 public Bitset Encrypt(Bitset data, Bitset key)
 {
     return(Process(data, key, true));
 }
Beispiel #9
0
 /// <summary>
 /// 复制构造函数
 /// </summary>
 /// <param name="rhs">待复制OldBitset</param>
 public Bitset(Bitset rhs)
 {
     data = rhs.data.Clone() as bool[];
 }