Download

FAES Encryption Inconsistency?

I don’t know if I’m misunderstanding something or what, but I am getting unexpected results from FAES::EncryptData. I am getting 1) The same encrypted output for any input of the same length. E.g. four and tree will result in the same encrypted output. 2) The output for each input varies for each launching of the application. E.g. the input string “tree” might output “RlsHfblIY” (shortened for readability) but then when I relaunch the application, “tree” will have a different output other than “RlsHfblIY”. Most of this is pulled from a stackoverflow page iirc.

FString UTTDatabaseAgent::EncryptPassword(FString InPassword)
{
	if(InPassword.IsEmpty()) return InPassword;  //empty string? do nothing
	
 	InPassword.Append("salty");
	
	uint32 Size; 
	uint8 Count = InPassword.Len() % 3;
	for(int i = 0; i < Count; i++)
	{
		InPassword.Append("salty");
	}
 
	Size = InPassword.Len();
	Size = Size + (FAES::AESBlockSize - (Size % FAES::AESBlockSize));
 
	uint8* Blob = new uint8[Size]; //So once we calculated size we allocating space in memory 

	if( FString::ToBlob(InPassword,Blob,InPassword.Len())) {
		FString KeyStr = TEXT("7x!A%D*G-KaPdSgVkYp3s6v9y/B?E(H+");
		TCHAR *KeyTChar = KeyStr.GetCharArray().GetData();
		ANSICHAR *KeyAnsi = (ANSICHAR*)TCHAR_TO_ANSI(KeyTChar);
        // I was using a key for EncryptData but decided to try ANSICHAR*. 
        // Both had the same results unfortunately
		FAES::FAESKey Key = FAES::FAESKey();
		Key.Reset();
		for(int i = 0; i < KeyStr.Len(); i++)
		{
			Key.Key[i] = KeyStr[i];
		}
		if(!Key.IsValid())
		{
			UE_LOG(LogHttp, Error, TEXT("Password Encryption Failed: Key is not valid."))
			return InPassword;
		}
        // Tried this too. Didn't work either.
		// InPassword = FString::FromHexBlob(Blob,Size);
		FAES::EncryptData(Blob,Size,KeyAnsi); 
		InPassword = FBase64::Encode(Blob,Size); 
		delete Blob; 
		return InPassword; 
	}
	else
	{
		UE_LOG(LogHttp, Error, TEXT("Password Encryption Failed: Password cannot be coverted to blob."));
	}
	delete Blob; 
	return ""; 
}