Jump to content


Photo

Useful String Handling Functions


  • Please log in to reply
9 replies to this topic

#1 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 01 January 2008 - 11:47 PM

Extra String Handling Functions
by: Rixeno

I plan on making some string handling functions that can be used for any purpose without credit (although appreciated). So far, I have five scripts that I made:
- string_reverse
- string_randomize
- string_asci
- string_encrypt (and decrypt)
- string_create

String_reverse
This script takes a string and returns that exact string, yet reversed. So far, it doesn't get fooled with these " " and these ' ', and I think it supports just about any character after that.
Example :
string_reverse == esrever_gnits
//string_reverse
//Takes a string and reverses it
//Argument(s):
//argument0 = string
//Returns: The reversed string
//Written by Rixeno
var str_length,str_new,i1,i2,str_char;
str_length=string_length(argument0);
str_new="";
i1=str_length;
i2=1;
repeat(str_length)
{
str_char=string_char_at(argument0,i1);
str_new=string_insert(str_char,str_new,i2);
i1-=1;
i2+=1;
}
return string(str_new);
String_randomize
This script takes a string and returns a randomized version of the string. Basically, it takes every character and puts it at a random position. It's also a simple encrypting function.
//string_randomize
//This script takes a string and places the characters at random positions
//Argument(s):
//argument0 = string
//Returns: The randomized string
//Written by Rixeno
var str_length,str_new,str_char,i1;
str_length=string_length(argument0)
str_new=""
i1=1
repeat (str_length)
{
str_char=string_char_at(argument0,i1)
str_new=string_insert(str_char,str_new,random(str_length))
i1+=1
}
return string(str_new);
String_asci
This script returns the asci value of the whole string (not only one character) byu checking every character.
//string_asci
//This script returns the ascii value of the string for every character
//Argument(s):
//argument0 = string
//Returns: Asci value of the string
//Written by Rixeno
var str_length,str_new,str_char,i1,i2;
str_length=string_length(argument0);
str_new="";
i1=1;
i2=0
repeat (str_length)
{
str_char=string_char_at(argument0,i1)
str_new=string_insert(string(ord(str_char)),str_new,i2)
i1+=1
i2+=3
}
return string(str_new);
String_encrypt
This script takes a string and encrypts it according to the number you give it. It can encrypt and by using the reverse number (number*(-1)), you can decrypt the message.
//string_encrypt
//This script encrypts/decrypts a string using the numerical value
//Ex:       Encrypt level 5
//          Decrypt level (-5)
//Argument(s):
//argument0 = string
//argument1 = encryptic value (number)
//Returns: The encrypted/decrypted string
//Written by Rixeno
var str_length,str_new,str_char,str_crypt,str_number,i
1;
str_length=string_length(argument0)
str_new=""
i1=1
repeat (str_length)
{
str_char=string_char_at(argument0,i1)
str_crypt=real(ord(str_char)+argument1)
str_new=string_insert(chr(str_crypt),str_new,i1)
i1+=1
}
return string(str_new);
String_create
This script creates a random word with the desired length and whether or not to start with a vowel or not. The letters can be changed and easily set to any language.
//string_create_random
//This script creates a random word (you can remove/add any letter you want in the list too)
//Argument(s):
//argument0 = length
//argument1 = start with a vowel (true/false)
//Returns: The new word as a string
//Written by Rixeno
var str_new,str_vowel,str_char;
str_new=""
str_vowel=argument1
repeat (argument0)
{
    if (str_vowel==true)
    {
    str_vowel=false
    str_new=string_insert(choose('a','e','i','o','u'),str_new,argument0+1)
    }
    else
    {
        if (choose(1,2)==1)
        {
        str_char=choose('b','c','d','f','g','h','j','k','l','m')
        }
        else
        {
        str_char=choose('n','p','q','r','s','t','v','w','x','y','z')
        }
        str_new=string_insert(str_char,str_new,argument0+1)
        str_vowel=true
    }
}
return string(str_new);

