GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-12-12 06:11:17

PlasticineGuy
Member
Registered: 2009-12-12
Posts: 4

Encrypt/decrypt string and file script

Expand/*
** Usage:
**     my_str = crypt(str)
**
** Arguments:
**     str        String to encrypt, string
**
** Returns:
**     my_str     Result of encryption, string
**
** Notes:
**     This script works for both encryption and decryption.
**
** GMLscripts.com
*/
{
    var str, str2, i;
    str = argument0;
    str2 = '';
    for(i = 1; i <= string_length(str); i += 1) {
        str2 += chr(~ord(string_char_at(str, i)));
    }
    return str2;
}

The above is a simple encryption script using the bitwise NOT operator designed to be easy to use but effective.
Input: PlasticineGuy
Output: ¯“žŒ‹–œ–‘š¸Š†

I have also designed a file encryption script:

Expand/*
** Usage:
**     file_crypt(file)
**
** Arguments:
**     file       String to encrypt, file ID
**
** Returns:
**     1          Returns a value to avoid odd behaviour described by Xot.
**
** Notes:
**     This script works for both encryption and decryption.
**
** GMLscripts.com
*/
{
    var buffer, buffer2, fp, fn, i;
    buffer = "";
    buffer2 = "";
    fn = argument0;
    fp = file_bin_open(fn, 2);
    while(file_bin_position(fp) != file_bin_size(fp)){
        buffer += chr(file_bin_read_byte(fp));
    }
    file_bin_close(fp);
    for(i = 1; i <= string_length(buffer); i += 1) {
        buffer2 += chr(~ord(string_char_at(buffer, i)));
    }
    fp = file_text_open_write(fn);
    file_text_write_string(fp, buffer2);
    file_text_close(fp);
    return 1;
}

EDIT: Fixed to follow specification.

Last edited by PlasticineGuy (2009-12-12 06:12:28)

Offline

#2 2009-12-14 09:47:49

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

Re: Encrypt/decrypt string and file script

You could almost say this is NOT encryption at all! (hur, hur)

I guess it wouldn't hurt to have some other encryption scripts to choose from. The only thing is, this is just a special case of XOR encryption, which I think is already in the submission queue. But you do have the file handling, so maybe I'll combine the scripts.

I wonder if the file handling could be improved.


Abusing forum power since 1986.

Offline

#3 2009-12-15 00:56:34

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Encrypt/decrypt string and file script

Yes, if dealing with a text file as un encrypted source...

Also, while not eof should be used expecially if file_bin_size does a seak to end and seek back to calculate the sise.

Stuff you result in a variable instead

size  = get file size
while at < size
{
at+=1; //instead of get file position...
}

But yeah. many ways to do this.


Also, this may fail for huge files. a byte by byte could be done for large file (slow thought)
open source file
opten dest file
while not eof (source)
{
read source byte
encrypt/decripte
write dest byte
}
close source file
close dest file

Last edited by icuurd12b42 (2009-12-15 00:57:48)

Offline

#4 2009-12-15 08:01:53

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

Re: Encrypt/decrypt string and file script

If you are concerned about the limits of strings, they can hold about 500MB of data. Anything more than a few thousand characters should probably be handled with a DLL unless speed isn't an issue.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB