Exemplo n.º 1
0
        /// <summary>
        ///     S盒
        /// </summary>
        /// <param name="input">输入(48位)</param>
        /// <returns>输出(32位)</returns>
        private static BitArray SBox(BitArray input)
        {
            BitArray output = new BitArray(32);
            //8个S盒
            for (int i = 0; i < 8; i++)
            {
                int col = 0;
                int row = 0;
                GenerateRowAndColumnNumber(input, i*6, out row, out col);
                //output.SetRange(i*4, 4, SwitchSBox(i)[row][col]);
                output.SetRange(i*4, 4, SB[i][row][col]);
            }

            return output;
        }
Exemplo n.º 2
0
        /// <summary>
        ///     生成子密钥
        /// </summary>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        private static BitArray[] GenerateKeys(BitArray key)
        {
            //置换选择1 64位->56位
            key = SwapSelect1(key);
            //Debug.WriteLine(key.PrintInBinary());

            BitArray[] output = new BitArray[16];

            BitArray c = new BitArray(28);
            BitArray d = new BitArray(28);
            c.SetRange(0, 28, key);
            d.SetRange(0, 28, key, 28);

            //Debug.WriteLine(c.PrintInBinary());
            //Debug.WriteLine(d.PrintInBinary());

            for (int i = 0; i < 16; i++)
            {
                //密钥产生
                //循环左移
                c = c.ROL(LeftShiftTime[i]);
                d = d.ROL(LeftShiftTime[i]);
                //Debug.WriteLine("i:{2} C:{0} D:{1}", c.PrintInBinary(), d.PrintInBinary(), i+1);
                //置换选择2
                output[i] = SwapSelect2(c.Append(d));
                //Debug.WriteLine(output[i].PrintInBinary());
                //Debug.WriteLine("C:{0} D:{1} out:{2}",c.PrintInHex(),d.PrintInHex(),output[i].PrintInHex());
            }
            return output;
        }