Last active
July 25, 2020 21:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Highly recommended to organize like this :