Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1.   private static string Cipher(string input, string key, bool encipher)
  2.         {
  3.             for (int i = 0; i < key.Length; ++i)
  4.                 if (!char.IsLetter(key[i]))
  5.                     return null; // Error
  6.  
  7.             string output = string.Empty;
  8.             int nonAlphaCharCount = 0;
  9.  
  10.             for (int i = 0; i < input.Length; ++i)
  11.             {
  12.                 if (char.IsLetter(input[i]))
  13.                 {
  14.                     bool cIsUpper = char.IsUpper(input[i]);
  15.                     char offset = cIsUpper ? 'A' : 'a';
  16.                     int keyIndex = (i - nonAlphaCharCount) % key.Length;
  17.                     int k = (cIsUpper ? char.ToUpper(key[keyIndex]) : char.ToLower(key[keyIndex])) - offset;
  18.                     k = encipher ? k : -k;
  19.                     char ch = (char)((Mod(((input[i] + k) - offset), 26)) + offset);
  20.                     output += ch;
  21.                 }
  22.                 else
  23.                 {
  24.                     output += input[i];
  25.                     ++nonAlphaCharCount;
  26.                 }
  27.             }
  28.  
  29.             Console.WriteLine(output);
  30.             return output;
  31.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement