From: shapper on
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
From: Jeff Johnson on
"shapper" <mdmoura(a)gmail.com> wrote in message
news:ddba985f-8f2c-4f90-a71e-68ebf2530ae4(a)h13g2000yqk.googlegroups.com...

> 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.


From: Peter Duniho on
On Wed, 02 Sep 2009 09:47:21 -0700, shapper <mdmoura(a)gmail.com> wrote:

> 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
From: shapper on
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[i];
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[i] = (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[i] = String.fromCharCode(((c - a
+ 13) % (26)) + a); }
else if (c >= A && c < Z) { b[i] = String.fromCharCode(((c
- A + 13) % (26)) + A); }
else { b[i] = s.charAt(i); }
}
return b.join('');
};

This is a JQuery function.

Thanks,
Miguel

From: Peter Duniho on
On Wed, 02 Sep 2009 13:05:27 -0700, shapper <mdmoura(a)gmail.com> wrote:

> 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[i];
> 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[i] = (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