Enjoy :(

Edited by Rixeno, 03 January 2008 - 06:25 PM.

  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#2 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 02 January 2008 - 04:15 PM

Sorry if this is a bump, but there are two new scripts that I added to the list. It is not only for the reverse string now, this topic will be for all my string handling scripts from now on...

EDIT: Added new string encryption script
EDIT2: Added new random word/name generator

Edited by Rixeno, 03 January 2008 - 12:59 AM.

  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#3 kubaszewczyk11

kubaszewczyk11

    GMC Member

  • New Member
  • 100 posts

Posted 21 February 2008 - 03:37 PM

Good!
But please don't double post, Rixeno.
  • 0

#4 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 21 February 2008 - 09:06 PM

Thanks for the comment kubaszewczyk11, glad you like it :blink:
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#5 brawllegend

brawllegend

    GMC Member

  • New Member
  • 204 posts

Posted 22 February 2008 - 09:00 PM

Those are great! I have one question though. When I type in something with some of the some letter, it's like a cryptogram and it just assigns a different character for each character of the string. If I modified it, like so:
//string_encrypt
//This script encrypts a string using the numerical value
//Argument(s):
//argument0 = string
//argument1 = encryptic value (number)
//Returns: The encrypted string
//Written by Rixeno, modified by brawllegend
var str_length,str_new,str_char,str_crypt,str_number,i
1;
str_length=string_length(argument0)
str_new=""
i1=1
repeat (str_length)
{
str_char=string_char_at(argument0,i1)
str_crypt=real(ord(str_char)+argument1)
str_crypt+=round((sqr(i1)+(i1*13)-75)-round(sqrt(abs(i1*16)))/3)
str_new=string_insert(chr(str_crypt),str_new,i1)
i1+=1
}
str_crypt=0
return string(str_new);
(I just modified your code)
and to decrypt
//string_decrypt
//This script decrypts a string using the numerical value
//Argument(s):
//argument0 = string
//argument1 = decryptic value (number)
//Returns: The decrypted string
//Written by Rixeno, modified by brawllegend
var str_length,str_new,str_char,str_crypt,str_number,i
1;
str_length=string_length(argument0)
str_new=""
i1=1
repeat (str_length)
{
str_char=string_char_at(argument0,i1)
str_crypt=real(ord(str_char)-argument1)
str_crypt-=round((sqr(i1)+(i1*13)-75)-round(sqrt(abs(i1*16)))/3)
str_new=string_insert(chr(str_crypt),str_new,i1)
i1+=1
}
str_crypt=0
return string(str_new);
(Again, I just modified your code)

...Would that be at least a little more secure? Or would it not matter and even a novice cracker could find it out...

It works though! Anyways, great scripts and if I use them, I'll be sure to give credit

edit*Hmmm... it seems there are characters that it creates that Game Maker cannot type (for lack of a better word). I think the only way for it to work is to have a code set a global variable to the encrypted version within the game, not within the code cuz that won't work) sorry if that was too vague or just stupid...

anyways, if i can come up with a way for game maker to be able to set it within the code, i'll pm you it (if you want me to) but im not gonna do it right now.

Edited by brawllegend, 22 February 2008 - 10:48 PM.

  • 0

#6 Rithiur

Rithiur

    GMC Member

  • GMC Elder
  • 90 posts
  • Version:Unknown

Posted 23 February 2008 - 10:35 AM

edit*Hmmm... it seems there are characters that it creates that Game Maker cannot type (for lack of a better word). I think the only way for it to work is to have a code set a global variable to the encrypted version within the game, not within the code cuz that won't work) sorry if that was too vague or just stupid...

<{POST_SNAPBACK}>


Yes. This is because the encryption is simply shifting the ASCII numbers of the characters. The problems is that ASCII codes 0 to 31 are not actual characters, but basically command characters with no character associated with them (well, except for tab and newline chars). Since these characters can not really be typed, you can't enter them manually.

Also, a word of warning about that kind "encryption". If you intend to save it into a file, you must use the binary file handling functions. This is because if the data happens to contain either a new line or carriage return (ASCII 10 or 13), it can not be read properly by the text reading functions, since it will treat them as new lines and not read the character. You can not even manually add them, because it's not possible to distinguish between them.
  • 0
<a href="http://rithiur.anthd.com" target="_blank">phpInDev</a> - Mostly PHP, but with a flavour of Game Maker

#7 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 23 February 2008 - 06:35 PM

Well, it's only a basic encrypting system to encrypt a string. I made in the intention to be simply used within the game, not externally in a text file. And for the ASCII "problem" and not showing some characters, it doesn't really matter, because it you save the string, it won't consider the black areas as spaces, it will be whatever the ASCII value was, and that can go really high (I went over 9999999 or something like that in ASCII and there wasn't an error. The higher the number, the harder it is to decrypt the message (sometimes)
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#8 brawllegend

brawllegend

    GMC Member

  • New Member
  • 204 posts

Posted 24 February 2008 - 03:43 AM

Would that be really easy for someone to crack though? I don't see how anyone could, but I'm sure there's a simple way... I'm hoping to learn the more complicated encryption methods when I get into high school, but for now, I'll stick with the simple ones.
  • 0

#9 Rithiur

Rithiur

    GMC Member

  • GMC Elder
  • 90 posts
  • Version:Unknown

Posted 24 February 2008 - 04:02 PM

Would that be really easy for someone to crack though? I don't see how anyone could, but I'm sure there's a simple way...

Well, generally speaking yes. There is certain Kerchkhoffs' principle, which goes "a cryptosystem should be secure even if everything about the system, except the key, is public knowledge.". This is particularly relevant here, because once this particular encryption method is known, it's practically cracked.

The key to this particular encryption method is almost entirely irrelevant. The problem is that since the key is added to the ASCII value as it is, it generates the problem that there is only 256 different possibilities for the key, because the ASCII system has only 256 values and once the values go higher than that, they simply roll back to zero.

For example, the following code will display the intial message correctly:
msg = "Not very secure encryption";
key = 192;

enc = encrypt(msg, key + 8 * 256);
dec = decrypt(enc, key + 17 * 256);

show_message(dec);

This is actually quite good example of why you shouldn't try to come up with your own encryption algorithms. The reason that professionally generated ones exist, is because those are proven to be effective and aren't affected by simple attacks like this.

When you create a algorithm, you really need to ask yourself, if you can mathematically prove it is flawless. If you can't, you probably shouldn't be doing it.

That being said, you could also question the need for encryption, if it's only inside a game and not intended to deal with anything outside the software.
  • 0
<a href="http://rithiur.anthd.com" target="_blank">phpInDev</a> - Mostly PHP, but with a flavour of Game Maker

#10 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 24 February 2008 - 09:29 PM

Yeah, but I just write random scripts anyways. I barley ever find a use to half of them because it's not really good for a game and such, but Is till enjoy sharing some. Also, something encrypted just means that it's in a way, different from it's original form. So "meh" could be simply "nfi" and it would be somewhat encrypted, even though it just changed by one number (in ASCII value).
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-