Example #1
0
        public static void WriteBinaryToFile(string filename, string binaryText)
        {
            // Write binary encrypted or decrypted data to file.
            FileStream    fs       = new FileStream(filename, FileMode.Create);
            StringBuilder sub_text = new StringBuilder(800);

            byte[] bytes  = new byte[100];
            int    length = 800;

            for (int i = 0; i <= binaryText.Length / 800; i++)
            {
                int remain = binaryText.Length - i * 800;
                if (remain < 800)
                {
                    length = remain;
                }
                sub_text.Remove(0, sub_text.Length);
                sub_text.Append(binaryText.Substring(i * 800, length));
                for (int j = 0; j < sub_text.Length / 8; j++)
                {
                    bytes[j] = BaseTransform.FromBinaryToByte(sub_text.ToString().Substring(j * 8, 8));
                    if (remain < 800)
                    {
                        Console.Write(bytes[j].ToString());
                    }
                }
                fs.Write(bytes, 0, sub_text.Length / 8);
            }
            Console.WriteLine();
            fs.Close();
        }
Example #2
0
        public static string FileReadToBinary(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open);

            Console.WriteLine("File size : " + fs.Length);
            // Read from file 100 bytes per 1 time and transform to binary data.
            int           fileLength = (int)fs.Length;
            StringBuilder text       = new StringBuilder((int)fs.Length * 8);

            byte[] bytes      = new byte[100];
            int    startindex = 0;

            // int IsEnd = -1;
            while (fs.Read(bytes, startindex, bytes.Length) != 0)
            {
                // if (IsEnd > 0)
                // {
                // }
                foreach (byte b in bytes)
                {
                    if (text.Length == fileLength * 8)
                    {
                        break;
                    }
                    text.Append(BaseTransform.FromDecimalToBinary(b));
                }
            }
            fs.Close();
            return(text.ToString());
        }
 static TransformTables()
 {
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("01000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("02000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("04000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("08000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("10000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("20000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("40000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("80000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("1b000000"), 4, 1));
     TransformTables.Rcon.Add(new Matrix(BaseTransform.FromHexToBinary("36000000"), 4, 1));
 }
        public override string EncryptionStart(string PlainText, string CipherKey, bool IsTextBinary)
        {
            // Encryption Process
            StringBuilder binaryText = null;

            if (IsTextBinary == false)
            {
                PlainText = BaseTransform.FromTextToBinary(PlainText);
            }
            else
            {
                //binaryText = PlainText;
            }
            binaryText = new StringBuilder(BaseTransform.setTextMultipleOf128Bits(PlainText));
            StringBuilder EncryptedTextBuilder = new StringBuilder(binaryText.Length);
            // 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);
            // Initialize Progress Bar
            OnInitProgress(new ProgressInitArgs(binaryText.Length));
            //Matrix state = new Matrix(4, 4);
            for (int j = 0; j < (binaryText.Length / 128); j++)
            {
                //state.setState(binaryText.ToString().Substring(j * 128, 128));
                Matrix 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);
                    }
                }
                EncryptedTextBuilder.Append(state.ToString());
                // Increase Progress Bar
                OnIncrementProgress(new ProgressEventArgs(state.ToString().Length));
            }
            return(EncryptedTextBuilder.ToString());
        }
Example #5
0
 public string this[int i, int j]
 {
     get
     {
         return(matrix[i, j]);
     }
     set
     {
         //If it gets hexa decimal transform to binary
         if (value.Length == 2)
         {
             matrix[i, j] = BaseTransform.FromHexToBinary(value);
         }
         else if (value.Length == 8)
         {
             matrix[i, j] = value;
         }
     }
 }
        public override string DecryptionStart(string PlainText, string CipherKey, bool IsTextBinary)
        {
            // Decryption Process
            string binaryText = "";

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

            // 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);
            // Initialize Progress Bar
            OnInitProgress(new ProgressInitArgs(binaryText.Length));
            //Matrix state = new Matrix(4, 4);
            for (int j = 0; j < (binaryText.Length / 128); j++)
            {
                //state.setState(binaryText.Substring(j * 128, 128));
                Matrix 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);
                        //DecryptedTextBuilder.Append(state.ToString());
                    }
                }
                // 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);
                }
                else
                {
                    DecryptedTextBuilder.Append(state.ToString());
                }
                // Increase Progress Bar
                OnIncrementProgress(new ProgressEventArgs(state.ToString().Length));
            }
            //return DecryptedTextBuilder.ToString().TrimEnd('0');
            return(DecryptedTextBuilder.ToString());
        }