Can not load encrypted pak files

/////////////////////////////////////////updated at 07/28/2016

EncyptedPakTest.Build.cs:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "PakFile" });

PrivateDependencyModuleNames.AddRange(new string[] {  });

EncryptedPakTestActor.h:

#pragma once
#include "IPlatFormFilePak.h"
#include "GameFramework/Actor.h"
#include "EncryptedPakTestActor.generated.h"

UCLASS()
class ENCRYPTEDPAKTEST_API AEncryptedPakTestActor : public AActor
{
	GENERATED_BODY()
	
public:	
	AEncryptedPakTestActor();
	virtual void BeginPlay() override;
	virtual void Tick( float DeltaSeconds ) override;
private:
	void Test();
	void Load(const FString & pakName);
private:
	FPakPlatformFile * m_PakPlatformFile;
	FStreamableManager * m_Stream;
};

EncryptedPakTestActor.cpp

#include "EncryptedPakTest.h"
#include "EncryptedPakTestActor.h"

AEncryptedPakTestActor::AEncryptedPakTestActor()
	: m_PakPlatformFile(new FPakPlatformFile)
	, m_Stream(new FStreamableManager) {

	PrimaryActorTick.bCanEverTick = true;

	IPlatformFile& InnerPlatform = FPlatformFileManager::Get().GetPlatformFile();
	m_PakPlatformFile->Initialize(&InnerPlatform, TEXT(""));
	FPlatformFileManager::Get().SetPlatformFile(*m_PakPlatformFile);
}

void AEncryptedPakTestActor::Load(const FString & pakName) {
	auto pakPath = FPaths::GameContentDir() + pakName + ".pak";
	auto mountingPath = FPaths::EngineContentDir() + pakName + "/";
	m_PakPlatformFile->Mount(*pakPath, 0, *mountingPath);
	TSet<FString> fileList;
	FPakFile PakFile(*pakPath, false);
	PakFile.FindFilesAtPath(fileList, *PakFile.GetMountPoint(), true, false, true);
	for (auto & ele : fileList) {
		FString assetShortName = FPackageName::GetShortName(ele);
		assetShortName.RemoveFromEnd(FPackageName::GetAssetPackageExtension());
		ele = "/Engine/" + pakName + "/" + assetShortName + TEXT(".") + assetShortName;
	}
	for (const auto & target : fileList) {
		m_Stream->SynchronousLoad(target);
	}
}

void AEncryptedPakTestActor::Test() {
	Load("0a2e3bc4f1a811e5a15500163e0018da");
	Load("0aaa19c4a5ab11e3a0e100163e18af41");
}

void AEncryptedPakTestActor::BeginPlay()
{
	Super::BeginPlay();
	Test();
}

void AEncryptedPakTestActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
}

////////////////////////////////////////////////////original

I use UnrealPak.exe to encrypt my uassets which are loaded at runtime , the encrypt command is something like

UnreakPak.exe  targetPak.pak -create="directory of uassets" -encrypt -compress

I defined AES_KEY in file Core.Build.cs:

Definitions.Add("AES_KEY=\"128bit key\"");

then I use the following code to mount Pak files:

m_PakPlatform = new FPakPlatformFile;
IPlatformFile& InnerPlatform = FPlatformFileManager::Get().GetPlatformFile();
m_PakPlatform->Initialize(&InnerPlatform, TEXT(""));
FPlatformFileManager::Get().SetPlatformFile(*m_PakPlatform);

m_PakPlatform->Mount(*pakFilePath, 0, *mountingPath))

then load uasset:

TSet<FString> fileList;
PakFile.FindFilesAtPath(fileList, *PakFile.GetMountPoint(), true, false, true);
FStreamableManager * m_Stream = new FStreamableManager ;

for (const auto & file : fileList) {
    m_Stream->SynchronousLoad(file );
}

the output is

material1.uasset’ contains unrecognizable data, check that it is of the expected type

when I remove the ‘-encrypt’ option, everything works fine.

can anyone help me .

加载之后,应该怎么调用呢?,例如调用一个StaticMeth

在actor中 设置一个MeshComponent, 然后直接用SetStaticMesh 就行了

Hey noobersh,

Can you update your post to show the classes you have created to do this, including the header, #include statements, and what is in your GAMENAME.Build.cs?

hey, Kyle, thanks for your reply!
I have updated my post, and supplied code you need。

Hey nooberfsh,

I believe the issue is that your AES key is simply too short. The key is always checked to make sure that it is at least 32 characters long. The key you are using “128 bit key” doesn’t meet this requirement.

In the Core.Build.cs, I used

Definitions.Add("AES_KEY=\"pnvh}XPG!*v6L{)@fbQrH($L-7gBw2g4\"");

I was able to load the files into the “Stream”:

Hey :
actually my AES_KEY is 128 bit long, and I used your key for testing, it was not working either.
Files can be loaded into the “Stream”, but “Stream” can not recognize the data in the files which was the main problem.

were u able to solve it ?
and are u facing any crash during decryption ?

yes.
yes.
solution: see pic.

131874-123.png

were u able to make it work in 4.14.3 ?