C ++ DES算法不断生成不正确的最终块

C++ DES algorithm keeps generating incorrect final block

我有一项学校作业,我需要使用标准库在 C++ 中对 DES 进行编程。我已经完成了大部分算法,但是与我们用来验证的 openssl des-cbc 相比,最后一个块没有正确加密。其他块都正常工作,所以我认为这与我的填充有关

void des::get_mes_data(std::string mes_file){
    std::ifstream mes_in(mes_file, std::ios::binary);
    std::string content;
    std::getline(mes_in, content, std::string::traits_type::to_char_type(std::string::traits_type::eof()));

    //error seems to be here since last block is the only block affected
    if(content.size()%8 != 0) content.append(8-(content.size()%8), 0);
    //end error

    for(char c : content){
        if(c == 0) std::cout<<" NULL "<<std::endl;
        else if( c == EOF) std::cout<<" EOF "<<std::endl;
        else std::cout<<c;
    }
    std::string mes_bin="";
    for(int i = 0; i < content.size(); i++){
        mes_bin += char_to_bin(content[i]);
        if(mes_bin.size() == 64){
            m_mes_data.push_back(mes_bin);
            mes_bin = "";
        }
    }
}

我尝试在用 0 填充之前在内容末尾插入一个 EOF 字符,我还尝试在用 0 填充之前插入“\r\n”以及我在这里使用的指南http://page.math.tu-berlin.de/~kant/teaching/hess/krypto-ws2006/des.htm 但我尝试过的一切似乎都无法解决问题。

关于填充,我是否遗漏了什么,或者在向末尾添加 0 (nul) 之前,我是否需要使用某些特殊字符来结束我的内容?

感谢Joker_vD的解决方案

问题是由于在 openssl PKCS #5(或 PKCS #7 on wikipedia)之后没有正确填充而引起的。通过更改行

if(content.size()%8 != 0) content.append(8-(content.size()%8), 0);

if(content.size()%8 != 0) content.append(8-(content.size()%8), 8-content.size()%8);
else content.append(8, 8);

程序开始正常填充。