我有一个XOR函数:

string encryptDecrypt(string toEncrypt) {
char key[64] = { 'F', '2', 'D', 'C', '5', '4', '0', 'D', 'B', 'F', '3', 'E', '1', '2', '9', 'F', '4', 'E', 'A', 'A', 'F', '7', '6', '7', '5', '6', '9', 'E', '3', 'C', 'F', '9', '7', '5', '2', 'B', '4', 'B', '8', '2', '6', 'D', '9', '8', 'F', 'D', '8', '3', '8', '4', '6', '0', '8', '5', 'C', '0', '3', '7', 'D', '3', '5', 'F', '7', '5' };
string output = toEncrypt;
for (int i = 0; i < toEncrypt.size(); i++)
    output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
return output;


}

我加密了.ini:

[test]
baubau = 1
haha = 2
sometext = blabla


我如何尝试解密和使用值:

std::string Filename = "abc.ini";
std::ifstream input(Filename, std::ios::binary | ios::in);  // Open the file
std::string line;                                           // Temp variable
std::vector<std::string> lines;                             // Vector for holding all lines in the file
while (std::getline(input, line))                           // Read lines as long as the file is
{
lines.push_back(encryptDecrypt(line));
}
// Here i should call the ini reader? but how?
CIniReader iniReader("abc.ini");
string my = encryptDecrypt(iniReader.ReadString("test", "baubau", ""));
for (auto s : lines)
{
    cout << my;
    cout << endl;
}


我的错误在哪里?一些帮助将不胜感激,非常感谢!

最佳答案

您可以做的是:


逐行读取文件,然后将键和值分开,即在看到“ key = value”的地方将其分为键和值。
加密值。
如果值不再是文件编码中的有效文本,则对值进行Base64编码。
将行替换为'key = base64-encoded-value'。
以后,当您读取密钥的编码值时,它只是一个简单的Base64编码的字节字符串,然后对字符串进行Base64解码,然后解密该值。


例如,这一行:

包包= 1

将值“ 1”作为字符串,并使用函数对其进行加密。在这种情况下,结果是可打印的字符串“ w”。但是,我会将其视为任意字节。

Base64编码的“加密”值。例如,UTF-8(或ASCII)中'w'的Base64编码为“ dw ==”。

将行替换为:

包鲍= dw ==

或者,如果您喜欢:

baubau =“ dw ==”

稍后,当您读取baubau的值时,只需对Base64解码'dw ==',获得'w',然后解密'w'即可得出'1'。

10-07 19:25
查看更多