Rot13

S

shapper

Hello,

Does anyone knows a script to perform Rot13 and Rot5 rotation?

And can I use the same script by just changing the value of 13 to 5?

Thanks,
Miguel
 
J

Jeff Johnson

Does anyone knows a script to perform Rot13 and Rot5 rotation?

Rot13: A-->N B-->O ... M-->Z N-->A O-->B, etc. It should take you all of two
or three minutes to code that out.
And can I use the same script by just changing the value of 13 to 5?

Probably not, since 26 isn't divisible by 5.
 
P

Peter Duniho

Hello,

Does anyone knows a script to perform Rot13 and Rot5 rotation?

Script? What do you mean?
And can I use the same script by just changing the value of 13 to 5?

Sort of. You'll have to also specify the range of ASCII values being
affected.

Pete
 
S

shapper

Something like the following?

public static string Rot13(string text) {

char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++) {
int number = (int)chars;
if (number >= 'a' && number <= 'z') {

if (number > 'm')
number -= 13;
else
number += 13;


} else if (number >= 'A' && number <= 'Z') {
if (number > 'M') {
number -= 13;
} else {
number += 13;
}
}
chars = (char)number;
}
return new string(chars);
}

I am not sure if the á, ç, À, Õ, etc characters are considered this
way.

I would like to use this for email obfuscation so I need to have the
obfuscation on the client and on the server done in the same way. On
the client I have:

$.rotate13 = function(s) {
var b = [],c,i = s.length,a = 'a'.charCodeAt(),z = a + 26,A=
'A'.charCodeAt(),Z = A + 26;
while (i--) {
c = s.charCodeAt(i);
if (c >= a && c < z) { b = String.fromCharCode(((c - a
+ 13) % (26)) + a); }
else if (c >= A && c < Z) { b = String.fromCharCode(((c
- A + 13) % (26)) + A); }
else { b = s.charAt(i); }
}
return b.join('');
};

This is a JQuery function.

Thanks,
Miguel
 
P

Peter Duniho

Something like the following?

public static string Rot13(string text) {

char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++) {
int number = (int)chars;
if (number >= 'a' && number <= 'z') {

if (number > 'm')
number -= 13;
else
number += 13;


} else if (number >= 'A' && number <= 'Z') {
if (number > 'M') {
number -= 13;
} else {
number += 13;
}
}
chars = (char)number;
}
return new string(chars);
}

I am not sure if the á, ç, À, Õ, etc characters are considered this
way. [...]


ROT13 is an ASCII thing, and IMHO you should limit your implementation to
modifying only the ASCII alphabetic characters (i.e. not those other "etc
characters" you mention).

As far as your specific implementation goes, it seems fine to me. I would
take the approach that your JQuery function does, shifting the calculation
down to the range 0-25 and using the modulo function, rather than
conditionally adding or subtracting depending on the input. But there's
not really any particularly compelling reason to choose one approach over
the other.

Other differences I might have had in my implementation include using a
StringBuilder instead of a char[] as my workspace for modifying the
existing string, or possibly even using a byte[] as input and requiring
the caller to convert to/from an ASCII byte[]. Alternatively, do the
conversions in the method for the caller, but generate an error if some
non-ASCII text is found.

But really, the code you posted should accomplish the basic functionality
you seem to be looking for, so why mess with it if it works and suits your
needs? :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Import / Export CSV. 3
ZIP in NET 4.0 1
About ROT13 6
Screenshot of PDF page. Does anyone knows how to do this? 3
Regex - Validate Url 4
NET 4.0 and NET 3.5 1
Image 4
Word 2007 to PDF 1

Top