You are not logged in.
Pages: 1
Here is the script I made as a request at the GMC, it will encrypt/decrypt text into/out of ROT13 format:
/*
** 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
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
I added the "i" variable to the declaration line and changed "text" to "str."
Offline
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
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
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
@xot, well that proves I completely have no idea what I'm talking about .
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 .
Offline
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
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 ) and the string_length() in the loop. Here is the new script:
/*
** 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
Gah, indent your for loop. Also get rid of that dangling semi-colon in there.
Offline
You guys are so picky. Fixed script:
EDIT: It seems that I can't add the extra indent to the code...
/*
** 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
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.
// 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
Abusing forum power since 1986.
Offline
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
Welcome aboard, fellow Atarian! Why not introduce yourself here: http://www.gmljoint.com/viewtopic.php?id=4
Abusing forum power since 1986.
Offline
Pages: 1