(Using 4.8 Preview 4)
First of all, FAES::EncryptData and FAES:: DecryptData have no return type or non-const reference, so I assume the data of the given array will be modified itself?
If so, well, both functions don’t modify the data of the given array, the data of the array just stays the same.
What am I doing wrong?
My code:
TArray<uint8> UMyClass::EncryptAes(const TArray<uint8>& ByteArray, const FString& Key)
{
if((ByteArray.Num() <= 0) || (Key.Len() <= 0))
{
return ByteArray;
}
int32 Size = ByteArray.Num();
uint8* EncryptedBytes = new uint8[Size];
FMemory::Memcpy(EncryptedBytes, ByteArray.GetData(), Size);
const TCHAR* KeyChars = Key.GetCharArray().GetData();
FAES::EncryptData(EncryptedBytes, Size, TCHAR_TO_ANSI(KeyChars));
TArray<uint8> EncryptedByteArray;
EncryptedByteArray.Append(EncryptedBytes, Size);
delete EncryptedBytes;
return EncryptedByteArray;
}
TArray<uint8> UMyClass::DecryptAes(const TArray<uint8>& ByteArray, const FString& Key)
{
if((ByteArray.Num() <= 0) || (Key.Len() <= 0))
{
return ByteArray;
}
int32 Size = ByteArray.Num();
uint8* DecryptedBytes = new uint8[Size];
FMemory::Memcpy(DecryptedBytes, ByteArray.GetData(), Size);
const TCHAR* KeyChars = Key.GetCharArray().GetData();
FAES::DecryptData(DecryptedBytes, Size, TCHAR_TO_ANSI(KeyChars));
TArray<uint8> DecryptedByteArray;
DecryptedByteArray.Append(DecryptedBytes, Size);
delete DecryptedBytes;
return DecryptedByteArray;
}
Have you verified that AES_KEY is defined for your project? Otherwise the meat of those methods is #ifdef’d out and it’ll do nothing.
Hm no, I didn’t, I thought that the FAES class itself would do that.
Ehm, what is AES_KEY for? The method receives a key already as a ANSICHAR*? Those methods also never even use the AES_KEY?
It appears to be a define to enable/disable all the AES related functionality.
https://docs.unrealengine.com/latest/INT/Programming/UnrealBuildSystem/TargetFiles/index.html
In your <ProjectName>.Target.cs file, add the following to SetupGlobalEnvironment:
OutCPPEnvironmentConfiguration.Definitions.Add("AES_KEY=1");
Wow, thank you so much! I would have never been able to figure this one out!
One thing though, these 2 functions are inside a plugin, is it somehow possible to make this work inside a plugin, or does every user of the plugin has to add this line to their Target.cs file?
You should be able to specify it in the <PluginName>.Build.cs file, I think you just need to add
Definitions.Add("AES_KEY=1");
to the <PluginName> constructor.
Hm, nothing works…
I tried all of these things, one at a time and all at the same time:
- Definitions.Add(“AES_KEY=1”); in plugin build cs (Plugins/PluginName/Source/PluginName/)
- Definitions.Add(“AES_KEY=1”); in project build cs (Source/ProjectName/)
- OutCPPEnvironmentConfiguration.Definitions.Add(“AES_KEY=1”); in overridden SetupGlobalEnvironment in project build cs (Source/)
They all compile, but they all give the same output:
key : “5F4DCC3B5AA765D61D8327DEB882CF995F4DCC3B5AA765D61D8327DEB882CF99”
string : “3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738”
encrypted : “3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738”
decrypted : “3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738”
No errors or warnings, aside from the standard:
LogActor:Warning: GameSession /Game/Maps/UEDPIE_0_DefaultMap.DefaultMap:PersistentLevel.GameSession_4 has natively added scene component(s), but none of them were set as the actor’s RootComponent - picking one arbitrarily
LogActor:Warning: GameNetworkManager /Game/Maps/UEDPIE_0_DefaultMap.DefaultMap:PersistentLevel.GameNetworkManager_4 has natively added scene component(s), but none of them were set as the actor’s RootComponent - picking one arbitrarily
Anyway, what else could I still try?
Hmm, the next step would be to put a break point where you go to decrypt the key and actually step through the function to make sure everything is working as you expect.
EDIT: Also, did you make sure to regenerate your project solutions after you changed the build files?
With regenerate you mean “Refresh Visual Studio Project” in the UE4 editor, right?
Hmm, I haven’t done it that way before. I normally just right click on my UProject file and select “Generate Visual Studio project files”.
Ah, awesome, didn’t know it could be done like that as well.
Anyway, it’s 21:00 here, I’ll post the results of the debugging after I’ve tried it for a couple of hours tomorrow.
Hmm, well, I’ve tried again for a couple of hours, but I couldn’t get it to work.
I decided to just copy paste the FAES code and remove the AES_KEY nonsense. It now works:
key : “5F4DCC3B5AA765D61D8327DEB882CF995F4DCC3B5AA765D61D8327DEB882CF99”
string : “3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738”
encrypted : “8BD6DB651034835045ED630B3167FADA33DBEBDEAF961125C9C96C453BAD0733B896745C43033FB89A71CE0B831C36D69D7F9B1797DBC47AD12B4C33831C2C5CED523FF77DAFE52119E4FB31CE1F57528BD6DB651034835045ED630B3167FADA33DBEBDEAF961125C9C96C453BAD0733B896745C43033FB89A71CE0B831C36D6”
decrypted : “3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738”
I feel kinda bad about copy pasting it, since now I’ll have to constantly check if their FAES code changed, and if it did, then I’ll have to update my copy pasted classes as well, but at least it works now.
Ha, so it was the define. Interesting, I haven’t dug too deep into the Unreal build system so I’m not sure why that define wouldn’t be properly added (or maybe it was and you need to rebuild the engine source to pick up the change). Glad you got it figured out.