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()); }
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()); }