XOR 加密适用于不在 Visual Studio 2017 的代码块
XOR-Encryption works on Code blocks not on Visual Studio 2017
所以我想在写入 .txt 文件时加密我的数据,所以我从这段代码中选择 XOR-Encryption:
Github
因此,当我 运行 在代码中阻止它 运行s 并显示此结果时:
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] = '[=10=]'; // 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);
}
请注意,字符串终止符需要一个额外的字节 - 字符串应该终止。
所以我想在写入 .txt 文件时加密我的数据,所以我从这段代码中选择 XOR-Encryption: Github 因此,当我 运行 在代码中阻止它 运行s 并显示此结果时:
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] = '[=10=]'; // 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);
}
请注意,字符串终止符需要一个额外的字节 - 字符串应该终止。