コード例 #1
0
ファイル: ProcessAES.cs プロジェクト: SiTox/DemoAES
        public string EncryptionStart(string PlainText, string CipherKey, bool IsTextBinary)
        {
            StringBuilder binaryText = null;

            if (IsTextBinary == false)
            {
                PlainText = BaseTransform.FromTextToBinary(PlainText);
            }
            else
            {
                //binaryText = PlainText;
            }
            binaryText = new StringBuilder(BaseTransform.setTextMutipleOf128Bits(PlainText));

            StringBuilder EncryptedTextBuilder = new StringBuilder(binaryText.Length);

            #region Make All-round keys

            Matrix Matrix_CipherKey = new Matrix(BaseTransform.FromHexToBinary(CipherKey));
            Keys key = new Keys();
            key.setCipherKey(Matrix_CipherKey);
            key = this.KeyExpansion(key, false);

            #endregion
            
            #region Main Transform
            for (int j = 0; j < (binaryText.Length / 128); j++)
            {          
                state = new Matrix(binaryText.ToString().Substring(j * 128, 128));

                state = this.AddRoundKey(state, key, 0);

                for (int i = 1; i < 11; i++)
                {
                    if (i == 10)
                    {
                        state = this.SubBytes(state, false);
                        state = this.ShiftRows(state, false);
                        state = this.AddRoundKey(state, key, i);
                    }
                    else
                    {
                        state = this.SubBytes(state, false);
                        state = this.ShiftRows(state, false);
                        state = this.MixColumns(state, false);
                        state = this.AddRoundKey(state, key, i);
                    }
                }
#endregion

                EncryptedTextBuilder.Append(state.ToString());
            }
            OnEndState(new EndStateArgs(true));
            return EncryptedTextBuilder.ToString();
            
        }
コード例 #2
0
ファイル: ProcessAES.cs プロジェクト: SiTox/DemoAES
        public Matrix AddRoundKey(Matrix state, Keys key, int Round)
        {
            if (Round > key.RoundKeys.Count - 1)
            {
                throw new IndexOutOfRangeException("The round key is must between 0 and 10 in 128bit AES.");
            }

            return MatrixMultiplication.XOR(state, key.RoundKeys[Round]);
        }
コード例 #3
0
ファイル: ProcessAES.cs プロジェクト: SiTox/DemoAES
        public Keys KeyExpansion(Keys key, bool IsReverse)
        {
            for (int i = 4; i < key.RoundKeys.Count * 4; i++)
            {
                string[] Wi_1 = key.RoundKeys[(i - 1) / 4].getWord((i - 1) % 4);

                Matrix mat_Wi_1 = new Matrix(4, 1);
                mat_Wi_1.setWord(Wi_1, 0);

                if (i % 4 == 0)
                {
                    Wi_1 = this.RotWord(Wi_1);
                    mat_Wi_1.setWord(Wi_1, 0);
                    mat_Wi_1 = this.SubBytes(mat_Wi_1, false);
                    mat_Wi_1 = MatrixMultiplication.XOR(mat_Wi_1, TransformTables.Rcon[(i - 1) / 4]);
                }

                Matrix Wi_4 = new Matrix(4, 1);
                Wi_4.setWord(key.RoundKeys[(i - 4) / 4].getWord((i - 4) % 4), 0);

                Matrix temp = MatrixMultiplication.XOR(mat_Wi_1, Wi_4);

                string[] Wi = temp.getWord(0);

                key.RoundKeys[i / 4].setWord(Wi, i % 4);
            }

            return key;
        }
コード例 #4
0
ファイル: ProcessAES.cs プロジェクト: SiTox/DemoAES
        public string DecryptionStart(string PlainText, string CipherKey, bool IsTextBinary)
        {
            string binaryText = "";

            if (IsTextBinary == false)
            {
                binaryText = BaseTransform.FromTextToBinary(PlainText);
            }
            else
            {
                binaryText = PlainText;
            }

            StringBuilder DecryptedTextBuilder = new StringBuilder(binaryText.Length);
            
            #region Make All-round keys

            Matrix Matrix_CipherKey = new Matrix(BaseTransform.FromHexToBinary(CipherKey));
            Keys key = new Keys();
            key.setCipherKey(Matrix_CipherKey);
            key = this.KeyExpansion(key, false);

            #endregion


            #region Main Transforms
            for (int j = 0; j < (binaryText.Length / 128); j++)
            {                
                state = new Matrix(binaryText.Substring(j * 128, 128));

                state = this.AddRoundKey(state, key, 10);

                for (int i = 9; i >= 0; i--)
                {
                    if (i == 0)
                    {
                        state = this.ShiftRows(state, true);
                        state = this.SubBytes(state, true);
                        state = this.AddRoundKey(state, key, i); 
                    }
                    else
                    {
                        state = this.ShiftRows(state, true);
                        state = this.SubBytes(state, true);
                        state = this.AddRoundKey(state, key, i);
                        state = this.MixColumns(state, true);
                    }
                }
                
                #region It's for correct subtracted '0' that have added for set text multiple of 128bit
                if ((j * 128 + 128) == binaryText.Length)
                {
                    StringBuilder last_text = new StringBuilder(state.ToString().TrimEnd('0'));

                    int count = state.ToString().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(state.ToString());
                }
            }     
            #endregion

            OnEndState(new EndStateArgs(true)); //End of Thread
            return DecryptedTextBuilder.ToString();
        }