FAES::DecryptData function is encrypting data instead of decrypting

Both FAES::DecryptData and FAES::EncryptData functions are behaving in the same way. Both of them are encrypting data and giving exactly the same outputs from same inputs. I can’t decrypt encrypted data.

Look at the codes. Note that “Reverse” boolean activates decryption mode.

in UMyBlueprintFunctionLibrary.h:

UFUNCTION(BlueprintCallable, BlueprintPure, meta = (DisplayName = "Encrypt with key (AES)", Keywords = "encrypt aes faes"), Category = "CPP Native Functions - ")
			static FString EncryptWithAES(FString InputString, bool Reverse);

in UMyBlueprintFunctionLibrary.cpp:

FString UMyBlueprintFunctionLibrary::EncryptWithAES(FString InputString, bool Reverse)
{	
	int32 Size = InputString.Len(); // Calculates length of the input string

	TCHAR *String = InputString.GetCharArray().GetData();	// Turn input string...
	uint8* BytesString = (uint8*)(String);					// ...into byte array

	FString Key = "e";											// Choose a key then...
	TCHAR *KeyTChar = Key.GetCharArray().GetData();			// ...turn key string...
	ANSICHAR *KeyAnsi = (ANSICHAR*)TCHAR_TO_ANSI(KeyTChar); // ...into ANSICHAR array.
	
	if (Reverse)	FAES::DecryptData(BytesString, Size, KeyAnsi); // Decrypt or...
	else			FAES::EncryptData(BytesString, Size, KeyAnsi); // encrypt.

	TArray<uint8> EncryptedByteArray;						// Define a new array to store the output data
	EncryptedByteArray.Append(BytesString, Size);			// Move output of the FAES functions to this array
	FString output = FBase64::Encode(EncryptedByteArray);	// Turn array into FString
	return output;
}

Hey -

I have entered the bug report UE-32368 for the DecryptData() function not actually decrypting the input string.

Cheers

Your project needs to have AES_KEY macro defined in order to be able to encrypt anything with AES. We do not define in on purpose because otherwise anyone who has access to UE4 source code would be able to decrypt your data. Have a look at AES.cpp. The macro should be either hardcoded in source code or defined in your target.cs files.

Hi,
I debated opening a new question but my query is relevant to this answer thread post (and this post comes up when searching google).

I’m somewhat lacking in a lot of c++ knowledge, so please could you tell me how you define the AES_KEY macro in the target.cs file as I couldn’t find any answer in my searches.

Also, I’ll admit to being a bit confused, does AES_KEY only work if the engine is built from source and modified or can it be defined in a standard C++ project target.cs file without any engine source code modifications (which is what I’m hoping for)?

Thanks.

Hey -

You should be able to define the key you want to use for encryption by adding Definitions.Add(“AES_KEY=[SpecificKey]”); to the ProjectName.Build.cs file and then regenerate project solution files. This should work for a binary project from the launcher without source modifications.

Hi UE4 Devs!
Why do I get error CS0103?
\Epic Games\MyProject_1\Source\MyProject_1.Target.cs(13,9) : error CS0103: ??? “Definitions” ??? ? ⥪?饬 ???⥪???

They still can and the method is exactly the same as if you defined it yourself: you find a function that takes this AES key to encrypt or decrypt and then you see how it gets called, and you most probably get a static and not encrypted C-style string embedded in the executable. Or if they encrypt that, you attach a debugger and get the raw C-style string at runtime.

Why even bother, it feels like it was a waste of time to implement this feature. Unless it’s just for managers, so they can tell other managers that Unreal Engine protects its’ data assets with AES-256 encryption :slight_smile: