Issue with writing out AES encrypted file with SaveStringToFile()

In our project we use FAES encryption/decryption to save out user information to a local file tagged with .ejs. Every once in a while, if the file becomes a sufficient size, the SaveStringToFile function will not serialize the String properly and thus on reading back from the file, the contents are not read properly. The particular issue I have found is in the following line.

else if (SaveAsUnicode)
{
	UCS2CHAR BOM = UNICODE_BOM;
	Ar->Serialize( &BOM, sizeof(UCS2CHAR) );

	auto Src = StringCast<UCS2CHAR>(StrPtr, String.Len());
	Ar->Serialize( (UCS2CHAR*)Src.Get(), Src.Length() * sizeof(UCS2CHAR) );
}

The second variable is passed into the Serialize function as a much smaller value than is should be and causes the encrypted String to not write to the file properly.

My first concern would be whether your encryption process could be generated a null terminator. The string then thinks that is the end of the string, even though you have arbitrarily more data that is supposed to be stored.

Would that affect the length of the Src variable that’s being passed into the Serializer? I didn’t think the null terminator would alter the physical size of the string.

Sorry I was under the impression the error was happening when the encrypted string is being ready back in. You can verify that the string being saved out is not being fully saved?

I can. Before calling the StringtoFile function the string is the correct size, but somewhere between casting and passing into Serialize it loses about 100KB of data.

Are you able to step into the StringCast and Serialize calls? You may need to build with DebugEditor configuration but that should allow you step through the engine code and maybe you can catch when the error occurs. I’d need a reproduction project with your code and data to test here to catch it myself.

After setting up the DebugEditor config I was not able to step into the StringCast function. The only real information I was able to ascertain is that the String being cast has a size of 247969. On the instances it does not break, the Serialize function reads a passed length of 343296 or so and on the instances it fails it reads a passed length of 247904. To me it seems to be an issue happening during casting but I was unable to get under the hood of that function.

AES Plugin: