如何接受字符串作为异或加密程序的密钥



我正在尝试在加密/解密程序中使用完整的用户输入字符串作为密钥。我还在尝试弄清楚如何将加密输出为原始输入的单词,但只带有空格,然后再次加密(解密)以使单词恢复正常。

因此,例如,用户可以输入自己的句子(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;
}

检查密钥派生函数。通常,此类密钥使用盐进行哈希处理,并且固定大小的哈希值可以用作密钥。盐是随机的。

请参阅 http://en.wikipedia.org/wiki/PBKDF2http://en.wikipedia.org/wiki/Key_derivation_function它是派生密钥的标准。

最新更新