Tag Archives: ROT-13
ROT-13 cypher in C#
ROT-13 is a letter substitution cypher that replaces a letter with the letter 13 letters after it in the alphabet. ROT-13 is an example of the Caesar cypher with the difference that Caesar cypher allows you to specify the number of letters to shift.
Main method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static string Rot13(string input) { StringBuilder result = new StringBuilder(); Regex regex = new Regex("[A-Za-z]"); foreach (char c in input) { if (regex.IsMatch(c.ToString())) { int charCode = ((c & 223) - 52) % 26 + (c & 32) + 65; result.Append((char)charCode); } else { result.Append(c); } } return result.ToString(); } |
Usage:
1 2 3 4 5 | // Results to syhkolgrf.pbz string cypheredText = Rot13("fluxbytes.com"); // Results to fluxbytes.com string deCypheredText = Rot13(cypheredText); |