/* * Rot13.cs * ROT13 Text Transformation in Managed Code (C#) * * This class/method simply does a Rot13 (rotate 13) transformation on the argument string. * This is useful for obfuscation. If something isn't important enough to encrypt, but * you don't want someone to walk by and read it, use this. It hasn't shown any errors so * far. The general algorithm/structure was copied from another source, but rewritten completely, * and modified enough so that it doesn't infringe any copyrights. * * Original code by Samuel Allen. Copyright 2008. * Dot Net Perls, http://dotnetperls.com/ * * */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; public static class Rot13 { /* public static string Rotate(string toRotate) { StringBuilder sb = new StringBuilder(toRotate.Length); for (int i = 0; i < toRotate.Length; i++) { char c = toRotate[i]; int ci = (int)c; if (ci >= 65 && ci < 91) { ci += 13; if (ci >= 91) { ci -= 26; } } else if (ci >= 97 && ci < 123) { ci += 13; if (ci >= 123) { ci -= 26; } } sb.Append((char)ci); } return sb.ToString(); }*/ public static string Rotate2(string toRotate) { char[] charArray = toRotate.ToCharArray(); for (int i = 0; i < charArray.Length; i++) { char c = charArray[i]; int ci = (int)c; if (ci >= 65 && ci < 91) { ci += 13; if (ci >= 91) { ci -= 26; } } else if (ci >= 97 && ci < 123) { ci += 13; if (ci >= 123) { ci -= 26; } } charArray[i] = (char)ci; } return new string(charArray); } /* public static void Test() { string testString1 = "There was once a fox who had a pretty fur coat, and they called him fire."; string testString2 = "This is a code string. What do you think about that?"; string testString3 = "Sam"; long t1 = Environment.TickCount; for (int i = 0; i < 100000; i++) { string s1 = Rot13.Rotate(testString1); string s2 = Rot13.Rotate(testString2); string s3 = Rot13.Rotate(testString3); if (s1 == s2 || s2 == s3) { break; } } long t2 = Environment.TickCount; Console.WriteLine((t2 - t1) + " ms for Rotate"); for (int i = 0; i < 10000; i++) { string s1 = Rot13.Rotate2(testString1); string s2 = Rot13.Rotate2(testString2); string s3 = Rot13.Rotate2(testString3); if (s1 == s2 || s2 == s3) { break; } } long t3 = Environment.TickCount; Console.WriteLine((t3 - t2) + " ms for Rotate2"); } * */ }