【发布时间】:2015-03-12 06:30:48
【问题描述】:
我正在尝试在加密/解密程序中使用完整的用户输入字符串作为密钥。我还试图弄清楚如何将加密输出为原始输入的单词,但只使用空格,然后再次加密(解密)以使单词恢复正常。
例如,用户可以输入自己的句子 (hello world) 和自己的密钥 (testkey),然后程序使用 testkey 对其进行加密,例如将结果输出为“h e l l o w o r l d”。然后再次加密并返回“hello world”。
任何帮助都将不胜感激,即使您可以帮助获取用户输入的密钥或加密输出。谢谢!
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string sentence = "";
string encrypted = "";
string unencrypt = "";
char key[] = "";
cout << "Enter sentence: ";
getline(cin, sentence);
cout << "Enter key: ";
cin >> key;
for (int temp = 0; temp < sentence.size(); temp++){
encrypted += sentence[temp] ^ (int(key) + temp) % 2;
}
cout << "Encrypted = " << encrypted;
for (int temp = 0; temp < sentence.size(); temp++){
unencrypt += sentence[temp] ^ (int(key) + temp) % 2;
}
cout << endl;
cout << "Unencrypted = " << unencrypt;
return 0;
}
【问题讨论】:
-
我不会称之为加密。
-
是的,我知道,但这显然是程序的一部分...
-
我怀疑你打算用 地址 异或。
-
cin >> key;尝试读入一个单字符数组(""的 NUL 终止符)...您几乎无法将密钥放入其中。使用另一个std::string或int,如果这是您最终想要得到的。顺便说一句,std::string构造函数会创建一个空字符串...不需要= ""。
标签: c++ encryption xor