UE4学习笔记(21)Crypto++_AES加密解密

发表于2018-01-16
评论0 8.6k浏览
环境
win10
Crypto++ 5.6.5
unreal 4.17.2
vs2017

简介

AES (高级加密标准,英文全称:Advanced Encryption Standard)在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。先要了解更多的可以上wiki了解

程序员就是想偷懒,本来想直接用引擎现有的api—— FAES::EncryptData/DecryptData 来实现加密解密,奈何用上最主要问题就是原文本和解密后的文本不一致。网上搜了相关资料,遇到这个问题的人也有几个,但是并没有找到可靠的解决方案。想要了解引擎内置的这方面,可以查看下面几个链接:
       FAES encryption

顺便发现别人写了自己的插件 SodiumUE4(链接),并且上传到了github上,有兴趣的同学可以看一看,还没试用过。

本文主要使用Crypto++ 5.6.5编译的lib库,在unreal中调用使用。

步骤

下载并编译 Crypto++ 5.6.5

  • 右键crylib属性,C/C++ ——> 代码生成 ——> 运行库 设置为 DLL(/MD)

  • 选择 release x64编译
  • 可以在“cryptopp565\x64\Output\Release”路径下找到cryptlib.lib

为引擎拷贝必要文件

  • 将 cryptlib.lib拷贝到“UE_4.17\Engine\Source\ThirdParty\CryptoPP\5.6.5\lib\Win64\VS2015”,如果没有文件夹,请自行建立。

  • 将cryptopp565源代码的。h头文件全部拷贝到“UE_4.17\Engine\Source\ThirdParty\CryptoPP\5.6.5\include”,如果没有文件夹,请自行建立。

  • 如果想省事,可用题主编译好的压缩文件,直接解压到“UE_4.17\Engine\Source\ThirdParty\CryptoPP\5.6.5\”文件夹下。

创建自己的AES插件

  • 创建好插件后,在。build.cs添加CryptoPP(可能不用这一步)
        PrivateDependencyModuleNames.AddRange(
            new string[]
            {
                "CoreUObject",
                "Engine",
                "Slate",
                "SlateCore",
                "CryptoPP",
                // ... add private dependencies that you statically link with here ...  
            }
            );
 bEnableExceptions = true;
 Definitions.Add("BOOST_SYSTEM_NOEXCEPT");
AESBPLibrary.h
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Engine.h"
#include "AESBPLibrary.generated.h"
UCLASS()
class UAESBPLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_UCLASS_BODY()
    ///ECB mode
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "ECB_AESEncryptData", Keywords = "ECB_AESEncryptData", aes_key = "0123456789ABCDEF0123456789ABCDEF"), Category = "AES")
        static FString ECB_AESEncryptData(FString aes_content, FString aes_key);
    UFUNCTION(BlueprintCallable, meta = (DisplayName = "ECB_AESDecryptData", 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", 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
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "AESBPLibrary.h"
#include "AES.h"
#include "string.h"
#include "ThirdParty/CryptoPP/5.6.5/include/aes.h"
#include "ThirdParty/CryptoPP/5.6.5/include/hex.h"
#include "ThirdParty/CryptoPP/5.6.5/include/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,
        true
    );
    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,
            true
        ));
        decryptor.Put((byte *)cipherText, strlen(cipherText));
        decryptor.MessageEnd();
        result = true;
    }
    catch (const std::exception&)
    {
        outstr = "error";
        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,
        true);
    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,
            true));
        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,
        true);
    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,
            true));
        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,
        true);
    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,
            true));
        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,
        true);
    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,
            true));
        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,
        true);
    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,
            true));
        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

  • 测试蓝图

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引