非常基本的文件加密形式?

jma*_*erx 4 c++ encryption algorithm

加密纯文本文件的简单有效方法是什么?我不是在寻求安全,我只是希望普通用户不能篡改它.假设我有一个带有换行符分隔的电子邮件地址的文件.我希望它在打开时看起来像一个乱码,但很容易解密.

ten*_*our 7

最简单的实用形式是XOR密码.基本上,您生成任意长度的随机数的加密/解密密钥.要加密或解密数据,请使用密钥对数据进行异或.

它不是很安全; 它主要用于进行轻度混淆.

#include <vector>

typedef unsigned __int8 BYTE;

std::vector<BYTE> xor_encryptdecrypt(const std::vector<BYTE>& encryptionKey, const std::vector<BYTE>& input)
{
    std::vector<BYTE> ret;
    ret.reserve(input.size());

    std::vector<BYTE>::const_iterator keyIterator = encryptionKey.begin();

    for(std::vector<BYTE>::const_iterator inputIterator = input.begin(); inputIterator != input.end(); ++ inputIterator)
    {
        ret.push_back(*inputIterator ^ *keyIterator);

        // advance the key iterator, wrapping to begin.
        if(++ keyIterator == encryptionKey.end())
            keyIterator = encryptionKey.begin();
    }
    return ret;
}


int main()
{
    // variable-length key of randomness, used for both encryption and decryption.
    std::vector<BYTE> key;
    key.push_back(0x55);
    key.push_back(0xae);
    key.push_back(0x8c);
    key.push_back(0x14);

    std::vector<BYTE> input;
    input.push_back(0x00);
    input.push_back(0x01);
    input.push_back(0x02);
    input.push_back(0x03);
    input.push_back(0x04);
    input.push_back(0x05);

    // encrypt
    std::vector<BYTE> encrypted = xor_encryptdecrypt(key, input);

    // decrypt
    std::vector<BYTE> decrypted = xor_encryptdecrypt(key, encrypted);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)