Skip to content

Instantly share code, notes, and snippets.

@Creta5164
Last active July 25, 2020 21:15
Show Gist options
  • Save Creta5164/0926345fcc727521450f439432f73314 to your computer and use it in GitHub Desktop.
Save Creta5164/0926345fcc727521450f439432f73314 to your computer and use it in GitHub Desktop.
An obfuscation class that uses the simplest way to prevent arbitrary modification of strings.
//StringObfuscator.gmlclass
//This is port of my .NET version of StringObfuscator.cs
//https://gist.github.com/Creta5164/7efb69bb9414a556169a5c8782923a67
//-----------------------------------------------------------------------------
/// @function StringObfuscator_Convert(textData)
/// @description Make string to obfuscated binary.
/// @param {string} textData String data that convert obfuscated binary.
var textData = argument0;
if (!is_string(textData))
return noone;
var MAX = 256;
var length = string_byte_length(textData);
var text = buffer_create(length, buffer_fixed, 1);
buffer_write(text, buffer_text, textData);
buffer_seek(text, buffer_seek_start, 0);
var result = buffer_create(length + 1, buffer_fixed, 1);
//To obfuscate without interfering with the in-game random seed.
var seedCache = random_get_seed();
randomize();
var key = $7F + irandom($7F);
random_set_seed(seedCache);
buffer_write(result, buffer_u8, key);
var char;
for (var i = 0; i < length; i++) {
char = buffer_read(text, buffer_u8);
buffer_write(
result, buffer_u8,
(char + key) mod MAX
);
}
buffer_delete(text);
return result;
//-----------------------------------------------------------------------------
/// @function StringObfuscator_Parse(bufferData)
/// @description Make obfuscated binary string to source.
/// @param {buffer} bufferData Obfuscated binary's buffer.
var bufferData = argument0;
if (!buffer_exists(bufferData))
return noone;
var MAX = 256;
buffer_seek(bufferData, buffer_seek_start, 0);
var length = buffer_get_size(bufferData);
var result = buffer_create(length - 1, buffer_fixed, 1);
var key = buffer_read(bufferData, buffer_u8);
for (var i = 0; i < length - 1; i++) {
buffer_write(
result, buffer_u8,
(buffer_read(bufferData, buffer_u8) - key) mod MAX
);
}
buffer_seek(result, buffer_seek_start, 0);
return result;
//-----------------------------------------------------------------------------
//Example code
//Make 'Hello, world' string to obfuscated binary and recovers it to string.
var buffer = StringObfuscator_Convert("Hello, world!");
buffer_save(buffer, "obfuscated.dat");
var parse = StringObfuscator_Parse(buffer);
show_debug_message("Parsed text : " + buffer_read(parse, buffer_text));
buffer_save(parse, "parse.txt");
//Dispose buffers.
buffer_delete(buffer);
buffer_delete(parse);
@Creta5164
Copy link
Author

StringObfuscator for gms2

@Creta5164
Copy link
Author

Highly recommended to organize like this :

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment