GML Custom Encryption

Discussion in 'Tutorials' started by TheWaffle, Jun 7, 2019.

Tags:
  1. TheWaffle

    TheWaffle Member

    Joined:
    Feb 2, 2017
    Posts:
    26
    GM Version: 1.4 (any should work)
    Target Platform: All
    Download: N/A
    Links: N/A


    This is a simple encryption method I created for my games.
    Sure, there are other methods, MD5 among others, but I like doing things myself
    so that I fully understand what is happening.

    Code:
    {
        // my own random encryption method
        random_set_seed(floor(argument1));
        var _ecrypt="";
        for (var i=1; i<=string_length(_string); i+=1)                      // right_oh (8)
        {                                                                   // i=3
            
            var _skip=random(3);
            for (var ii=0; ii<_skip; ii++)
            {
                var _mask = random(255);    // select a non-sequential mask
            }
            
            var _char = chr( (ord(string_char_at(_string,i)) ^ _mask) );  // g
            if _char=="" {_char=chr(_mask);} // ensure not 0
            //var _inchar=string_byte_at(_string,i);      // other methode
            //var _char=ansi_char(_inchar ^ _mask);
            
            _ecrypt=_ecrypt+_char;                                     // right_oh
        }
        _string=_ecrypt;
    
    
    
        // save a string to a file and decrypt for testing
        var _buffer=buffer_create(string_byte_length(_string)+1,buffer_fixed,1); // 1 byte per char
        buffer_write(_buffer,buffer_string,_string);
        buffer_save(_buffer,argument0+"d");
        buffer_delete(_buffer);   
    }
    I would place this code into a script such as:

    scr_savefile(filename,key)

    so that argument0 is the file name and argument1 is the encryption key.
    If you wish to encrypt a JSON file

    see
    https://forum.yoyogames.com/index.php?threads/how-to-localize-your-game-using-json.55568/

    you would place my code after creating your JSON text and then save it.
    To decrypt, just run the same code after load the text, and then convert back to JSON.

    Possible setting to change to make your version unique:

    1 - change _skip to something other than random(3)
    this is how many random numbers to skip before using the mask

    2 - change _mask to something less than random(255)
    this is the mask used to xor with your original text file to hide the test

    3 - maybe add some extra random letters at random points (very fancy)

    In my game, each time the user saves a game, a new random key is generated and saved to
    a text file. GameMaker offers 2 different random generators creating many possible outcomes
    for a randomly generated key.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice