• UE5中一机一码功能


    创建蓝图函数库

    1、获取第一个有效的硬盘ID

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "Kismet/BlueprintFunctionLibrary.h"
    5. #include "GetDiskIDClass.generated.h"
    6. /**
    7. *
    8. */
    9. UCLASS()
    10. class OPENTEST_API UGetDiskIDClass : public UBlueprintFunctionLibrary
    11. {
    12. GENERATED_BODY()
    13. public:
    14. UFUNCTION(BlueprintCallable)
    15. static FString GetFirstDiskID();
    16. };
    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "GetDiskIDClass.h"
    3. #include
    4. FString UGetDiskIDClass::GetFirstDiskID()
    5. {
    6. FString SerialNumber;
    7. for (int DriveNumber = 0; DriveNumber < 16; ++DriveNumber) {
    8. FString Drive = FString::Printf(TEXT("\\\\.\\PhysicalDrive%d"), DriveNumber);
    9. HANDLE hDevice = CreateFile(*Drive, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    10. if (hDevice != INVALID_HANDLE_VALUE) {
    11. STORAGE_PROPERTY_QUERY storageQuery;
    12. memset(&storageQuery, 0, sizeof(storageQuery));
    13. storageQuery.PropertyId = StorageDeviceProperty;
    14. storageQuery.QueryType = PropertyStandardQuery;
    15. BYTE buffer[4096];
    16. DWORD bytesReturned = 0;
    17. if (DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &storageQuery, sizeof(storageQuery), &buffer, sizeof(buffer), &bytesReturned, NULL)) {
    18. STORAGE_DESCRIPTOR_HEADER* header = (STORAGE_DESCRIPTOR_HEADER*)buffer;
    19. if (header->Size > 0 && header->Size <= bytesReturned) {
    20. STORAGE_DEVICE_DESCRIPTOR* deviceDescriptor = (STORAGE_DEVICE_DESCRIPTOR*)buffer;
    21. if (deviceDescriptor->SerialNumberOffset > 0) {
    22. SerialNumber = FString(ANSI_TO_TCHAR((char*)deviceDescriptor + deviceDescriptor->SerialNumberOffset));
    23. }
    24. }
    25. }
    26. CloseHandle(hDevice);
    27. if (!SerialNumber.IsEmpty()) {
    28. return SerialNumber;
    29. }
    30. }
    31. }
    32. // 如果没有找到有效的硬盘ID,返回一个空字符串
    33. return FString("");
    34. }

    2、获取第一个有效的Mac地址

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "Kismet/BlueprintFunctionLibrary.h"
    5. #include "GetFirstMacAddrClass.generated.h"
    6. /**
    7. *
    8. */
    9. UCLASS()
    10. class OPENTEST_API UGetFirstMacAddrClass : public UBlueprintFunctionLibrary
    11. {
    12. GENERATED_BODY()
    13. public:
    14. UFUNCTION(BlueprintCallable)
    15. static FString GetFirstMac();
    16. };
    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "GetFirstMacAddrClass.h"
    3. #include
    4. #include
    5. FString UGetFirstMacAddrClass::GetFirstMac()
    6. {
    7. IP_ADAPTER_INFO IpAddresses[16];
    8. ULONG OutBufferLength = sizeof(IP_ADAPTER_INFO) * 16;
    9. // Read the adapters
    10. uint32 RetVal = GetAdaptersInfo(IpAddresses, &OutBufferLength);
    11. if (RetVal == NO_ERROR)
    12. {
    13. PIP_ADAPTER_INFO AdapterList = IpAddresses;
    14. // Walk the set of addresses to find the first valid MAC address
    15. while (AdapterList)
    16. {
    17. // If there is an address to read
    18. if (AdapterList->AddressLength > 0)
    19. {
    20. TArray MacAddr;
    21. MacAddr.AddZeroed(AdapterList->AddressLength);
    22. FMemory::Memcpy(MacAddr.GetData(), AdapterList->Address, AdapterList->AddressLength);
    23. FString Address;
    24. for (TArray::TConstIterator it(MacAddr); it; ++it)
    25. {
    26. Address += FString::Printf(TEXT("%02x"), *it);
    27. }
    28. // Return the first valid MAC address found
    29. return Address;
    30. }
    31. AdapterList = AdapterList->Next;
    32. }
    33. }
    34. // If no valid MAC address is found, return an empty string
    35. return FString("");
    36. }

    3、加密

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "Kismet/BlueprintFunctionLibrary.h"
    5. #include "EncryptClass.generated.h"
    6. /**
    7. *
    8. */
    9. UCLASS()
    10. class OPENTEST_API UEncryptClass : public UBlueprintFunctionLibrary
    11. {
    12. GENERATED_BODY()
    13. public:
    14. UFUNCTION(BlueprintCallable)
    15. static bool EncryptStringWithAES(const FString& InputString, const FString& EncryptionKey, FString& OutEncryptedString);
    16. };
    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "EncryptClass.h"
    3. #define UI UI_ST
    4. #include
    5. #include
    6. #undef UI
    7. bool UEncryptClass::EncryptStringWithAES(const FString& InputString, const FString& EncryptionKey, FString& OutEncryptedString)
    8. {
    9. const unsigned char* iv = (const unsigned char*)"0123456789012345"; // 初始化向量(IV),需要与解密时一致
    10. int keylength = 128; // 加密密钥长度,可以是128、192或256
    11. EVP_CIPHER_CTX* ctx;
    12. ctx = EVP_CIPHER_CTX_new();
    13. EVP_CIPHER_CTX_init(ctx);
    14. const EVP_CIPHER* cipherType = EVP_aes_128_cbc(); // 选择加密算法
    15. if (EVP_EncryptInit_ex(ctx, cipherType, NULL, (const unsigned char*)TCHAR_TO_UTF8(*EncryptionKey), iv) != 1) {
    16. // 初始化加密上下文失败
    17. return false;
    18. }
    19. int max_output_length = InputString.Len() + EVP_MAX_BLOCK_LENGTH; // 预估加密后的最大长度
    20. unsigned char* encryptedOutput = new unsigned char[max_output_length];
    21. int encryptedLength = 0;
    22. if (EVP_EncryptUpdate(ctx, encryptedOutput, &encryptedLength, (const unsigned char*)TCHAR_TO_UTF8(*InputString), InputString.Len()) != 1) {
    23. // 加密数据失败
    24. EVP_CIPHER_CTX_free(ctx);
    25. delete[] encryptedOutput;
    26. return false;
    27. }
    28. int finalEncryptedLength = 0;
    29. if (EVP_EncryptFinal_ex(ctx, encryptedOutput + encryptedLength, &finalEncryptedLength) != 1) {
    30. // 完成加密失败
    31. EVP_CIPHER_CTX_free(ctx);
    32. delete[] encryptedOutput;
    33. return false;
    34. }
    35. encryptedLength += finalEncryptedLength;
    36. // Convert encrypted data to hex string
    37. FString HexString;
    38. for (int i = 0; i < encryptedLength; ++i) {
    39. FString Hex = FString::Printf(TEXT("%02x"), encryptedOutput[i]);
    40. HexString += Hex;
    41. }
    42. OutEncryptedString = HexString;
    43. EVP_CIPHER_CTX_free(ctx);
    44. delete[] encryptedOutput;
    45. return true;
    46. }

    4、解密

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "Kismet/BlueprintFunctionLibrary.h"
    5. #include "DecryptClass.generated.h"
    6. /**
    7. *
    8. */
    9. UCLASS()
    10. class OPENTEST_API UDecryptClass : public UBlueprintFunctionLibrary
    11. {
    12. GENERATED_BODY()
    13. public:
    14. UFUNCTION(BlueprintCallable)
    15. static bool DecryptStringWithAES(const FString& EncryptedString, const FString& EncryptionKey, FString& OutDecryptedString);
    16. };
    1. #include "DecryptClass.h"
    2. #define UI UI_ST
    3. #include
    4. #include
    5. #undef UI
    6. bool UMynewJMClass::DecryptStringWithAES(const FString& EncryptedString, const FString& EncryptionKey,
    7. FString& OutDecryptedString)
    8. {
    9. const unsigned char* iv = (const unsigned char*)"0123456789012345"; // IV, 与加密时一致
    10. int keylength = 128; // 密钥长度,与加密时一致
    11. EVP_CIPHER_CTX* ctx;
    12. ctx = EVP_CIPHER_CTX_new();
    13. EVP_CIPHER_CTX_init(ctx);
    14. const EVP_CIPHER* cipherType = EVP_aes_128_cbc(); // 选择加密算法
    15. if (EVP_DecryptInit_ex(ctx, cipherType, NULL, (const unsigned char*)TCHAR_TO_UTF8(*EncryptionKey), iv) != 1) {
    16. // 初始化解密上下文失败
    17. return false;
    18. }
    19. int max_output_length = EncryptedString.Len() / 2; // 预估解密后的最大长度
    20. unsigned char* decryptedOutput = new unsigned char[max_output_length];
    21. int decryptedLength = 0;
    22. // 将十六进制字符串转换回原始加密数据
    23. TArray EncryptedData;
    24. for (int i = 0; i < EncryptedString.Len(); i += 2) {
    25. FString ByteString = EncryptedString.Mid(i, 2);
    26. uint8 Byte = (uint8)FCString::Strtoi64(*ByteString, nullptr, 16);
    27. EncryptedData.Add(Byte);
    28. }
    29. if (EVP_DecryptUpdate(ctx, decryptedOutput, &decryptedLength, EncryptedData.GetData(), EncryptedData.Num()) != 1) {
    30. // 解密数据失败
    31. EVP_CIPHER_CTX_free(ctx);
    32. delete[] decryptedOutput;
    33. return false;
    34. }
    35. int finalDecryptedLength = 0;
    36. if (EVP_DecryptFinal_ex(ctx, decryptedOutput + decryptedLength, &finalDecryptedLength) != 1) {
    37. // 完成解密失败
    38. EVP_CIPHER_CTX_free(ctx);
    39. delete[] decryptedOutput;
    40. return false;
    41. }
    42. decryptedLength += finalDecryptedLength;
    43. // 将解密后的内容存储在 OutDecryptedData 中
    44. TArray DecryptedData;
    45. DecryptedData.SetNumUninitialized(decryptedLength);
    46. FMemory::Memcpy(DecryptedData.GetData(), decryptedOutput, decryptedLength);
    47. // Convert the bytes to a hex string
    48. FString HexString;
    49. for (uint8 Byte : DecryptedData) {
    50. FString Hex = FString::Printf(TEXT("%02x"), Byte); // Convert byte to two-digit hex representation
    51. HexString += Hex;
    52. }
    53. OutDecryptedString = HexString;
    54. EVP_CIPHER_CTX_free(ctx);
    55. delete[] decryptedOutput;
    56. return true;
    57. }

    5、注册

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "Kismet/BlueprintFunctionLibrary.h"
    5. #include "HexClass.generated.h"
    6. /**
    7. *
    8. */
    9. UCLASS()
    10. class OPENTEST_API UHexClass : public UBlueprintFunctionLibrary
    11. {
    12. GENERATED_BODY()
    13. public:
    14. UFUNCTION(BlueprintCallable)
    15. static FString HexStringToPlainText(const FString& HexString);
    16. };
    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "HexClass.h"
    3. #include "GenericPlatform/GenericPlatformMisc.h"
    4. #include "Misc/Guid.h"
    5. FString UHexClass::HexStringToPlainText(const FString& HexString)
    6. {
    7. FString PlainText;
    8. // 将十六进制字符串转换为原始文本
    9. for (int i = 0; i < HexString.Len(); i += 2)
    10. {
    11. FString ByteString = HexString.Mid(i, 2);
    12. int32 ByteValue = FCString::Strtoi(*ByteString, nullptr, 16);
    13. PlainText.AppendChar((TCHAR)ByteValue);
    14. }
    15. return PlainText;
    16. }
    17. FString UMyhuanyuanClass::GetMachineId()
    18. {
    19. return FPlatformMisc::GetMachineId().ToString();
    20. }
    21. TArray UMyhuanyuanClass::GetAllMacAddress()
    22. {
    23. return FPlatformMisc::GetMacAddress();
    24. }

     分割线

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "Kismet/BlueprintFunctionLibrary.h"
    5. #include "newfunctionClass.generated.h"
    6. /**
    7. *
    8. */
    9. UCLASS()
    10. class OPENTEST_API UnewfunctionClass : public UBlueprintFunctionLibrary
    11. {
    12. GENERATED_BODY()
    13. UFUNCTION(BlueprintCallable)
    14. static bool NewEncryptStringWithAES(const FString& InputString, const FString& EncryptionKey, FString& OutEncryptedString);
    15. UFUNCTION(BlueprintCallable)
    16. static bool NewDecryptStringWithAES(const FString& EncryptedString, const FString& EncryptionKey, FString& OutDecryptedString);
    17. };
    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "newfunctionClass.h"
    3. #include "Misc/Base64.h"
    4. #define UI UI_ST
    5. #include
    6. #include
    7. #include
    8. #include
    9. #undef UI
    10. bool UnewfunctionClass::NewEncryptStringWithAES(const FString& InputString, const FString& EncryptionKey,
    11. FString& OutEncryptedString)
    12. {
    13. unsigned char iv[EVP_MAX_IV_LENGTH];
    14. // 使用密钥的哈希值作为 IV
    15. unsigned char keyHash[EVP_MAX_IV_LENGTH];
    16. uint32_t keyHashLen = 0;
    17. uint8_t* keyData = (uint8_t*)TCHAR_TO_UTF8(*EncryptionKey);
    18. EVP_MD_CTX* mdCtx;
    19. mdCtx = EVP_MD_CTX_new();
    20. EVP_DigestInit_ex(mdCtx, EVP_sha256(), NULL);
    21. EVP_DigestUpdate(mdCtx, keyData, FCStringAnsi::Strlen((char*)keyData));
    22. EVP_DigestFinal_ex(mdCtx, keyHash, &keyHashLen);
    23. EVP_MD_CTX_free(mdCtx);
    24. for (int i = 0; i < EVP_MAX_IV_LENGTH; ++i) {
    25. iv[i] = keyHash[i % keyHashLen]; // 使用密钥哈希值填充 IV
    26. }
    27. EVP_CIPHER_CTX* ctx;
    28. ctx = EVP_CIPHER_CTX_new();
    29. EVP_CIPHER_CTX_init(ctx);
    30. const EVP_CIPHER* cipherType = EVP_aes_128_cbc(); // 选择加密算法
    31. if (EVP_EncryptInit_ex(ctx, cipherType, nullptr, keyHash, iv) != 1) {
    32. // 初始化加密上下文失败
    33. EVP_CIPHER_CTX_free(ctx);
    34. return false;
    35. }
    36. TArray InputData;
    37. InputData.Append((uint8*)TCHAR_TO_UTF8(*InputString), InputString.Len()); // 将输入字符串转换为 UTF8 格式的 TArray
    38. int max_output_length = InputData.Num() + EVP_MAX_BLOCK_LENGTH; // 预估加密后的最大长度
    39. unsigned char* encryptedOutput = new unsigned char[max_output_length];
    40. int encryptedLength = 0;
    41. if (EVP_EncryptUpdate(ctx, encryptedOutput, &encryptedLength, InputData.GetData(), InputData.Num()) != 1) {
    42. // 加密数据失败
    43. EVP_CIPHER_CTX_free(ctx);
    44. delete[] encryptedOutput;
    45. return false;
    46. }
    47. int finalEncryptedLength = 0;
    48. if (EVP_EncryptFinal_ex(ctx, encryptedOutput + encryptedLength, &finalEncryptedLength) != 1) {
    49. // 完成加密失败
    50. EVP_CIPHER_CTX_free(ctx);
    51. delete[] encryptedOutput;
    52. return false;
    53. }
    54. encryptedLength += finalEncryptedLength;
    55. // 将加密后的内容转换为十六进制字符串
    56. FString HexString;
    57. for (int i = 0; i < encryptedLength; ++i) {
    58. FString Hex = FString::Printf(TEXT("%02x"), encryptedOutput[i]);
    59. HexString += Hex;
    60. }
    61. OutEncryptedString = HexString;
    62. EVP_CIPHER_CTX_free(ctx);
    63. delete[] encryptedOutput;
    64. return true;
    65. }
    66. bool UnewfunctionClass::NewDecryptStringWithAES(const FString& EncryptedString, const FString& EncryptionKey,
    67. FString& OutDecryptedString)
    68. {
    69. unsigned char iv[EVP_MAX_IV_LENGTH];
    70. // 使用密钥的哈希值作为 IV
    71. unsigned char keyHash[EVP_MAX_IV_LENGTH];
    72. uint32_t keyHashLen = 0;
    73. uint8_t* keyData = (uint8_t*)TCHAR_TO_UTF8(*EncryptionKey);
    74. EVP_MD_CTX* mdCtx;
    75. mdCtx = EVP_MD_CTX_new();
    76. EVP_DigestInit_ex(mdCtx, EVP_sha256(), NULL);
    77. EVP_DigestUpdate(mdCtx, keyData, FCStringAnsi::Strlen((char*)keyData));
    78. EVP_DigestFinal_ex(mdCtx, keyHash, &keyHashLen);
    79. EVP_MD_CTX_free(mdCtx);
    80. for (int i = 0; i < EVP_MAX_IV_LENGTH; ++i) {
    81. iv[i] = keyHash[i % keyHashLen]; // 使用密钥哈希值填充 IV
    82. }
    83. EVP_CIPHER_CTX* ctx;
    84. ctx = EVP_CIPHER_CTX_new();
    85. EVP_CIPHER_CTX_init(ctx);
    86. const EVP_CIPHER* cipherType = EVP_aes_128_cbc(); // 选择解密算法
    87. if (EVP_DecryptInit_ex(ctx, cipherType, nullptr, keyHash, iv) != 1) {
    88. // 初始化解密上下文失败
    89. EVP_CIPHER_CTX_free(ctx);
    90. return false;
    91. }
    92. // 将十六进制编码的字符串转换为字节数据
    93. TArray EncodedData;
    94. EncodedData.AddUninitialized(EncryptedString.Len() / 2); // 计算十六进制编码的字符串长度
    95. for (int i = 0; i < EncryptedString.Len() / 2; ++i) {
    96. FString Hex = EncryptedString.Mid(i * 2, 2);
    97. EncodedData[i] = FCString::Strtoi64(*Hex, nullptr, 16);
    98. }
    99. int max_output_length = EncodedData.Num();
    100. unsigned char* decryptedOutput = new unsigned char[max_output_length];
    101. int decryptedLength = 0;
    102. if (EVP_DecryptUpdate(ctx, decryptedOutput, &decryptedLength, EncodedData.GetData(), EncodedData.Num()) != 1) {
    103. // 解密数据失败
    104. EVP_CIPHER_CTX_free(ctx);
    105. delete[] decryptedOutput;
    106. return false;
    107. }
    108. int finalDecryptedLength = 0;
    109. if (EVP_DecryptFinal_ex(ctx, decryptedOutput + decryptedLength, &finalDecryptedLength) != 1) {
    110. // 完成解密失败
    111. EVP_CIPHER_CTX_free(ctx);
    112. delete[] decryptedOutput;
    113. return false;
    114. }
    115. decryptedLength += finalDecryptedLength;
    116. // 将解密后的内容转换为 FString 字符串
    117. OutDecryptedString = FString(reinterpret_cast<char*>(decryptedOutput), decryptedLength);
    118. EVP_CIPHER_CTX_free(ctx);
    119. delete[] decryptedOutput;
    120. return true;
    121. }

  • 相关阅读:
    C++向指定内存地址写入数据(Windows)
    Java面向对象三大基本特征之多态
    “Method Not Allowed“,405问题分析及解决
    语音芯片基础知识 什么是语音芯 他有什么作用 发展趋势是什么
    【C语言 | 数组】C语言数组详解(经典,超详细)
    【蓝桥杯冲击国赛计划第7天】模拟和打表 {题目:算式问题、求值、既约分数、天干地支}
    程序员的心得体会
    html网页多个div鼠标移动自动排列实例
    sunxi-spi驱动的DMA配置
    vue2 数据响应式原理——数据劫持(对象篇)
  • 原文地址:https://blog.csdn.net/weixin_42318094/article/details/134298677