GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2014-04-25 17:39:19

Sorsy
Member
Registered: 2014-04-25
Posts: 2

INI file not using the default value

Hello!
I'm trying to use the rc4 script to encrypt my INI file. Everything is working fine except that I can't seem to be able to change the default value that INI sets itself if it cannot find existing INI file.
Here is what scripts I have:
scr_cr - rc4 itself
{
    var str,key,out,len,i,S,j,temp,pos,t;
    str = argument0;
    key = argument1;
    out = "";
    len = string_length(key);
    for (i=0; i<256; i+=1) S[i] = i;
    j = 0;
    for (i=0; i<256; i+=1) {
        j = (j + S[i] + ord(string_char_at(key,(i mod len)+1))) mod 256;
        temp = S[i];
        S[i] = S[j];
        S[j] = temp;
    }
    i = 0;
    j = 0;
    for (pos=0; pos<string_length(str); pos+=1) {
        i = (i + 1) mod 256;
        j = (j + S[i]) mod 256;
        temp = S[i];
        S[i] = S[j];
        S[j] = temp;
        t = (S[i] + S[j]) mod 256;
        out += chr(ord(string_char_at(str,pos+1)) ^ S[t]);
    }
    return out;
}

And I call it with this:
ini_read_real_encrypted
{
var section,key,encryption_key,value;
section = argument0;
key = argument1;
encryption_key = argument2;
value = ini_read_string(section,key,1);
value = scr_cr(value,encryption_key);
return value;
}

The problem is that when I change the default value of ini_read_string(section,key,1) in ini_read_real_encrypted it doesn't do anything, it is still setting the default to 0.
Could someone look at this and tell me if I'm doing something wrong? I can provide some more info if needed.
Thanks in advance!

Offline

#2 2014-04-26 04:30:58

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

Re: INI file not using the default value

The function ini_read_string() works with strings, therefore the default value should also be a string.

There are some other things you should be aware of.

INI files are meant for normal text. Storing data encrypted by RC4 will likely cause problems without taking other steps to ensure the data is in a format compatible with INI. Transcoding the data into Base64 is a good option. Also be aware that reading large amounts of data from INI files can be problematic. INI is meant for small values such as application settings. GameMaker can write values longer than 1000 characters to an INI file but it will not be able read them back in.

If you are using GameMaker: Studio, string handling differs from earlier versions in some very important ways. The RC4 script may not work as expected or at all. One factor is the support of multibyte characters (Unicode). RC4 is meant to work on byte streams. Multibyte characters read by string_char_at() may result in lost data. GM:Studio has string functions for working with individual bytes directly but they will not solve the next problem.

GM:Studio strings are terminated by a null character. If the encryption produces a null, which is quite possible, any data following it will be lost. In general, binary data should not be stored in strings when using GM:Studio. Arrays, data structures, and (preferably) buffers are the only safe options. New versions of the RC4 script need to be created to support these.

You can still use RC4 with INI files but it will take some extra steps to ensure the data is handled safely.

See the comments posted with the script for additional details.

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


Abusing forum power since 1986.

Offline

#3 2014-05-02 11:16:38

Sorsy
Member
Registered: 2014-04-25
Posts: 2

Re: INI file not using the default value

Hi, I'm just reporting in what i did in a last couple of days so that it could help someone else.
I did indeed encoded the arc4 data with base64 which is now actualy in gms itself, so that was quick. But then I've found out that while it was working nicely in Windows, it wasn't working on Android. And then I've found that the ini file which is gm creating on first lounch isn't utf8 which seems to be the problem. So I've just added the right one to the Included files. Now it works just how it should ;)
Thank you for a much needed help! :]

Last edited by Sorsy (2014-05-02 11:33:29)

Offline

#4 2014-05-05 16:18:13

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

Re: INI file not using the default value

Thanks for the update and tip about Android. Glad you got it working.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB