public string Encrypt(string plainText, string key) { // construct the arr[5][5] based on PlayFair algorithm. construct_array(key); char current, next = ' '; // this is the String that will contains the encrpted text. String ret = ""; for (int k = 0; k < plainText.Length; k += 2) { current = plainText[k]; if (k < plainText.Length - 1) { // if there's 2 same adjacent characters seperate them by adding char 'x' in the middle. if (plainText[k + 1] == plainText[k]) { next = 'x'; k--; } else { next = plainText[k + 1]; } } // if plainText.Length is odd so the last character with paired with char 'x' if (k + 1 == plainText.Length) { next = 'x'; k--; } // the next 2 character positions in arr[5][5] point x1 = find(current); point x2 = find(next); // here's the 3 conditions of PlayFair algorithm if (x1.getJ() == x2.getJ()) { ret += arr[(x1.getI() + 1) % 5, x1.getJ() % 5]; ret += arr[(x2.getI() + 1) % 5, x2.getJ() % 5]; } else if (x1.getI() == x2.getI()) { ret += arr[(x1.getI()), (x1.getJ() + 1) % 5]; ret += arr[x2.getI(), (x2.getJ() + 1) % 5]; } else { ret += arr[x1.getI(), x2.getJ()]; ret += arr[x2.getI(), x1.getJ()]; } } return(ret.ToUpper()); }
public string Decrypt(string cipherText, string key) { construct_array(key); char current, next = ' '; String ret = ""; for (int k = 0; k < cipherText.Length; k += 2) { current = Char.ToLower(cipherText[k]); if (k < cipherText.Length - 1) { if (cipherText[k + 1] == cipherText[k]) { next = 'x'; k--; } else { next = cipherText[k + 1]; } } if (k + 1 == cipherText.Length) { next = 'x'; k--; } point x1 = find(Char.ToLower(current)); point x2 = find(Char.ToLower(next)); if (x1.getJ() == x2.getJ()) { if (x1.getI() == 0) { ret += arr[4, x1.getJ()]; } else { ret += arr[x1.getI() - 1, x1.getJ()]; } if (x2.getI() == 0) { ret += arr[4, x2.getJ()]; } else { ret += arr[(x2.getI() - 1), x2.getJ()]; } } else if (x1.getI() == x2.getI()) { if (x1.getJ() == 0) { ret += arr[x1.getI(), 4]; } else { if (arr[(x1.getI()), (x1.getJ() - 1)] != 'x') { ret += arr[(x1.getI()), (x1.getJ() - 1)]; } } if (x2.getJ() == 0) { ret += arr[x2.getI(), 4]; } else { ret += arr[x2.getI(), (x2.getJ() - 1)]; } } else { if (arr[x2.getI(), x1.getJ()] == 'x') { ret += arr[x1.getI(), x2.getJ()]; // ret += 'x'; continue; } // if (arr[x1.getI(), x2.getJ()] != 'x') ret += arr[x1.getI(), x2.getJ()]; if (arr[x2.getI(), x1.getJ()] != 'x') { ret += arr[x2.getI(), x1.getJ()]; } } } return(ret.ToUpper()); }