• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Encrypt Decrypt / Encode Decode

N

NoFontNL

Guest
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.
 

curato

Member
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.
 
N

NoFontNL

Guest
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?
 
N

NoFontNL

Guest
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.
 

FrostyCat

Redemption Seeker
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:
N

NoFontNL

Guest
Create a grow buffer
I did that with out=buffer_create(1024,buffer_grow,1);
Finish by writing a single byte of 0, then seeking to the beginning of the buffer, then reading as buffer_string
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.
 

FrostyCat

Redemption Seeker
How do I do that? buffer_string is the way to go, but what do I put as 'value' in buffer_write?
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);
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.
What's so difficult about this?
Code:
var result = buffer_read(out, buffer_string);
buffer_delete(out);
return result;
 
N

NoFontNL

Guest
What's so difficult about this?
Code:
var result = buffer_read(out, buffer_string);
buffer_delete(out);
return result;
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?
 
D

dannyjenn

Guest
is there any alternative to this method?
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.
 
Top