问题描述
说明:
我正在尝试编写一个简单的程序,用于读取短语,然后将其加密,然后将加密的短语输出到终端窗口。请参阅下面的代码了解更多信息
Description:
I am trying to write a simple program for fun that will read in a phrase then xor encrypt it then output the encrypted phrase to the terminal window. See code below for more info.
代码:
#include
#include
using namespace std;
code:
#include #include using namespace std;
int main ()
{
string mystr;
cout << "What's the phrase to be Encrypted? ";
char key[11]="ABCDEFGHIJK"; //The Encryption Key, for now its generic
getline(cin, mystr);
string result;
for (int i=0; i<10; i++) {
result.push_back(mystr[i] ^ key[i]);
cout << result[i];
}
cout << "\n";
return 0;
}
结果:
以上代码可以工作,当我输入一个非常长的字符串,它只会加密前10个字符(我认为)。我想要能够输入一个大字符串加密它与11位密钥,然后输出到终端。我如何做到这一点?
Results:
The code above works however When I input a very long string it only encrypts the first 10 characters (I think). I would like to be able to input a large string encrypt it with the 11 bit key then output it to the terminal. How do I do this?
另外:
我已经问过这个问题,这个问题位于这个问题:
帮助:
如果您有任何想法如何解决这个问题,请给我一个例子,说明我想要的是什么,或者说我需要什么。
help:
If you have any idea how to fix this could you please give an example of either what Im missing or what I need with explanation.
推荐答案
您只循环了超过10个字符,您的for循环定义为(int i = 0; i< 10; i ++ )
。你想循环你的整个字符串长度,然后用你的键mod 11 XOR。
You're only looping over 10 characters as defined by your for loop for (int i=0; i<10; i++)
. You want to loop over your entire string length and then XOR with your key mod 11.
for (int i=0; i<mystr.size(); i++) {
result.push_back(mystr[i] ^ key[i%11]);
cout << result[i];
}
这篇关于非常长的字符串输入到xor加密程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!