GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2007-11-04 17:53:25

Austin
Member
From: Florida
Registered: 2007-10-17
Posts: 9

ROT13 Encryption

Here is the script I made as a request at the GMC, it will encrypt/decrypt text into/out of ROT13 format:

Expand/*
**  Usage:
**      val = rot13(a);
**
**  Arguments:
**      a           1st argument, text to be encrypted/decrypted
**
**  Returns:
**      val         the encrypted/decrypted text
**
**  Notes:
**      Only works for letters, that means no numbers, symbols, spaces, etc.
**
**  GMLscripts.com
*/
{
	var a,b,str,val,i;
	a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	b = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
	str = string_letters(argument0);
	val = "";
	for (i=1; i<=string_length(str); i+=1;)
		val += string_char_at(b,string_pos(string_char_at(str,i),a));
	return val;
}

This probably could be optimized further, it was just a quick request... ^^;

Last edited by Austin (2007-11-05 18:47:52)

Offline

#2 2007-11-05 17:07:07

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: ROT13 Encryption

Thanks for the script, I was hoping somebody would post a ROT13 function. The only thing it seems to need is to declare the variable "i" in the "var" statement. Just make that edit and I think it's good. I don't think it could be made more efficient. The only other change I'd make is to rename the argument "str" which is the standard style used for Game Maker's built-in functions. Calling it "a" is a little confusing since variable "a" is actually doing something different in the code.


Abusing forum power since 1986.

Offline

#3 2007-11-05 18:48:54

Austin
Member
From: Florida
Registered: 2007-10-17
Posts: 9

Re: ROT13 Encryption

I added the "i" variable to the declaration line and changed "text" to "str."

Offline

#4 2007-11-07 00:17:59

Quimp
Member
Registered: 2007-10-09
Posts: 15

Re: ROT13 Encryption

I agree about the argument "a", it could lead to confusion. Other than that, you could remove the string_length() function from the loop to make it faster but I don't know how much it could affect speed.

Offline

#5 2007-11-07 07:19:08

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: ROT13 Encryption

Well I have no experience with rot13, or any encryption at all, but isn't this encryption a bit "easy" to break: it uses a static encryption.. I wonder wouldn't it be possible to adjust this encryption slightly so you can give your own "a" (the "a" which is used as first string), and that the script then makes the "b" from this a..

Offline

#6 2007-11-07 14:52:25

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: ROT13 Encryption

Quimp, I agree, I would normally precompute the string_length() since it's going to be called many times in a loop.

paul23, ROT13 isn't designed to be secure, it's designed to hide text that can be cracked very easily. It is popular as a spoiler hider on some old-school forums and newsgroups. Some newsgroup readers even have a built-in ROT13 tool. I'm sure there are browser extensions out there as well. As for the encryption alterations you are describing, that would make a Caesar cipher. That would be a good script for someone to submit. It's not secure, but like ROT13, it has it's uses because it is so easily allows decryption.


Abusing forum power since 1986.

Offline

#7 2007-11-07 15:51:54

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: ROT13 Encryption

@xot, well that proves I completely have no idea what I'm talking about tongue.


but as for the speed of "string_length" - well if that is the slow point you better use a repeat loop (and an own index variable), like my perftest showed it's way faster, since the "expression" is only computed once, as opposed to be computed every round. Though changing for loops by repeat statements doesn't really improve readability tongue.

Offline

#8 2007-11-07 16:35:47

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: ROT13 Encryption

Readability is the only reason I wouldn't use the repeat construct, although I have often used it anyway for the small boost in speed.


Abusing forum power since 1986.

Offline

#9 2007-11-07 17:27:11

Austin
Member
From: Florida
Registered: 2007-10-17
Posts: 9

Re: ROT13 Encryption

I was just trying to make the code as short as possible, that is why I had the "a" and "b" variables (and I couldn't think of a better name tongue) and the string_length() in the loop. Here is the new script:

Expand/*
**  Usage:
**      val = rot13(text);
**
**  Arguments:
**      text        1st argument, text to be encrypted/decrypted
**
**  Returns:
**      val         the encrypted/decrypted text
**
**  Notes:
**      Only works for letters, that means no numbers, symbols, spaces, etc.
**
**  GMLscripts.com
*/
{
	var a,b,str,len,val,i;
	a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	b = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
	str = string_letters(argument0);
        len = string_length(str);
	val = "";
	for (i=1; i<=len; i+=1;)
		val += string_char_at(b,string_pos(string_char_at(str,i),a));
	return val;
}

Offline

#10 2007-11-07 18:07:11

Yourself
Member
Registered: 2007-10-09
Posts: 48

Re: ROT13 Encryption

Gah, indent your for loop.  Also get rid of that dangling semi-colon in there.

Offline

#11 2007-11-08 07:23:53

Austin
Member
From: Florida
Registered: 2007-10-17
Posts: 9

Re: ROT13 Encryption

You guys are so picky. tongue Fixed script:
EDIT: It seems that I can't add the extra indent to the code...

Expand/*
**  Usage:
**      val = rot13(text);
**
**  Arguments:
**      text        1st argument, text to be encrypted/decrypted
**
**  Returns:
**      val         the encrypted/decrypted text
**
**  Notes:
**      Only works for letters, that means no numbers, symbols, spaces, etc.
**
**  GMLscripts.com
*/
{
    var a,b,str,len,val,i;
    a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    b = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
    str = string_letters(argument0);
    len = string_length(str);
    val = "";
    for (i=1; i<=len; i+=1)
        val += string_char_at(b,string_pos(string_char_at(str,i),a));
    return val;
}

Last edited by Austin (2007-11-08 07:26:35)

Offline

#12 2007-11-08 15:49:37

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: ROT13 Encryption

The missing indent is an issue with the code highlighting software. It automatically reformats the indentation and will only indent a code block if it is inside { curly braces }. Kind of annoying sometimes. That's part of the reason why code like that is on one line at the main site, or I'll use braces when I don't have to.

Expand// This
for (i=1; i<=len; i+=1) val += string_char_at(b,string_pos(string_char_at(str,i),a));
// or That
for (i=1; i<=len; i+=1) {
    val += string_char_at(b,string_pos(string_char_at(str,i),a));
}

Abusing forum power since 1986.

Offline

#13 2008-01-06 01:43:35

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: ROT13 Encryption

ROT13 is now on the site:

http://www.gmlscripts.com/script/rot13

Thanks!


Abusing forum power since 1986.

Offline

#14 2008-01-07 22:49:30

atarian
Member
Registered: 2008-01-07
Posts: 9

Re: ROT13 Encryption

hi guys

well this is the first topic i have read and in doing so deiceded to make a ceaser_cipher i will post it soon

Offline

#15 2008-01-07 23:00:43

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: ROT13 Encryption

Welcome aboard, fellow Atarian! Why not introduce yourself here: http://www.gmljoint.com/viewtopic.php?id=4


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB