在我的解密程序中未收到输出文件

Not receiving output file in my decryption program

提问人:Abe 提问时间:2/28/2019 最后编辑:Abe 更新时间:3/12/2019 访问量:96

问:

我已经为此苦苦挣扎了几天,但我就是无法弄清楚。我对C++很陌生,我正在为我自己的编码制作一个简单的解码程序。但是由于某种奇怪的原因,我没有得到输出。我正在CodeBlocks IDE中编码,然后通过PowerShell运行它,我尝试通过常规命令提示符运行,但这无济于事。有人有解决方案吗?非常感谢你,安倍晋三。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

std::string base64_decode(const std::string &s);
std::string DecryptB64 (std::string s);

const std::string &SALT1 = "hidden";
const std::string &SALT2 = "hidden";
const std::string &SALT3 = "hidden";
const std::string &BASE64_CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int main(int argc, char **argv)
{
    if(argc != 3)
        return std::cout << "Programma heeft twee argumenten nodig, input en output!" << std::endl, 2;
    std::string in (argv[1]), out (argv[2]);
    std::ifstream fi (in);
    if(!fi)
        return std::cout << "Kan input bestand '" << in << "' niet lezen!" << std::endl, 3;
    std::string data;
    fi >> data;
    if(!fi)
        return std::cout << "Input bestand '" << in << "' corrupted!" << std::endl, 4;
    data = DecryptB64(data);
    std::ofstream fo (out);
    if (!fo)
        return std::cout << "Kan output bestand '" << out << "' niet lezen!" << std::endl, 5;
    fo << data;
    std::cout << "Decoden is gelukt!" << std::endl;
    return 0;
}

std::string base64_decode(const std::string &s)
{
        std::string ret;
        std::vector<int> vec(256, -1);
        for (int i = 0; i < 64; i++)
            vec [BASE64_CODES[i]] = i;
        int val = 0, bits = -8;
        for (const auto &c : s)
            {
                if (vec[c] == -1) break;
                val = (val << 6) + vec[c];
                bits += 6;

                if (bits >= 0)
                    {

                        ret.push_back(char((val >> bits) & 0xFF));
                        bits -= 8;
                    }
            }

        return ret;
}

std::string DecryptB64 (std::string s)
{
    s = s.erase (7, 1);
    s = s.erase (1, 1);
    s = base64_decode (s);
    s = s.substr (SALT2.length() + SALT3.length());
    s = s.substr (0, s.length() - SALT1.length());
    s = base64_decode (s);
    s = s.substr (0, s.length() - SALT1.length());
    s = s.erase (7, SALT3.length());
    s = s.substr (SALT1.length());
    s = s.substr (0, s.length() - SALT2.length() - SALT3.length());
    return s;
}

以下是加密代码:

#ifndef BASE64_H
#define BASE64_H

#include <vector>
#include <string>

namespace Base64
{
    std::string base64_encode(const std::string &);

    const std::string &SALT1 = ":EG:::ZXQL";
    const std::string &SALT2 = ":ZAerPLgfs";
    const std::string &SALT3 = "decrypt696";


    std::string EncryptB64 (std::string s)
    {
        s = SALT1 + s + SALT2 + SALT3;
        s = base64_encode (s);
        return s;
    }

    const std::string &BASE64_CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789+/";

    std::string base64_encode(const std::string &s)
    {
        std::string ret;
        int val = 0;
        int bits = -6;
        const unsigned int b63 = 0x3F;

        for(const auto &c : s)
        {
            val = (val << 8) + c;
            bits += 8;
            while(bits >= 0)
            {
                ret.push_back(BASE64_CODES[(val >> bits) & b63]);
                bits -= 6;
            }
        }

        if(bits > -6)
            ret.push_back(BASE64_CODES[((val << 8) >> (bits+8)) & b63]);

        while(ret.size() % 4)
            ret.push_back('=');

        return ret;
    }
}

