UE4.26调用CryptoPP8.6库实现加密解密

本文档详细介绍了如何在UE4项目中集成和使用最新版本的Crypto++库,包括环境配置、库文件的下载、编译、拷贝、库引入以及创建蓝图接口调用加密解密函数。通过遵循步骤,可以解决旧版本CryptoPP库存在的问题,并在UE4中实现AES加密的多种工作模式。


前言

在UE4项目中用到CryptoPP库进行加密解密,但UE4内置CryptoPP库只有Crypto++5.65、Crypto++5.62两个版本,使用过程中会出现好多漏洞,这在新版本已经修复,所以只能升级到最新版Crypto++8.6,本文主要使用Crypto++8.6编译的lib库,在unreal中调用使用。


一、环境

win10
Crypto++ 8.6 (Crypto++下载地址)
unreal 4.26.2
vs2017

二、使用步骤

1.下载Crypto++ 8.6并打开

任意选择一个版本下载,这里选择美国原版。
在这里插入图片描述
下载以后解压到文件夹中,使用VS打开工程文件cryptest.sln
在这里插入图片描述
打开CryptoPP工程文件,会发现有四个子项目:

  • cryptdll - 生成cryptopp.dll动态库
  • dlltest - 用来测试cryptopp.dll,依赖cryptdll项目
  • cryptlib - 生成cryptlib.lib静态库
  • cryptest - 用来测试cryptopp,依赖cryptlib项目
    所以,我们有两种使用CryptoPP方法,一种是静态链接(MT),还有一种是动态链接(MD),使用对应的工程编译即可。因为UE4默认使用动态编译,我们下文以动态链接为例。
    在这里插入图片描述

2.编译cryptlib

右键cryptest和crylib属性,C/C++ ——> 代码生成 ——> 运行库 设置为 DLL(/MD)
这里写图片描述

选择 release x64编译
可以在“cryptopp860\x64\Output\Release”路径下找到cryptlib.lib
下文开始测试使用CryptoPP,实际使用中,如果功能上逻辑上需要修改,可以参考上文测试工程中的示例程序,以及官方文档。下文编译如果报告XX重复定义等错误,请检查LIB库工程和本测试工程的:C/C++ ——> 代码生成 ——> 运行库是否统一。

3.为项目拷贝必要文件

  • 使用UE4现有工程或新建一个工程,打开工程目录,
  • 将 cryptlib.lib拷贝到“工程名XXX\ThirdParty\crypto\lib\Win64\”,如果没有文件夹,请自行建立。
  • 将cryptopp860源代码的.h头文件全部拷贝到“工程名XXX\ThirdParty\crypto\include\Win64\”,如果没有文件夹,请自行建立。

4.将库引入到项目中

UE4模块引入第三方库,只要在Build.cs中配置头文件路径及添加lib文件即可。
在这里插入图片描述

//~~~和之前一样,此处代码不变
	DynamicallyLoadedModuleNames.AddRange(
		new string[]
		{
   
   
			// ... add any modules that your module loads dynamically here ...
		}
		);
	//此处开始配置第三方库crypto,这里的路径是工程目录,一定跟工程目录吻合,否则会各种报错。
        string cryptoPath = ModuleDirectory + "../../../ThirdParty/crypto/";
        if (Target.Platform == UnrealTargetPlatform.Win64)
        {
   
   
        	string platform = "/Win64/";
        	PublicIncludePaths.Add(cryptoPath + "include" + platform);
        	// PublicLibraryPaths.Add(cryptoPath + "lib" + platform);	//使用这个方法会警告
        	PublicSystemLibraryPaths.Add(cryptoPath + "lib" + platform);
        	PublicAdditionalLibraries.Add("cryptlib.lib");
            bUseRTTI = true;	//不加这行,解密的时候会报错
        }

虽然添加了十几行代码,但重要的代码就三行

PublicIncludePaths.Add(cryptoPath + “include” + platform); //此行是把(.h)头文件路径引入。
PublicLibraryPaths.Add(cryptoPath + “lib” + platform); //此行是把(.lib)库文件路径引入。
PublicAdditionalLibraries.Add(“cryptoPath .lib”); //此行是把(.lib)库文件引入。
之后就可以在你的代码中#include 第三方的(.h)文件,使用其中的功能了。

5.创建UE4接口文件

  • UE4中添加C++文件,继承于BlueprintFunctionLibrary类

在这里插入图片描述

  • 命名为AESBPLibrary

在这里插入图片描述

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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "AESBPLibrary.generated.h"

/**
 * 
 */
UCLASS()
class PRO13_API UAESBPLibrary : public UBlueprintFunctionLibrary
{
   
   
	GENERATED_UCLASS_BODY()

	///ECB mode
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "ECB_AESEncryptData_H", Keywords = "ECB_AESEncryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF"), Category = "AES")
        static FString ECB_AESEncryptData(FString aes_content, FString aes_key);

    UFUNCTION(BlueprintCallable, meta = (DisplayName = "ECB_AESDecryptData_H", Keywords = "ECB_AESDecryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF"), Category = "AES")
        static FString ECB_AESDecryptData(FString aes_content, FString aes_key, bool & result);

    ///CBC mode
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CBC_AESEncryptData_H", Keywords = "CBC_AESEncryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CBC_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV);

    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CBC_AESDecryptData", Keywords = "CBC_AESDecryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CBC_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result);

    ///CBC_CTS mode
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CBC_CTS_AESEncryptData", Keywords = "CBC_CTS_AESEncryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CBC_CTS_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV);

    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CBC_CTS_AESDecryptData", Keywords = "CBC_CTS_AESDecryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CBC_CTS_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result);

    ///CFB mode
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CFB_AESEncryptData", Keywords = "CFB_AESEncryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CFB_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV);

    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CFB_AESDecryptData", Keywords = "CFB_AESDecryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CFB_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result);

    ///OFB mode
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "OFB_AESEncryptData", Keywords = "OFB_AESEncryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString OFB_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV);

    UFUNCTION(BlueprintCallable, meta = (DisplayName = "OFB_AESDecryptData", Keywords = "OFB_AESDecryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString OFB_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result);

    ///CTR mode
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CTR_AESEncryptData", Keywords = "CTR_AESEncryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CTR_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV);

    UFUNCTION(BlueprintCallable, meta = (DisplayName = "CTR_AESDecryptData", Keywords = "CTR_AESDecryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF", aes_IV = "ABCDEF0123456789"), Category = "AES")
        static FString CTR_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result);

};
  • AESBPLibrary.cpp
// Fill out your copyright notice in the Description page of Project Settings.


#include "AESBPLibrary.h"

#include "string.h"
//这里使用工程目录,不是UE4安装目录
#include "../../../ThirdParty/crypto/include/Win64/aes.h"
#include "../../../ThirdParty/crypto/include/Win64/hex.h"
#include "../../../ThirdParty/crypto/include/Win64/modes.h"

using namespace std;
using namespace CryptoPP;




UAESBPLibrary::UAESBPLibrary(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
   
   

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值