Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static string Cipher(string input, string key, bool encipher)
- {
- for (int i = 0; i < key.Length; ++i)
- if (!char.IsLetter(key[i]))
- return null; // Error
- string output = string.Empty;
- int nonAlphaCharCount = 0;
- for (int i = 0; i < input.Length; ++i)
- {
- if (char.IsLetter(input[i]))
- {
- bool cIsUpper = char.IsUpper(input[i]);
- char offset = cIsUpper ? 'A' : 'a';
- int keyIndex = (i - nonAlphaCharCount) % key.Length;
- int k = (cIsUpper ? char.ToUpper(key[keyIndex]) : char.ToLower(key[keyIndex])) - offset;
- k = encipher ? k : -k;
- char ch = (char)((Mod(((input[i] + k) - offset), 26)) + offset);
- output += ch;
- }
- else
- {
- output += input[i];
- ++nonAlphaCharCount;
- }
- }
- Console.WriteLine(output);
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement