Created
May 15, 2016 07:44
-
-
Save anonymous/d5b5ff9373937d2096bacc86e4e7f1a4 to your computer and use it in GitHub Desktop.
Some class code of caesar cryptography sample. (http://csharppad.com/gist/b874d4eaf8089322cb2a75db869cbdcd)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* | |
* This Gist was created at CSharpPad.com | |
* To run this file, open http://csharppad.com/gist/b874d4eaf8089322cb2a75db869cbdcd | |
**********/ | |
public class caesar { | |
string plain,output; | |
int Key; | |
public caesar(string source, int givenKey) { | |
plain = source; | |
Key = givenKey; | |
} | |
public void crypt() { | |
char[] array = plain.ToCharArray(); | |
for (int i = 1; i < array.Length; i++) { | |
if (char.IsLetter(array[i])) { | |
int c = array[i]; | |
if (c > 'a' && c < 'z') { | |
c = (((c-'a')+ Key) % 26)+'a'; | |
} | |
if (c > 'A' && c < 'Z') { | |
c = (((c-'A')+ Key) % 26)+'A'; | |
} | |
array[i] = (char)c; | |
} | |
output = new string(array); | |
} | |
} | |
public void decrypt() { | |
char[] array = plain.ToCharArray(); | |
for (int i = 1; i < array.Length; i++) { | |
if (char.IsLetter(array[i])) { | |
int c = array[i]; | |
if (c > 'a' && c < 'z') { | |
c = (((c-'a')- Key) % 26)+'a'; | |
} | |
if (c > 'A' && c < 'Z') { | |
c = (((c-'A')- Key) % 26)+'A'; | |
} | |
array[i] = (char)c; | |
} | |
output = new string(array); | |
} | |
} | |
public string GetOutput() { | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment