ROT-13 encryption

11 views
Skip to first unread message

Guy1954

unread,
Oct 22, 2009, 8:43:29 PM10/22/09
to xblite
Hi Xbliters,

I just uploaded ComGen_v1_06.7z, which contains my Comment Generator,
v1.06.

I added 2 new options:
1. Cipher ROT-13
2. Decipher ROT-13

Why? Because I'm currently taking a class on network security and I'll
be quized on cryptology algorithms.And I just learn the simplistic
Caesar method called ROT-n (rotation of n alphabetic position). I
started my homework today. To my surprise, I had an interesting
(little) challenge: ROT-13 has been heavily used in Usenet!

I googled for ROT-13 and I found:
"The simple Caesar-cypher encryption that replaces each English letter
with the one 13 places forward or back along the alphabet, so that
"The butler did it!" becomes "Gur ohgyre qvq vg!".
Most Usenet news reading and posting programs include a rot13 feature.
It is used to enclose the text in a sealed wrapper that the reader
must
choose to open -- e.g., for posting things that might offend some
readers, or spoilers.
A major advantage of rot13 over rot(N) for other N is that it is
self-inverse, so the same code can be used for encoding and decoding.
See also spoiler space, which has partly displaced rot13 since
non-Unix-based newsreaders became common."

This was enough to spec it and I KISS-coded it in XBLite. KISS was
good enough for me, but is it for you? Any comment from a clever
Hacker?

Bye! Guy

Guy1954

unread,
Oct 22, 2009, 9:16:19 PM10/22/09
to xblite
My KISS version:

FUNCTION Rot13Encode$ (msg$) ' ROT-13 encryption
STATIC s_from$, s_to$

IFZ msg$ THEN RETURN ""

IFZ s_from$ THEN
s_from$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
s_to$ = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
END IF

code$ = msg$
XstTranslateChars (@code$, s_from$, s_to$)
RETURN code$

END FUNCTION


A more sophisticated version:

FUNCTION Rot13Encode$ (msg$) ' ROT-13 encryption

IFZ msg$ THEN RETURN ""

code$ = msg$
ixMax = LEN (msg$) - 1
FOR ix = 0 TO ixMax
ch = msg${ix}
SELECT CASE TRUE
CASE (ch >= 'A' && ch <= 'Z')
code${ix} = 'A' + ((ch - 'A' + 13) MOD 26)
CASE (ch >= 'a' && ch <= 'z')
code${ix} = 'a' + ((ch - 'a' + 13) MOD 26)
END SELECT
NEXT ix
RETURN code$

END FUNCTION

Guy1954

unread,
Oct 23, 2009, 6:42:20 AM10/23/09
to xblite
Hi Xbliters,

I just uploaded rot13_rot47_lonne_01.7z, which contains:
- Rot13.x: a CONSOLE program to test ROT-13 (rotate by 13 places)
- Rot13.exe
- Rot47.x: same as Rot13.x but for ROT-47 (rotate by 47 places)
- Rot47.exe

I welcome any comments.
Bye! Guy

Guy1954

unread,
Oct 24, 2009, 8:06:04 PM10/24/09
to xblite
Hi Xbliters,

I just uploaded Rot_WinX_lonne_01.7z, which contains encode.x, a GUI
application using WinX to replace the CONSOLE programs Rot13.x and
Rot47.x.

I need to code ROT-N and I'll be thru with cipher/decipher using
rotation.
Bye! Guy

Guy1954

unread,
Oct 25, 2009, 9:15:34 PM10/25/09
to xblite
Hi Xbliters,

I just uploaded Rot_WinX_lonne_02.7z.
In this version 2, I push the library WinX to the limit, and WinX
responded beautifully.
I believe that WinX is the primary library for XBLite GUI
developments.
Nice contribution, Callum!

The only feature that I think is missing (AFAIK) is the automatic
controls resizing when the main window is resized by the User.
I tried to use WinXAutoSizer_SetInfo without success. Maybe Callum
would be so nice as to have a look at encode.x and see what is
bugging? (the code is in FUNCTION CreateWindows)

Beside this, can any of you download it and execute it? I'd would
appreciate any feedback on it.

Bye! Guy

Vic Drastik

unread,
Oct 27, 2009, 8:50:24 PM10/27/09
to xbl...@googlegroups.com

Here is a more terse way to express ROT-13

FUNCTION STRING Rot13(STRING s)
STRING t

IFZ s THEN RETURN ""
t = s
FOR i = 0 TO UBOUND(t)
c = t{i} : t{i} = c +
13*(-(c>64)+2*(c>77)-(c>90)-(c>96)+2*(c>109)-(c>122))
NEXT i

RETURN t
END FUNCTION

Another way is to clear bit 5 of the character, thus converting from
lower to upper case, do the above using only 3 indicator functions
instead of 6, then put bit 5 back(if necessary), but this takes 2 extra
lines of code.


Vic

Guy1954

unread,
Oct 27, 2009, 10:21:46 PM10/27/09
to xblite
Hi Vic,

Whaoo! I'll have to put your code apart to understand how it works!
How did you get this algorithm?

Bye! Guy

Vic Drastik

unread,
Nov 19, 2009, 7:59:55 AM11/19/09
to xbl...@googlegroups.com
Guy1954 wrote:
> Hi Vic,
>
> Whaoo! I'll have to put your code apart to understand how it works!
> How did you get this algorithm?
>
> Bye! Guy
>

For some character whose ASCII value is c, and for some point n, the
expression (-(c>=n)) is 0 for 0...(n-1) and 1 for n...255. Since what we
need is something which makes no change up to 64, then adds 13 for
65-77, subtracts 13 for 78-90, etc., it was just a case of picking the
various n values required, then simplifying by noting that (-(c>=n)) is
the same as (-(c>(n-1))).

Vic

Guy1954

unread,
Nov 20, 2009, 4:18:13 AM11/20/09
to xblite
Hi Vic,

Thanks for the explanations.
I replaced my version of Rot13 with yours: when it comes to cypher,
hacking is the way to go!
Why? because the messages can be really big and a speedier version
might succeed when a slower one might not take a reasonable time to do
the job.

Thank you for the tips, Vic.
Guy
Reply all
Reply to author
Forward
0 new messages