FAES encryption.

I’m not getting the FAES encryption to work :frowning:


void UMessageSender::BeginPlay()

	// connect to the message bus. this will return nullptr when something fails.
	TestEndpoint = FMessageEndpoint::Builder("TestEndpointName").Build();
	// TODO: appropriate error handling if something goes wrong :)	

	UE_LOG(LogTemp, Warning, TEXT("Normal: %s"), *encrpytedStr2);
	encrpytedStr2 = EncryptWithAES(*encrpytedStr2, false);

	UE_LOG(LogTemp, Warning, TEXT("Encrypted: %s"), *encrpytedStr2);

	FString decry = EncryptWithAES(encrpytedStr2, true);

	UE_LOG(LogTemp, Warning, TEXT("Decrypted: %s"), *decry);


FString UMessageSender::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 = "1";                                            // 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;


// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;
using System.Collections.Generic;

public class ProductSenderEditorTarget : TargetRules
	public ProductSenderEditorTarget(TargetInfo Target)
		Type = TargetType.Editor;

	// TargetRules interface.

	public override void SetupBinaries(
		TargetInfo Target,
		ref List<UEBuildBinaryConfiguration> OutBuildBinaryConfigurations,
		ref List<string> OutExtraModuleNames
		OutExtraModuleNames.AddRange( new string] { "ProductSender" } );
    public override void SetupGlobalEnvironment(
        TargetInfo Target,
        ref LinkEnvironmentConfiguration OutLinkEnvironmentConfiguration,
        ref CPPEnvironmentConfiguration OutCPPEnvironmentConfiguration


My results:

LogTemp:Warning: Normal: waazzaaa
LogTemp:Warning: Encrypted: dwBhAGEAegA=
LogTemp:Warning: Decrypted: ZAB3AEIAaABBAEcA

Does anyone have a good example for me or a little video tutorial?
Thank you :slight_smile:

I see you using FBase64::Encode() at the end of EncryptWithAES(), but no decode. Try decoding it prior decrypting, when using Reverse == true :wink:

I still do not get it working :frowning:
Could you give me a example?
Thank you.

Getting encryption right requires a lot of knowledge about cryptography, in addition to knowing about data representation.

I notice that you are turning the TCHAR array into a byte pointer.
This will not be compatible if you try to talk between different environment with different sizeof(TCHAR).
It also won’t work if you talk between different environments with different byte order.

I also notice that you don’t do any Initialization Vector management.
This means that your encryption will be trivially broken using a known-plaintext attack.
You should, at the very minimum, use something like CTR cipher mode, and include the block number in the encrypted message.
There are also other modes that are appropriate for encrypting potentially lossy network messages; that’s where encryption knowledge is necessary.

Finally, it’s not clear to me what you’re trying to defend against. A typical computer game cheater will cheat by attaching to the process on their computer, which already has all the decrypted data available.

I suggest that you don’t encrypt your data for now. If you find that you actually need encryption to defend against some particular kind of attack, then I suggest you find someone who is actually a skilled cryptography implementer to implement it for you. Crypto requires just one small bug or flawed choice, to become almost entirely worthless, and those not trained in the practice, usually don’t even know what flaws they have, and thus are doomed to never ship actually-working crypto.

Thank you jwatte for explaination.
I am sending messages over the network so I want it encrypted that people cannot see the data that is being send.
I want to learn encryption but I do not know where to start or what stept to follow to achieve the knowledge could you guide me in a direction for it?

You might try the book ‘Cryptography for Developers’ by Tom St. Denis. It gives a pretty good introduction to cryptography in general and has a chapter on AES. Otherwise, research the Internet. I haven’t used UE4’s implementation myself.