为什么显示密码而不显示消息?

时间:2019-05-09 02:06:07

标签: c++ encryption binary codeblocks

我有一个作业问题。 https://gyazo.com/443dd9aee7b2ebe24a3a781d97150a70。最后,我尝试解密密文,但它只是显示密码而不是消息!请帮忙!这将很快到期!

我尝试切换异或门方程,但是没有用。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string m;
    cout << "Please enter your message" << endl;
    getline(cin, m);

    cout << " " << endl;

    char p;
    cout << "Please enter a one-character password" << endl;
    cin >> p;

    cout << " " << endl;

    cout << "Encrypting..." << endl;

    cout << " " << endl;
    string c;

    for (int i = 0; i<m.length(); i++) {
        c += m[i]^p;
    }

    cout << "Your ciphertext is " << endl;
    cout << c << endl;

    cout << " " << endl;

    cout << "Decrypting..." << endl;

    string m2;
    for (int i = 0; i < c.length(); i++) {
        m2 = c[i+1]^p;
    }

    cout << "Your message is" << endl;
    cout << m2 << endl;
    return 0;
}

最后,我真的需要m2作为消息而不是密码来显示。

1 个答案:

答案 0 :(得分:0)

贝塔回答了问题。

我将方程式更改为m2 + = c [i] ^ p;