【问题标题】:How to accept string as key for XOR Encryption program?如何接受字符串作为 XOR 加密程序的密钥?
【发布时间】: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 &gt;&gt; key; 尝试读入一个单字符数组("" 的 NUL 终止符)...您几乎无法将密钥放入其中。使用另一个std::stringint,如果这是您最终想要得到的。顺便说一句,std::string 构造函数会创建一个空字符串...不需要= ""

标签: c++ encryption xor


【解决方案1】:

检查密钥派生功能。通常,这些键是用盐进行散列的,并且可以使用固定大小的散列值作为键。盐是随机的。

http://en.wikipedia.org/wiki/PBKDF2 http://en.wikipedia.org/wiki/Key_derivation_function 它是派生密钥的标准。

【讨论】:

  • 这仍然允许用户输入密钥吗?
  • 是的。有用户可以输入的密码。
  • 老实说,我认为这超出了我目前的技能水平,也达不到要求。不过谢谢!我去看看。
  • 如果您可以使用 OpenSSL C API,这实际上很容易。
  • 好的,我去看看!
猜你喜欢
  • 1970-01-01
  • 2014-12-18
  • 2010-11-22
  • 2022-06-14
  • 2011-01-09
  • 2020-03-20
  • 2011-03-21
  • 1970-01-01
  • 2019-03-17
相关资源
最近更新 更多