public override string encrypt(string text, string key) { key = this.prepareKey(key); List <string> nonAlpha = new List <string>(); string pureText = StringOperations.GetPureText(text, ref nonAlpha); int keyLength = key.Length; int columns = key.Length; int rows = (int)Math.Ceiling((double)pureText.Length / (double)columns); char[,] textMatrix = new char[rows, columns]; int charsIndex = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { textMatrix[i, j] = pureText[charsIndex++]; if (charsIndex == pureText.Length) { break; } } if (charsIndex == pureText.Length) { break; } } string encryptedText = ""; for (int columnIndex = 1; columnIndex <= columns; columnIndex++) { int targetIndex = 0; for (int i = 0; i < keyLength; i++) { if (key[i] == columnIndex.ToString()[0]) { targetIndex = i; break; } } for (int row = 0; row < rows; row++) { encryptedText += textMatrix[row, targetIndex]; } } return(StringOperations.GetFullText(encryptedText, nonAlpha).ToUpper()); }
public override string decrypt(string text, string key) { this.prepareKey(key); List <string> nonAlpha = new List <string>(); string pureText = StringOperations.GetPureText(text, ref nonAlpha).ToUpper(); int index = 0; int pureTextLength = pureText.Length; List <string> alphaPairs = new List <string>(); for (int i = 0; i < pureTextLength; i++) { string temp = pureText[i].ToString(); if (i + 1 != pureTextLength && pureText[i] == pureText[i + 1]) { temp += DUMMY; } else if (i + 1 != pureTextLength) { temp += pureText[i + 1].ToString(); i++; } alphaPairs.Add(temp); index++; } if (alphaPairs[alphaPairs.Count - 1].Length == 1) { alphaPairs[alphaPairs.Count - 1] += DUMMY; } List <string> alphaDecryptedPairs = new List <string>(); int x1 = 0; int x2 = 0; int y1 = 0; int y2 = 0; for (int i = 0; i < alphaPairs.Count; i++) { string currentPair = alphaPairs[i]; this.getCharsPosition(currentPair[0], currentPair[1], ref x1, ref y1, ref x2, ref y2); alphaDecryptedPairs.Add(this.getNewChars(x1, y1, x2, y2, false)); } string finalDecryption = string.Concat(alphaDecryptedPairs.ToArray()); return(StringOperations.GetFullText(finalDecryption, nonAlpha).ToUpper()); }
public override string encrypt(string text, string key) { List <string> nonAlpha = new List <string>(); string pureText = StringOperations.GetPureText(text, ref nonAlpha); int textActualLength = pureText.Length; int nonAlphaLength = nonAlpha.Count; int keyActualLength = key.Length; if (keyActualLength < textActualLength) { for (int i = 0; i < textActualLength - keyActualLength; i++) { if (Char.IsLetter(pureText[i])) { key += pureText[i]; } } } /* ... Append the text characters to the key * ex: plain text = MEETATTHEFOUNTAIN * key = KILT * new key = KILTMEETATTHEFOUN */ // Holds decrypted chars string encryptedPureText = ""; for (int i = 0; i < textActualLength; i++) { int textCharIndex = Math.Abs(pureText[i] - 'A'); int keyCharIndex = Math.Abs(key[i] - 'A'); encryptedPureText += this.tabulaRecta[textCharIndex, keyCharIndex]; } // ... Get the decrypted chars from the table // Holds the decrypted chars + non alpha chars char[] encryptedText = new char[text.Length]; for (int i = 0; i < nonAlphaLength; i++) { int currentNonAlphaIndex = int.Parse(nonAlpha[i].Substring(1, nonAlpha[i].Length - 1)); encryptedText[currentNonAlphaIndex] = nonAlpha[i][0]; } // ... Set non alpha chars in the array in their original indices return(StringOperations.GetFullText(encryptedPureText, nonAlpha).ToUpper()); }
public override string decrypt(string text, string key) { // Holds the text alphapet characters [a-zA-Z] List <string> nonAlpha = new List <string>(); string pureText = StringOperations.GetPureText(text, ref nonAlpha); int textActualLength = pureText.Length; int nonAlphaLength = nonAlpha.Count; int keyActualLength = key.Length; if (textActualLength != key.Length) { Console.WriteLine("Text and Key should be the same length!"); return(string.Empty); } string decryptedPureText = ""; for (int i = 0; i < textActualLength; i++) { char alpha = 'A'; for (int j = 0; j < 26; j++) { int keyCharacterIndex = Math.Abs(key[i] - 'A'); if (pureText[i] == this.tabulaRecta[j, keyCharacterIndex]) { break; } alpha++; } /* ... We have the key [Column] * Iterate through the decrypted, then find the row, if the char matched an index in the table * get the char */ decryptedPureText += alpha; } return(StringOperations.GetFullText(decryptedPureText, nonAlpha).ToUpper()); }
public override string decrypt(string text, int key) { List <string> nonAlpha = new List <string>(); string pureText = StringOperations.GetPureText(text, ref nonAlpha); int rows = key; int columns = (int)Math.Ceiling((double)pureText.Length / (double)key); char[,] textMatrix = new char[rows, columns]; int charsIndex = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { textMatrix[i, j] = pureText[charsIndex++]; if (charsIndex == pureText.Length) { break; } } if (charsIndex == pureText.Length) { break; } } string decryptedText = ""; for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { if (textMatrix[j, i] != '\0') { decryptedText += textMatrix[j, i]; } } } return(StringOperations.GetFullText(decryptedText, nonAlpha).ToUpper()); }