Encrypted string always becomes malformed

I have created an encryption function that will turn any string into a scrambled set of characters and can be reversed back to its normal state.

The problem I am having is the string will sometimes turn into a “malformed” string; and will be cut off at some point as seen below.

What I have tried so far is replacing special characters with an identifying phrase with it’s byte value attached, and it seems to be successful. But in the screenshot above there are no special characters present so I am unsure what is causing it to become malformed. (However, the “�” character seems to not be replaced)

function replaceSpecialChar(checkChar)
	local checkByte = string.byte(checkChar)
	local specials = {'\\', '$', '^', '*', '(', '%)', '.', '%[', '%]', '+', '-', '?', '"', "%%", "�"}
	for _, char in specials do
		local byte = string.byte(char)
		if checkByte == byte then
			return checkByte..";prefixGSUBBEDbyte"
		end
	end
	return checkChar
end

Final note:
Every few encryptions will come out fine:

What character is causing it to do this? The problem here seems to be that the “�” will not be replaced via gsub.

Probably this character since roblox stops recognizing the text as a string after the character
image

Thanks for the reply.

The problem here is that the “�” symbol is the same symbol as the one before and after it, so if it were the problem it would be cutoff earlier or later, It’s also the same one used in the photo where the string is seemingly fine. I have confirmed this by using string.byte and all of them coming up byte 239

I assumed this was the problem anyways and tried using string.gsub to replace it and for some reason it just doesn’t want to be detected by gsub. Any idea why?

Wouldn’t this just be an IDE issue? I believe it will return just fine, hovering over the red line might just give a warning.

Trying to require the module or use the string errors the script with this:
image

Same message when you hover over it in the script editor

Use the builtin string.format option to escape characters.

print(string.format("%q", "a\nbcdef\\"))

That’s not true, this is a placeholder symbol for when the script editor doesn’t know how to represent the character. So two symbols can look the same because they’re unrecognizable, but still not be equal.

Also if whatever you’re trying to encrypt isn’t large(such as simple text or an image), you can just represent it using a safer encoding, such as base64(if I’m not mistaken base64 will increase the string length by 33.3%).

I have checked each of these byte values and all of them are 239

image

And for some reason it still gets cut off.
I have explored alternative ways of having the character encoded but I really like how these symbols look lmao. I understand the easy solution is to switch to the alternative and I might just do that now, but I still need to understand and learn why these characters are always causing the string to become malformed. Also that byte 239 character will not be detected by gsub for some reason.

To add, it isn’t just this character, but multiple


image
image

Is this a bug?