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