前言
在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[]
{
}
);
string cryptoPath = ModuleDirectory + "../../../ThirdParty/crypto/";
if (Target.Platform == UnrealTargetPlatform.Win64)
{
string platform = "/Win64/";
PublicIncludePaths.Add(cryptoPath + "include" + 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类
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "AESBPLibrary.generated.h"
UCLASS()
class PRO13_API UAESBPLibrary : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
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);
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);
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);
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);
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);
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);
};
#include "AESBPLibrary.h"
#include "string.h"
#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)
{
}
#pragma region ECB
FString UAESBPLibrary::ECB_AESEncryptData(FString aes_content, FString aes_key)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
const char *plainText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);
ECB_Mode_ExternalCipher::Encryption ecbEncryption(aesEncryption);
StreamTransformationFilter ecbEncryptor(
ecbEncryption,
new HexEncoder(new StringSink(outstr)),
BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING
);
ecbEncryptor.Put((byte *)plainText, strlen(plainText));
ecbEncryptor.MessageEnd();
return UTF8_TO_TCHAR(outstr.c_str());
}
FString UAESBPLibrary::ECB_AESDecryptData(FString aes_content, FString aes_key, bool & result)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
const char *cipherText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
try
{
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
ECB_Mode<AES >::Decryption ecbDecryption((byte *)key, AES::MAX_KEYLENGTH);
HexDecoder decryptor(new StreamTransformationFilter(ecbDecryption, new StringSink(outstr), BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING
));
decryptor.Put((byte *)cipherText, strlen(cipherText));
decryptor.MessageEnd();
result = true;
}
catch (const std::exception& e)
{
outstr = "error";
outstr = e.what();
UE_LOG(LogTemp, Error, TEXT("ECB_AESDecryptData failed!"));
result = false;
}
return UTF8_TO_TCHAR(outstr.c_str());
}
#pragma endregion
#pragma region CBC
FString UAESBPLibrary::CBC_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *plainText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);
CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
StreamTransformationFilter cbcEncryptor(cbcEncryption, new HexEncoder(new StringSink(outstr)), BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING);
cbcEncryptor.Put((byte *)plainText, strlen(plainText));
cbcEncryptor.MessageEnd();
return UTF8_TO_TCHAR(outstr.c_str());
}
FString UAESBPLibrary::CBC_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *cipherText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
try
{
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
CBC_Mode<AES >::Decryption cbcDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);
HexDecoder decryptor(new StreamTransformationFilter(cbcDecryption, new StringSink(outstr), BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING));
decryptor.Put((byte *)cipherText, strlen(cipherText));
decryptor.MessageEnd();
result = true;
}
catch (const std::exception&)
{
outstr = "error";
UE_LOG(LogTemp, Error, TEXT("CBC_AESDecryptData failed!"));
result = false;
}
return UTF8_TO_TCHAR(outstr.c_str());
}
#pragma endregion
#pragma region CBC_CTS
FString UAESBPLibrary::CBC_CTS_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *plainText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);
CBC_CTS_Mode_ExternalCipher::Encryption cbcctsEncryption(aesEncryption, iv);
StreamTransformationFilter cbcctsEncryptor(cbcctsEncryption, new HexEncoder(new StringSink(outstr)), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING);
cbcctsEncryptor.Put((byte *)plainText, strlen(plainText));
cbcctsEncryptor.MessageEnd();
return UTF8_TO_TCHAR(outstr.c_str());
}
FString UAESBPLibrary::CBC_CTS_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *cipherText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
try
{
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
CBC_CTS_Mode<AES >::Decryption cbcctsDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);
HexDecoder decryptor(new StreamTransformationFilter(cbcctsDecryption, new StringSink(outstr), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING));
decryptor.Put((byte *)cipherText, strlen(cipherText));
decryptor.MessageEnd();
result = true;
}
catch (const std::exception&)
{
outstr = "error";
UE_LOG(LogTemp, Error, TEXT("CBC_CTS_AESDecryptData failed!"));
result = false;
}
return UTF8_TO_TCHAR(outstr.c_str());
}
#pragma endregion
#pragma region CFB
FString UAESBPLibrary::CFB_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *plainText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);
CFB_Mode_ExternalCipher::Encryption cfbEncryption(aesEncryption, iv);
StreamTransformationFilter cfbEncryptor(cfbEncryption, new HexEncoder(new StringSink(outstr)), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING);
cfbEncryptor.Put((byte *)plainText, strlen(plainText));
cfbEncryptor.MessageEnd();
return UTF8_TO_TCHAR(outstr.c_str());
}
FString UAESBPLibrary::CFB_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *cipherText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
try
{
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
CFB_Mode<AES >::Decryption cfbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);
HexDecoder decryptor(new StreamTransformationFilter(cfbDecryption, new StringSink(outstr), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING));
decryptor.Put((byte *)cipherText, strlen(cipherText));
decryptor.MessageEnd();
result = true;
}
catch (const std::exception&)
{
outstr = "error";
UE_LOG(LogTemp, Error, TEXT("CFB_AESDecryptData failed!"));
result = false;
}
return UTF8_TO_TCHAR(outstr.c_str());
}
#pragma endregion
#pragma region OFB
FString UAESBPLibrary::OFB_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *plainText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);
OFB_Mode_ExternalCipher::Encryption ofbEncryption(aesEncryption, iv);
StreamTransformationFilter ofbEncryptor(ofbEncryption, new HexEncoder(new StringSink(outstr)), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING);
ofbEncryptor.Put((byte *)plainText, strlen(plainText));
ofbEncryptor.MessageEnd();
return UTF8_TO_TCHAR(outstr.c_str());
}
FString UAESBPLibrary::OFB_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *cipherText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
try
{
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
OFB_Mode<AES >::Decryption ofbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);
HexDecoder decryptor(new StreamTransformationFilter(ofbDecryption, new StringSink(outstr), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING));
decryptor.Put((byte *)cipherText, strlen(cipherText));
decryptor.MessageEnd();
result = true;
}
catch (const std::exception&)
{
outstr = "error";
UE_LOG(LogTemp, Error, TEXT("OFB_AESDecryptData failed!"));
result = false;
}
return UTF8_TO_TCHAR(outstr.c_str());
}
#pragma endregion
#pragma region CTR
FString UAESBPLibrary::CTR_AESEncryptData(FString aes_content, FString aes_key, FString aes_IV)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *plainText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);
CTR_Mode_ExternalCipher::Encryption ctrEncryption(aesEncryption, iv);
StreamTransformationFilter ctrEncryptor(ctrEncryption, new HexEncoder(new StringSink(outstr)), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING);
ctrEncryptor.Put((byte *)plainText, strlen(plainText));
ctrEncryptor.MessageEnd();
return UTF8_TO_TCHAR(outstr.c_str());
}
FString UAESBPLibrary::CTR_AESDecryptData(FString aes_content, FString aes_key, FString aes_IV, bool & result)
{
std::string sKey = TCHAR_TO_UTF8(*aes_key);
std::string sIV = TCHAR_TO_UTF8(*aes_IV);
const char *cipherText = TCHAR_TO_ANSI(*aes_content);
std::string outstr;
try
{
SecByteBlock key(AES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);
byte iv[AES::BLOCKSIZE];
memset(iv, 0x30, AES::BLOCKSIZE);
sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);
CTR_Mode<AES >::Decryption ctrDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);
HexDecoder decryptor(new StreamTransformationFilter(ctrDecryption, new StringSink(outstr), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING));
decryptor.Put((byte *)cipherText, strlen(cipherText));
decryptor.MessageEnd();
result = true;
}
catch (const std::exception&)
{
outstr = "error";
UE_LOG(LogTemp, Error, TEXT("CTR_AESDecryptData failed!"));
result = false;
}
return UTF8_TO_TCHAR(outstr.c_str());
}
#pragma endregion
编译成功没有报错就可以在蓝图中调用了
6.蓝图调用
- 在关卡蓝图中调用相应接口进行加密解密
- 运行测试
成功输出。
三、总结
- 心得:
在实际操作中主要解决报错的问题,大多是因为Crypt++工程运行库和本工程运行库不一致或者是引用库文件目录有误所造成的,需要仔细检查核对。 以上就是今天要讲的内容,本文仅仅简单介绍了UE4使用外部lib库的使用,如果需要将lib库添加到插件中也是可以的,在此不多赘述。 - 本文参考:
https://blog.csdn.net/lyg920/article/details/51543346 https://blog.csdn.net/Szu_IT_Man/article/details/78790408 https://blog.csdn.net/qq_33042187/article/details/115446197 https://blog.csdn.net/l2543767402/article/details/89792336 在此,非常感谢以上几位大神的指引,文中有部分是摘抄大神手笔,请勿怪。
|