gpt4 book ai didi

c - XOR 加密适用于不在 Visual Studio 2017 上的代码块

转载 作者:太空宇宙 更新时间:2023-11-04 00:59:54 24 4
gpt4 key购买 nike

所以我想在写入 .txt 文件时加密我的数据,所以我从这段代码中选择 XOR-Encryption: Github因此,当我在代码块中运行时,它会运行并显示以下结果:

Encrypted: :=.43*-:8m2$.a
Decrypted:kylewbanks.com0

Process returned 0 (0x0) execution time : 0.025 s
Press any key to continue.

但是当我开始使用 Visual Studio 2017 时它显示此错误:

Error (active)  E0059   function call is not allowed in a constant expression   

这意味着我不能在声明数组时放置变量,所以有什么方法可以让我的加密在 VS2017 中工作。我认为问题是当使用常量声明变量时,无论如何强制它或其他易于使用的加密方法,我不需要安全只是为了防止文件中的纯文本。 无论如何,这是唯一的代码:

#include <stdio.h>
#include <string.h>

void encryptDecrypt(char *input, char *output) {
char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array

int i;
for(i = 0; i < strlen(input); i++) {
output[i] = input[i] ^ key[i % (sizeof(key)/sizeof(char))];
}
}

int main () {
char baseStr[] = "kylewbanks.com";

char encrypted[strlen(baseStr)];
encryptDecrypt(baseStr, encrypted);
printf("Encrypted:%s\n", encrypted);

char decrypted[strlen(baseStr)];
encryptDecrypt(encrypted, decrypted);
printf("Decrypted:%s\n", decrypted);
}

最佳答案

MSVC 不支持可变长度数组。一种方法是分配内存。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void encryptDecrypt(char *input, char *output) {
char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array
size_t i;
for(i = 0; i < strlen(input); i++) {
output[i] = input[i] ^ key[i % (sizeof(key)/sizeof(char))];
}
output[i] = '\0'; // terminate
}

int main () {
char baseStr[] = "kylewbanks.com";
size_t len = strlen(baseStr) + 1;

char *encrypted = malloc(len);
if(encrypted == NULL) {
// error handling
}
encryptDecrypt(baseStr, encrypted);
printf("Encrypted:%s\n", encrypted);

char *decrypted = malloc(len);
if(decrypted == NULL) {
// error handling
}
encryptDecrypt(encrypted, decrypted);
printf("Decrypted:%s\n", decrypted);

free(decrypted);
free(encrypted);
}

请注意,字符串终止符需要一个额外的字节 - 字符串应该终止。

关于c - XOR 加密适用于不在 Visual Studio 2017 上的代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44438063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com