Пример #1
0
        public override string Encrypt(string plainText, string key)
        {
            plainText = plainText.Replace("0x", "");
            key       = key.Replace("0x", "");
            //if (plainText.Length != 32 || key.Length != 32)
            //    return "Only 128-bit keys.";

            BMatrix keyMat = new BMatrix(4, 4, key);

            BuildKeySchedule(keyMat, false);

            BMatrix PTMat = new BMatrix(4, 4, plainText);

            //Add Round Key
            PTMat.AddRoundKey(keyMat);
            for (int i = 0; i < keyScheduleSize - 2; i++)
            {
                PTMat.SubBytes();
                PTMat.ShiftRows();
                PTMat.MixCols();
                PTMat.AddRoundKey(mKeySchedule[i + 1]);
            }
            PTMat.SubBytes();
            PTMat.ShiftRows();
            PTMat.AddRoundKey(mKeySchedule[keyScheduleSize - 1]);


            return("0x" + PTMat.ToPlainTxt());
        }
Пример #2
0
        public override string Decrypt(string cipherText, string key)
        {
            //https://stackoverflow.com/questions/3436822/aes-decryption-algorithm

            cipherText = cipherText.Replace("0x", "");
            key        = key.Replace("0x", "");
            if (cipherText.Length != 32 || key.Length != 32)
            {
                return("Only 128-bit keys.");
            }

            BMatrix keyMat = new BMatrix(4, 4, key);

            BuildKeySchedule(keyMat, false);

            BMatrix CTMat = new BMatrix(4, 4, cipherText);

            CTMat.AddRoundKey(mKeySchedule[keyScheduleSize - 1]);
            CTMat.InvShiftRows();
            CTMat.InvSubBytes();


            for (int i = keyScheduleSize - 2; i >= 1; i--)
            {
                CTMat.AddRoundKey(mKeySchedule[i]);
                CTMat.InvMixCols();
                CTMat.InvShiftRows();
                CTMat.InvSubBytes();
            }


            CTMat.AddRoundKey(mKeySchedule[0]);
            return("0x" + CTMat.ToPlainTxt());
        }
Пример #3
0
        public IActionResult Calculation()
        {
            var graph = new OrientedGraph(new RelationsAdapter(Request.Query).Adapt());

            var adjMatrix = graph.ToMatrix();

            var graphInfo         = new GraphInfo(graph);
            var taktsOfCreation   = graphInfo.TactsOfCreation();
            var taktsOfExtinction = graphInfo.TactsOfExtinction();
            var taktsOfStore      = graphInfo.TactsOfStore();
            var inputs            = graphInfo.Inputs;
            var outputs           = graphInfo.Outputs;
            var power             = graphInfo.Power;

            var powers  = graphInfo.MatricesToZero;
            var bMatrix = new BMatrix(adjMatrix);

            var matrixAdapter    = new MatrixAdapter();
            var graphInfoAdapter = new GraphInfoAdapter();

            return(Json(new
            {
                aMatrices = matrixAdapter.AdaptAdjacencyMatrixWithPowers(powers),
                graphInfo = graphInfoAdapter.AdaptGraphInfo(power, inputs, outputs),
                tactsTable = graphInfoAdapter.AdaptTacts(taktsOfCreation, taktsOfExtinction, taktsOfStore),
                bMatrix = matrixAdapter.AdaptBMatrix(bMatrix)
            }));
        }
Пример #4
0
        void BuildKeySchedule(BMatrix SeedKey, bool printKeys)
        {
            mKeySchedule    = new BMatrix[keyScheduleSize];
            mKeySchedule[0] = new BMatrix(SeedKey);
            //if (printKeys)
            //Console.WriteLine("cipher Key\n" + mKeySchedule[0].ToString());

            for (int i = 1; i < keyScheduleSize; i++)
            {
                mKeySchedule[i] = mKeySchedule[i - 1].GetNextKey(i - 1);
                //if (printKeys)
                //    Console.WriteLine("Round Key " + i + "\n" + mKeySchedule[i].ToString());
            }
        }
Пример #5
0
            public void AddRoundKey(BMatrix key)
            {
                //checked
                //if (rows != key.rows || cols != key.rows)
                //    return;

                for (int i = 0; i < rows; i++)
                {
                    for (int k = 0; k < cols; k++)
                    {
                        mat[i][k] = (byte)(mat[i][k] ^ key.mat[i][k]);
                    }
                }
            }
Пример #6
0
        public void BMatrixTest()
        {
            var bMatrix = new BMatrix(_graph.ToMatrix()).ToArray();

            var res = new[]
            {
                new[] { 0, 1, 1, 0, 0, 0, 1, 0 },
                new[] { 0, 0, 1, 0, 0, 0, 1, 0 },
                new[] { 0, 0, 0, 0, 0, 0, 0, 0 },
                new[] { 1, 2, 2, 0, 0, 0, 2, 0 },
                new[] { 0, 1, 1, 0, 0, 0, 1, 0 },
                new[] { 1, 2, 2, 1, 0, 0, 2, 0 },
                new[] { 0, 0, 0, 0, 0, 0, 0, 0 },
                new[] { 0, 1, 1, 0, 1, 0, 1, 0 }
            };

            Assert.AreEqual(res, bMatrix);
        }
Пример #7
0
            public BMatrix GetNextKey(int RconIndex)
            {
                BMatrix nextKey = new BMatrix(rows, cols);

                for (int i = 0; i < cols; i++)
                {
                    if (i == 0)
                    {
                        nextKey.SetCol(0, ShiftColUp(3, 1, false));
                        nextKey.SetCol(0, SubBytes(nextKey.GetCol(0)));
                        nextKey.SetCol(0, XOR(nextKey.GetCol(0), new byte[] { Rcon[RconIndex], 0, 0, 0 }));
                        nextKey.SetCol(0, XOR(nextKey.GetCol(0), GetCol(0)));
                    }
                    else
                    {
                        nextKey.SetCol(i, XOR(nextKey.GetCol(i - 1), GetCol(i)));
                    }
                }
                return(nextKey);
            }
Пример #8
0
            /// ///////////////////////////////////////////////////////////////////////////////////////////////////


            public BMatrix(BMatrix bmat)
            {
                this.rows = bmat.rows;
                this.cols = bmat.cols;
                mat       = bmat.mat;
            }