UE4 Fstring Encryption example using FAES

How does one Encrypt/Decrypt an FString with standard FAES::Encrypt() / FAES::Decrypt() functionality?

I am attempting to send some data to a score keeping server, and need to hide the plain text.

It’s quite easy then it looks

static void EncryptData
uint8 * Contents,
uint32 NumBytes,

So in Contents you place pointer to raw byte array (thats why it’s uint8*), NumBytes you place number of bytes you inputed and in Key, you place key. I think you need to generate AES keys first if im not mistaken.

So key to use this function is to how convert specific type to uint8* byte array… well FString has functions for that :slight_smile:

So we can make function

FString USomeClass::Encrypt(FString String) {

  if(String.IsEmpty()) return String;  //empty string? do nothing

  uint8* Blob; //we declere uint8 pointer
  uint32 Size; //for size calculation

 //first we need to calculate the size of array, encrypted data will be processed in blocks so
 //data size need to be aligned with block size

Size = String.Len();
Size = Size + (FAES::AESBlockSize - (Size % FAES::AESBlockSize))

  Blob = new uint8[Size]; //So once we calculated size we allocating space in memory 
                                            //which we use for encryption

  //We filling allocated space with string to process
  if( FString::ToBlob(String,Blob,String.Len())) { 
             FAES::EncryptData(Blob,Size,Key??); //We encrypt the data, don't know how you want to input key
             String = FString::FromHexBlob(Blob,Size); //now generate hex string of encrypted data
             delete Blob; //deleting allocation for safety
             return String; //and return it
  delete Blob; //deleting allocation for safety
  return ""; //If failed return empty string


Decryption is same way just you start with ToHexBlob insted ToBlob and at the end FromBlob insted of FromHexBlob and ofcorse you use decrypt function.

I coded this blindly so it’s untested and there might be error. but this is how you do it, something along those lines


Thank you! This helped a lot. I also came to realize that public encryption will work better for my purposes. So now I have to find such a library.

For anyone still looking at this question, I’ve since packaged a popular, audited encryption library - libsodium as UE4 plugin. GitHub - maxenko/SodiumUE4: An easy to use cryptography plugin for Unreal Engine 4 based on libsodium.

There is support for public and private encryption, and its really easy to use, from C++ or BP

1 Like