Skip to content

Instantly share code, notes, and snippets.

Created May 15, 2016 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d5b5ff9373937d2096bacc86e4e7f1a4 to your computer and use it in GitHub Desktop.
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 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