static string decrypt(String encryptedString, Playfair cipher) { encryptedString = encryptedString.ToLower(); string decryptedString = null; List <Plain> listPlains = new List <Plain>(); for (int c = 1; c < encryptedString.Length; c++) { if (c % 2 == 1) { Plain plain = new Plain(); plain.setval1(encryptedString[c - 1]); plain.setval2(encryptedString[c]); listPlains.Add(plain); } } List <Plain> listDecrypted = new List <Plain>(); foreach (Plain p in listPlains) { p.findCoordinates(cipher); if (p.getx1() == p.getx2()) //ten sam wiersz { if (p.gety1() == 0) { p.sety2(p.gety2() - 1); p.sety1(4); } else if (p.gety2() == 0) { p.sety2(4); p.sety1(p.gety2() - 1); } else { p.sety1(p.gety1() - 1); p.sety2(p.gety2() - 1); } } else if (p.gety1() == p.gety2()) //ta sama kolumna { if (p.getx2() == 0) { p.setx2(4); p.setx1(p.getx1() - 1); } else if (p.getx1() == 0) { p.setx2(p.getx2() - 1); p.setx1(4); } else { p.setx1(p.getx1() - 1); p.setx2(p.getx2() - 1); } } else ///reszta { int buf = p.gety1(); p.sety1(p.gety2()); p.sety2(buf); } p.findCipherLetters(cipher); listDecrypted.Add(p); } foreach (Plain p in listDecrypted) { decryptedString = decryptedString + string.Join("", p.getval1().ToString()) + string.Join("", p.getval2().ToString()); } for (int c = 1; c < decryptedString.Length - 1; c++) { if (c > 0 && c < decryptedString.Length) { if (decryptedString[c] == 'x' && decryptedString[c - 1] == decryptedString[c + 1]) { decryptedString = decryptedString.Replace("x", ""); } } } return(decryptedString); }
static string encrypt(String text, Playfair cipher) { text = text.ToLower(); ArrayList list = new ArrayList(); if (text[0] == 'j') { list.Add('i'); } else { list.Add(text[0]); } for (int c = 1; c < text.Length; c++) { if (text[c] == ' ' || text[c] == ',' || text[c] == '.' || text[c] == '?' || text[c] == '!' || text[c] == ':' || text[c] == ';' || text[c] == '*' || text[c] == '(' || text[c] == ')' || text[c] == '"' || text[c] == '\n') { } else if (text[c] == 'j') { list.Add('i'); } else { list.Add(text[c]); } } for (int c = 1; c < list.Count; c++) { if (list[c - 1].ToString() == list[c].ToString()) { list.Insert(c, 'x'); } } if ((list.Count - 1) % 2 == 0) { list.Add('x'); } String plaintext = string.Join("", list.ToArray()); // Console.WriteLine("Plaintext: " + plaintext); List <Plain> listPlains = new List <Plain>(); for (int c = 1; c < plaintext.Length; c++) { if (c % 2 == 1) { Plain plain = new Plain(); plain.setval1(plaintext[c - 1]); plain.setval2(plaintext[c]); listPlains.Add(plain); } } List <Plain> listEncrypted = new List <Plain>(); foreach (Plain p in listPlains) { p.findCoordinates(cipher); if (p.getx1() == p.getx2()) //ten sam wiersz { if (p.gety1() == 4) { p.sety2(p.gety2() + 1); p.sety1(0); } else if (p.gety2() == 4) { p.sety2(0); p.sety1(p.gety2() + 1); } else { p.sety1(p.gety1() + 1); p.sety2(p.gety2() + 1); } } else if (p.gety1() == p.gety2()) //ta sama kolumna { if (p.getx2() == 4) { p.setx2(0); p.setx1(p.getx1() + 1); } else if (p.getx1() == 4) { p.setx2(p.getx2() + 1); p.setx1(0); } else { p.setx1(p.getx1() + 1); p.setx2(p.getx2() + 1); } } else ///reszta { int buf = p.gety1(); p.sety1(p.gety2()); p.sety2(buf); } p.findCipherLetters(cipher); listEncrypted.Add(p); } String encryptedString = null; foreach (Plain p in listEncrypted) { encryptedString = encryptedString + string.Join("", p.getval1().ToString()) + string.Join("", p.getval2().ToString()); } return(encryptedString); }