UE4 c++加密 (附带读取和保存文件)

来源:互联网 发布:张辛苑自己的淘宝店 编辑:程序博客网 时间:2024/05/18 00:31

转载的网址找不到了,找到后会添上。。。

.h

#pragma once#include "GameFramework/Actor.h"#include "CPassword.generated.h"UCLASS()class AA_API ACPassword : public AActor{GENERATED_BODY()public:// Sets default values for this actor's propertiesACPassword();// Called when the game starts or when spawnedvirtual void BeginPlay() override;// Called every framevirtual void Tick( float DeltaSeconds ) override;FString Encrypt(FString Src, WORD Key);FString Decrypt(FString Des, WORD Key);UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "password")FString projectDir= FPaths::GameDir() + "/CC.txt"; //"D:/23.txt"; //;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "password")FString  NameString;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "password")FString  NameStringRE;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "password")FString  Savestring = "453243";
.cpp

#include "AA.h"#include "CPassword.h"#define C1 52845#define C2 22719#define COMPLEXITY_NUM 65WORD Key = 1314;//"D:\\123.txt";GameDir()+// Sets default valuesACPassword::ACPassword(){ // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;//FString a = Encrypt("123", Key);}// Called when the game starts or when spawnedvoid ACPassword::BeginPlay(){Super::BeginPlay();GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, Encrypt(NameString, Key));GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, Decrypt(NameStringRE, Key));//UE_LOG(LogTemp, Warning, TEXT("Cannot find"));//FString projectDir = FPaths::GameDir()+ "/CC.txt"; //"D:/23.txt"; //TArray<FString> StringArray;/*if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*projectDir)){GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("** Could not Find File **"));return;}FFileHelper::LoadANSITextFileToStrings(*(projectDir), NULL, StringArray);*/FString Content;//FFileHelper::LoadFileToString(Content, TEXT("Content/32.txt"));GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, Content);//FFileHelper::SaveStringToFile("");FFileHelper::SaveStringToFile(Savestring, *projectDir);FFileHelper::LoadFileToString(Content, *projectDir);GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, Content);}FString ACPassword::Encrypt(FString Src, WORD Key){FString Result, str;int32 i, j;Result = Src;for (i = 0; i < Src.Len(); i++){Result[i] = Src[i] ^ (Key >> 8);Key = ((BYTE)Result[i] + Key)*C1 + C2;}Src = Result;Result.Empty();for (i = 0; i < Src.Len(); i++){j = (BYTE)Src[i];str = "12";str[0] = COMPLEXITY_NUM + j / 26;str[1] = COMPLEXITY_NUM + j % 26;Result += str;}return Result;}FString ACPassword::Decrypt(FString Des, WORD Key){FString Result, str;int32 i, j;Result.Empty();for (i = 0; i < Des.Len() / 2; i++){j = ((BYTE)Des[2 * i] - COMPLEXITY_NUM) * 26;j += (BYTE)Des[2 * i + 1] - COMPLEXITY_NUM;str = "1";str[0] = j;Result += str;}Des = Result;for (i = 0; i < Des.Len(); i++){Result[i] = (BYTE)Des[i] ^ (Key >> 8);Key = ((BYTE)Des[i] + Key)*C1 + C2;}return Result;}



3 0
原创粉丝点击