//Title: Customizeable Caesar Cipher
//Credit to: DivineO'peanut
//Contributed by: DivineO'peanut
//Special thanks to: Inuyashaisbest (tracking a bug)
/*
http://en.wikipedia.org/wiki/Caesar_cipher
This is a highly-customizable implantation of the Caesar cipher. You can
leave the code as is, to get the "classic" Caesar cipher, or you can modify
caesar_lower and caesar_upper to partly revise the encryption.
The proc caesarcipher(text here,number of shifts) will encipher the text and return
the result, and caesardecipher(text here,number of shifts) will decipher it.
The lists "caesar_lower" and "caesar_upper" serve as the repertoire of characters the snippet uses, and can be
changed to result in different version of the cipher.
*/
//[MAIN SNIPPET CODE GOES HERE]
var/list/caesar_lower = list("a","b","c","d","e","f","g","h","i","j","k",
"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
var/list/caesar_upper = list("A","B","C","D","E","F","G","H","I","J","K",
"L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
proc/caesarencipher(t,n=3)
. = ""
var/length = length(t)
for(var/c in 1 to length)
var/char = copytext(t,c,c+1)
if(char in caesar_lower)
var/find = caesar_lower.Find(char)
if(find+n > caesar_lower.len)
. += caesar_lower[find+n-(caesar_lower.len)]
continue
else
. += caesar_lower[find+n]
continue
else if(char in caesar_upper)
var/find = caesar_upper.Find(char)
if(find+n > caesar_upper.len)
. += caesar_upper[find+n-(caesar_upper.len)]
continue
else
. += caesar_upper[find+n]
continue
proc/caesardecipher(t,n=3)
. = ""
var/length = length(t)
for(var/c in 1 to length)
var/char = copytext(t,c,c+1)
if(char in caesar_lower)
var/find = caesar_lower.Find(char)
if(find-n < 0)
. += caesar_lower[(caesar_lower.len-1)-find-(n-1)]
continue
else
. += caesar_lower[find-n]
continue
else if(char in caesar_upper)
var/find = caesar_upper.Find(char)
if(find-n < 0)
. += caesar_upper[(caesar_upper.len-1)-find-(n-1)]
continue
else
. += caesar_upper[find-n]
continue
///*
//Testing Code/Sample Implementation:
/*** Once you are done playing with the code, uncomment this and try
enciphering something again: ***/
/*
world
New()
caesar_lower = list("1","2","3","4","5","6","7","8","9")
caesar_upper = list("!","@","#","$","%","^","&","*","(")
*/
client/verb/encipher(txt as text,shifts=3 as num) // encipher a certain text
src << caesarencipher(txt,shifts)
client/verb/decipher(txt as text,shifts=3 as num) // decipher that text
src << caesardecipher(txt,shifts)
//*/
ID:257769
Sep 14 2007, 9:46 pm (Edited on Nov 7 2007, 9:13 am)
|
|
In response to Danial.Beta
|
|
Ha! Thanks for pointing that out. Hopefully no-one read this and thought I was some sort of weirdo.
|
I was working on an encryption library a while back and a nice thing about a caesar cipher is that encrypting and decrypting are the same. You just send it 26 - shifts the second time. This is what I had for caesar (defaults at 13 for ROT13):
proc/Caesar(string, shift=13) I'd put some more up but I had a problem getting one working right and now I don't know which one it was. =) |
In response to YMIHere
|
|
The problem is with ascii characters is that you can't customize the reservoir of characters you use, which is why I defaulted to using lists. That said, your version is probably better memory-wise (minus two lists), which is preferrable in some cases.
|
In response to DivineO'peanut
|
|
Heh, I'm not sure how the entire point of the proc went over my head. Next time I'll try reading. =)
|
That's kinda private information to be giving a computer program.
Funny typo jokes aside, seems simple buy useful. Good jerb!