/* * In a loop, it takes the current character of the text and the current character of the key (which loops along the length of the text) * and adds their values in the polybius square. */ public override String Encrypt(String text) { StringBuilder sb = new StringBuilder(); int progress = 0; for (int i = 0; i < text.Length; i++) { char currentEncryptionKeyChar = Key.StringValue[progress % Key.StringValue.Length], currentClearTextChar = text[i]; sb.Append(MyPolybiusSquare.GetValueOf(currentClearTextChar) + MyPolybiusSquare.GetValueOf(currentEncryptionKeyChar)); if (i < text.Length - 1) { sb.Append(" "); } progress++; } return(sb.ToString()); }
public override String Decrypt(String text) { StringBuilder sb = new StringBuilder(), polybiusValues = new StringBuilder(); for (int i = 0; i < text.Length; i++) { polybiusValues.Append(MyPolybiusSquare.GetValueOf(text[i])); } for (int i = 0; i < polybiusValues.Length / 2; i++) { int.TryParse(polybiusValues[i].ToString() + polybiusValues[polybiusValues.Length / 2 + i].ToString(), out int value); sb.Append(MyPolybiusSquare.GetElementOfValue(value)); } return(sb.ToString()); }