我正在使用 XTEA 算法制作一个加密/解密程序。加密/解密功能工作正常,但是当我加密文件然后解密它时,我在文件末尾得到一些额外的字符:
--- Original file ---
QwertY
--- Encrypted file ---
»¦æŸS@±
--- Deciphered from encrypted ---
QwertY  ß*tÞÇ
我不知道为什么最后会出现“ß*tÞÇ”。我将发布我的一些代码,但不是全部,因为它太长了。加密/解密功能采用 64 位数据和 128 位密钥,并将数据加密/解密到相同的块大小,同样是 64 位(此处类似功能)。然后可以将其写入新文件。
    long data[2]; // 64bits
    ZeroMemory(data, sizeof(long)*2);
    char password[16];
    ZeroMemory(password, sizeof(char)*16);
    long *key;
    if(argc > 1)
    {
        string originalpath = argv[1];
        string finalpath;
        string eextension = "XTEA";
        string extension = GetFileExtension(originalpath);
        bool encipherfile = 1;
        if(extension.compare(eextension) == 0) // If extensions are equal, dont encipher file
        {
            encipherfile = 0;
            finalpath = originalpath;
            finalpath.erase(finalpath.length()-5, finalpath.length());
        }
        ifstream in(originalpath, ios::binary);
        ofstream out(finalpath, ios::binary);
        cout << "Password:" << endl;
        cin.get(password,sizeof(password));
        key = reinterpret_cast<long *>(password);
        while(!in.eof())
        {
            ZeroMemory(data, sizeof(long)*2);
            in.read(reinterpret_cast<char*>(&data), sizeof(long)*2); // Read 64bits from file
            if(encipherfile == 1)
            {
                encipher(data, key);
                out.write(reinterpret_cast<char*>(&data), sizeof(data));
                continue;
            }
            if(encipherfile == 0)
            {
                decipher(data, key);
                out.write(reinterpret_cast<char*>(&data), sizeof(data));
            }
        }