1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Encrypt Decrypt / Encode Decode

Discussion in 'Programming' started by NoFontNL, Aug 9, 2019.

  1. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    305
    Hi, I'm currently using this script to encode / decode or whatever it's called.
    Code:
    /// crypt_string(in,key,mode)
    //
    //  Returns the given string enciphered or deciphered 
    //  using a simple Vigenere style cipher, and filtering
    //  out non-printable characters.
    //
    //      in          input, string
    //      key         enciphering key, string
    //      mode        0 = decipher, 1 = encipher
    //
    {
        var in, key, mode, out;
        in = argument0;
        key = argument1;
        mode = argument2;
        out = "";
        var inLen, keyLen, pos, inChar, keyChar, outChar;
        var inVal, keyVal, outVal, loVal, hiVal, span;
        inLen = string_length(in);
        keyLen = string_length(key);
        loVal = 32;
        hiVal = 126;
        span = (hiVal - loVal) + 1;
        for (pos=0; pos<inLen; pos+=1) {
            inChar = string_char_at(in, pos+1);
            keyChar = string_char_at(key, (pos mod keyLen)+1);
            inVal = min(max(loVal, ord(inChar)), hiVal) - loVal;
            keyVal = min(max(loVal, ord(keyChar)), hiVal) - loVal;
            if (mode) {
                outVal = ((inVal + keyVal) mod span) + loVal;
            }else{
                outVal = ((span + inVal - keyVal) mod span) + loVal;
            }
            outChar = chr(outVal);
            out = out + outChar;
        }
        return out;
    }
    
    However, when having a lot of text (json instance), then it takes up to 1 minute to decipher, which is pretty long. Is there another way to encode and decode with a key, which doesn't take up so much time?

    Thanks in advance.
     
  2. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    424
    Looks like a basic shift cypher to me. I don't know if you can find an algorithm for encrypting with a lower order of magnitude than O(n). I think the major drag her is all the file processing if you data file is truly that huge. I am not sure if there is a way to minimize the file size or break it into smaller sections, but I would think that would be your best bet to improve load times.
     
  3. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,714
  4. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    305
    How do I rewrite the script with buffers?
    Do I just write 'out = buffer_create()' and how do I determine the size of the buffer? Also, when reading from the buffer, should I use buffer_string or buffer_text?
     
  5. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    305
    I tried this:
    Code:
    /// crypt_string(in,key,mode)
    //
    //  Returns the given string enciphered or deciphered 
    //  using a simple Vigenere style cipher, and filtering
    //  out non-printable characters.
    //
    //      in          input, string
    //      key         enciphering key, string
    //      mode        0 = decipher, 1 = encipher
    //
    {
        var in, key, mode, out;
        in = argument0;
        key = argument1;
        mode = argument2;
        out = buffer_create(1024,buffer_grow,1);
        var inLen, keyLen, pos, inChar, keyChar, outChar;
        var inVal, keyVal, outVal, loVal, hiVal, span;
        inLen = string_length(in);
        keyLen = string_length(key);
        loVal = 32;
        hiVal = 126;
        span = (hiVal - loVal) + 1;
        for (pos=0; pos<inLen; pos+=1) {
            inChar = string_char_at(in, pos+1);
            keyChar = string_char_at(key, (pos mod keyLen)+1);
            inVal = min(max(loVal, ord(inChar)), hiVal) - loVal;
            keyVal = min(max(loVal, ord(keyChar)), hiVal) - loVal;
            if (mode) {
                outVal = ((inVal + keyVal) mod span) + loVal;
            }else{
                outVal = ((span + inVal - keyVal) mod span) + loVal;
            }
            outChar = chr(outVal);
            buffer_seek(out,buffer_seek_end,0);
            buffer_write(out,buffer_string,outChar);
            //out = out + outChar;
        }
        return buffer_read(out,buffer_string);
    }
    
    But this doesn't work.
     
  6. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,714
    It doesn't work because you didn't read the example from my previous post. Anyone who did should see this workflow:
    • Create a grow buffer
    • Add additional pieces by writing as buffer_text, without any seeking
    • Finish by writing a single byte of 0, then seeking to the beginning of the buffer, then reading as buffer_string
    This clearly isn't reflected in your code. You're just dumping all the content at the end of a grow buffer (with a huge gap at the beginning that isn't ever written to) and then reading further while the cursor is already at the back. Common sense should have told you that's senseless.

    Also, since your code is designed to be run over and over again (as opposed to my benchmark), you should use buffer_delete() on the buffer after you're done.
     
    Last edited: Aug 10, 2019
  7. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    305
    I did that with out=buffer_create(1024,buffer_grow,1);
    How do I do that? buffer_string is the way to go, but what do I put as 'value' in buffer_write?

    Also, I return the 'out' in the end, how do I delete the buffer afterwards? I can't delete it before, because then it won't return anything, I also can't delete it after the return, because it skips the rest of the script then.
     
  8. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,714
    This is the third time I'm asking you to read my example. It's obvious from the example that string fragments are written in the form buffer_write(buffer, buffer_text, fragment);. Common sense should have told you to do this without seeking beforehand:
    Code:
    buffer_write(out, buffer_text, outChar);
    
    What's so difficult about this?
    Code:
    var result = buffer_read(out, buffer_string);
    buffer_delete(out);
    return result;
    
     
  9. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    305
    ahhh, I thought it was like a ds_list, when you use a variable reffering to a value in it, then delete the list, then the variable doesn't contain the value anymore.

    Coming back to my first problem, also while using these faster buffers, it still takes way more time than when not encoding it at all. ( 1 minute difference )
    I don't want the user to easily edit stuff inside the file, so is there any alternative to this method?
     
  10. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    572
    You could try xor encryption instead, which is a simpler algorithm and might speed it up a little.

    Another thing to try would be to use a lookup table rather than calculations. I'd think that should speed it up.

    No idea if either of these would bring about any significant speed boost though.
     

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