IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 【无标题】UE4.26调用CryptoPP8.6库实现加密解密 -> 正文阅读

[游戏开发]【无标题】UE4.26调用CryptoPP8.6库实现加密解密


前言

在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)
{

}


#pragma region ECB

FString UAESBPLibrary::ECB_AESEncryptData(FString aes_content, FString aes_key)
{

	//std::string sKey = "0123456789ABCDEF0123456789ABCDEF";
	std::string sKey = TCHAR_TO_UTF8(*aes_key);
	const char *plainText = TCHAR_TO_ANSI(*aes_content);
	std::string outstr;


	//填key    
	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
	{
		//填key    
		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 = "0123456789ABCDEF0123456789ABCDEF";
	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;

	//填key    
	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);

	//填iv    
	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
	{
		//填key    
		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);

		//填iv    
		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 = "0123456789ABCDEF0123456789ABCDEF";
	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;

	//填key    
	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);

	//填iv    
	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
	{
		//填key    
		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);

		//填iv    
		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 = "0123456789ABCDEF0123456789ABCDEF";
	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;

	//填key    
	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);

	//填iv    
	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
	{
		//填key    
		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);

		//填iv    
		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 = "0123456789ABCDEF0123456789ABCDEF";
	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;

	//填key    
	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);

	//填iv    
	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
	{
		//填key    
		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);

		//填iv    
		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 = "0123456789ABCDEF0123456789ABCDEF";
	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;

	//填key    
	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);

	//填iv    
	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
	{
		//填key    
		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);

		//填iv    
		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
    在此,非常感谢以上几位大神的指引,文中有部分是摘抄大神手笔,请勿怪。
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-01-01 14:17:32  更:2022-01-01 14:18:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 20:27:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码