Exemple #1
0
        private static byte[] SubBytes(byte[] bytes)
        {
            byte[] result = new byte[bytes.Length];
            for (int x = 0; x < bytes.Length; x++)
            {
                result[x] = SBOX.Convert(bytes[x]);
            }

            byte[] _1 = { result[0], result[1], result[2], result[3] };
            byte[] _2 = { result[4], result[5], result[6], result[7] };
            byte[] _3 = { result[8], result[9], result[10], result[11] };
            byte[] _4 = { result[12], result[13], result[14], result[15] };
            Logger.WriteLine("====SubBytes====");
            Logger.WriteLine(BitConverter.ToString(_1).Replace("-", " "));
            Logger.WriteLine(BitConverter.ToString(_2).Replace("-", " "));
            Logger.WriteLine(BitConverter.ToString(_3).Replace("-", " "));
            Logger.WriteLine(BitConverter.ToString(_4).Replace("-", " "));
            Logger.WriteLine("================");
            return(result);
        }
Exemple #2
0
        private static void KeySchedule(byte[] bytes, int currentRound)
        {
            byte[,] bytes2d   = Bytes16To2DBytes4(bytes);
            byte[,] round_key = new byte[4, 4];
            byte[] r  = new byte[16];
            byte[] r_ = new byte[16];

            //Rot Word
            //一旦値を格納
            r[3]  = CURRENT_CHIPHER_KEY[3];  //09
            r[7]  = CURRENT_CHIPHER_KEY[7];  //cf
            r[11] = CURRENT_CHIPHER_KEY[11]; //4f
            r[15] = CURRENT_CHIPHER_KEY[15]; //3c
            //Logger.WriteLine("一番左の行(S-BOX変換前): " + BitConverter.ToString(r), Color.CadetBlue);
            //↓ 一番下を一番上に持ってくる
            r_[3]  = CURRENT_CHIPHER_KEY[7];  //cf
            r_[7]  = CURRENT_CHIPHER_KEY[11]; //4f
            r_[11] = CURRENT_CHIPHER_KEY[15]; //3c
            r_[15] = CURRENT_CHIPHER_KEY[3];  //09
            //↓ s-box変換
            r_[3]  = SBOX.Convert(r_[3]);     //8a
            r_[7]  = SBOX.Convert(r_[7]);     //84
            r_[11] = SBOX.Convert(r_[11]);    //eb
            r_[15] = SBOX.Convert(r_[15]);    //01

            //Logger.WriteLine("一番左の行(S-BOX変換後): " + BitConverter.ToString(r_), Color.Orange);

            r[0]  = CURRENT_CHIPHER_KEY[0];
            r[4]  = CURRENT_CHIPHER_KEY[4];
            r[8]  = CURRENT_CHIPHER_KEY[8];
            r[12] = CURRENT_CHIPHER_KEY[12];

            round_key[0, 0] = (byte)(CURRENT_CHIPHER_KEY[0] ^ r_[3] ^ RCON[currentRound]);
            round_key[1, 0] = (byte)(CURRENT_CHIPHER_KEY[4] ^ r_[7] ^ RCON[0]);
            round_key[2, 0] = (byte)(CURRENT_CHIPHER_KEY[8] ^ r_[11] ^ RCON[0]);
            round_key[3, 0] = (byte)(CURRENT_CHIPHER_KEY[12] ^ r_[15] ^ RCON[0]);

            byte[] aaa = new byte[4];
            aaa[0] = (byte)int.Parse(round_key[0, 0].ToString());
            aaa[1] = (byte)int.Parse(round_key[1, 0].ToString());
            aaa[2] = (byte)int.Parse(round_key[2, 0].ToString());
            aaa[3] = (byte)int.Parse(round_key[3, 0].ToString());
            //Logger.WriteLine(BitConverter.ToString(aaa), Color.CadetBlue);

            for (int x = 0; x <= 3; x++) //0,1,2 (3)
            {
                //0は除きたい(横インデックス1から埋め込んでいく)ので x は 1 2 3 のみに絞る
                if (x == 0)
                {
                    continue;
                }
                round_key[0, x] = (byte)(CURRENT_CHIPHER_KEY[0 + (1 * x)] ^ round_key[0, x - 1]);
                round_key[1, x] = (byte)(CURRENT_CHIPHER_KEY[4 + (1 * x)] ^ round_key[1, x - 1]);
                round_key[2, x] = (byte)(CURRENT_CHIPHER_KEY[8 + (1 * x)] ^ round_key[2, x - 1]);
                round_key[3, x] = (byte)(CURRENT_CHIPHER_KEY[12 + (1 * x)] ^ round_key[3, x - 1]);
            }

            CURRENT_CHIPHER_KEY = Bytes2D4ToBytes16(round_key);

            //Logger.WriteLine("ROUND_KEY: " + BitConverter.ToString(Bytes2D4ToBytes16(round_key)), Color.Yellow);
            byte[] _1 = { CURRENT_CHIPHER_KEY[0], CURRENT_CHIPHER_KEY[1], CURRENT_CHIPHER_KEY[2], CURRENT_CHIPHER_KEY[3] };
            byte[] _2 = { CURRENT_CHIPHER_KEY[4], CURRENT_CHIPHER_KEY[5], CURRENT_CHIPHER_KEY[6], CURRENT_CHIPHER_KEY[7] };
            byte[] _3 = { CURRENT_CHIPHER_KEY[8], CURRENT_CHIPHER_KEY[9], CURRENT_CHIPHER_KEY[10], CURRENT_CHIPHER_KEY[11] };
            byte[] _4 = { CURRENT_CHIPHER_KEY[12], CURRENT_CHIPHER_KEY[13], CURRENT_CHIPHER_KEY[14], CURRENT_CHIPHER_KEY[15] };
            //Logger.WriteLine("ROUND_KEY: " + BitConverter.ToString(CURRENT_CHIPHER_KEY), Color.DeepSkyBlue);

            Logger.WriteLine("----ROUND KEY----", Color.Red);
            Logger.WriteLine(BitConverter.ToString(_1).Replace("-", " "), Color.Red);
            Logger.WriteLine(BitConverter.ToString(_2).Replace("-", " "), Color.Red);
            Logger.WriteLine(BitConverter.ToString(_3).Replace("-", " "), Color.Red);
            Logger.WriteLine(BitConverter.ToString(_4).Replace("-", " "), Color.Red);
            Logger.WriteLine("-----------------", Color.Red);
        }