#endif // BASE_64

以及使用加密函数的代码:

    std::string WriteLog(const T &t)
{
    std::string path = GetPath(true);
    Helper::DateTime dt;
    std::string name = dt.GetDateTimeString("_") + ".log";

    try
    {
        std::ofstream file(path + name);
        if(!file) return "";
        std::ostringstream s;
        s << "[" << dt.GetDateTimeString() << "]" << std::endl << t << std::endl;
        std::string data = Base64::EncryptB64(s.str());
        file << data;
        if(!file)
            return "";
        file.close();
        return name;
    }
    catch(...)
    {
        return "";
    }
}

此外,以下是要使用程序解密的加密文件的内容:

OkVHOjo7WlhRTFsyMS5xMz5zMDE6IDE2OjI1OjE2XQpbVF2bUl2bSV2bT22bSF2bSl2bUl2bSV2bT22bVF2bUl2bTV2bTl2bQl2bT22bVF2bSV2bUl2bQl2bTl2bUl2bVF2bT22bSV2bQl2bTl2bT22bUl2bVF2bSV2bQl2bTl2bUl2bVF2bSV2bT22bSl2bTl2bQl2bVF2bUl2bSV2bT22bQl2bSl2bUl2bVF2bSV2bQl2bSl2bUl2bVF2bT22bSV2bQl2bSl2bTV2bR22bSV2bVF2bVV2bRF2bTF2bSF2bR22bQl2bVV2bUl2bSV2bRl2bT22bTF2bRF2bS22bR22bSF2bRl2bRF2bR22bSF2bRF2bUl2bS22bTF2bR22bSF2bQl2bRl2bRF2bWF2bSl2bTF2bR22bSF2bS22bTF2bSl2bSF2bQl2bUl2bT22bVV2bSF2bSl2bQl2bTl2bSV2bUl2bRV2bVF2bQl2bSF2bSl2bVl2bTl2bVV2bSV2bRl2bR22bS22bR22bQl2bSF2bRl2bSV2bVV2bR22bSF2bRl2bSV2bVV2bVF2bTF2bRF2bSF2bRl2bVV2bR22bQl2bTl2bSl2bRl2bSV2bVV2bRF2bSF2bVF2bUl2bVV2bSl2bTF2bUl2bRF2bR22bSF2bSl2bVF2bUl2bRF2bT22bTF2bSF2bVV2bSl2bRF2bUl2bVF2bSV2bT22bVV2bSF2bUl2bRF2bVF2bVV2bSV2bSF2bSl2bUl2bVF2bVV2bSV2bRF2bSl2bSF2bSl2bVF2bVV2bSV2bUl2bRF2bSF2bTF2bVV2bVF2bUl2bRF2bSF2bVF2bRF2bVV2bUl2bSV2bSF2bVF2bRF2bUl2bSV2bSF2bVF2bSF2bRF1KOlpBZXJQTGdmc3RlY4J6cHQ3OTY=
C++ 文件 IOSTREAM IFSTREAM 解码

评论

0赞 0x5453 3/1/2019
你是如何运行你的程序的?您是在命令行上运行还是使用 IDE?
0赞 molbdnilo 3/1/2019
请发布一个最小的可重复示例。无法判断您没有获得任何输出是因为写入失败,还是因为解密已损坏,还是因为其他原因。
3赞 Alan Birtles 3/1/2019
对我有用,大概有一些未定义的行为just_some_secret_calculations_for_the_decrypting
0赞 Alan Birtles 3/1/2019
请注意,如果不是 7 位值,则在字符已签名的平台(大多数平台)上无法正常工作。如果输出是二进制的,则应以二进制模式打开输出流vec[ c ]c
0赞 Abe 3/1/2019
谢谢大家的回复,我已经编辑了帖子以添加更多信息,所以我希望我已经清除了所有内容。

答: 暂无答案