【发布时间】:2015-12-02 23:06:03
【问题描述】:
这个问题要我编写一个一次性小键盘密码系统,其中消息比密钥长。该程序将使用 srand() 和 rand() 生成从 0 到 4 的数字(如果键有 5 个字母)并将它们用于整个句子。 srand 使用种子,它是初始键的字母的 ASCII 值的总和。现在的问题是加密的句子和例子不一样(下附)。
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
using namespace std;
int main ()
{
string message, key;
int messagevalue, keyvalue, i=0, j=0, seed=0;
cout<<"Enter the message: ";
getline(cin, message);
cout << "Enter the key: ";
getline(cin, key);
messagevalue = message.length();
keyvalue = key.length();
while (key[i] != 0)
{
i++;
j++;
}
cout << "The message is: " << setw(20) << message << endl;
for (i=0; i < messagevalue; i++)
{
seed = seed + key[i];
}
srand(seed);
for(i=0; i < messagevalue; i++)
{
message[i] = 65 + (message[i] + key[rand()%j]) % 26;
}
cout <<"The cipher is: " << setw(21) << message <<endl;
for(i=0; i < messagevalue; i++)
{
message[i] = 65 + 26 + (message[i] - key[rand()%j]) % 26;
if ((message[i] - key[i]) >= 0)
{
message[i] = message[i] - 26;
}
}
cout << "The message again is: " << setw(14) << message << endl;
return 0;
}
【问题讨论】:
-
我看不出您的输入和解密的消息之间有什么区别。或者这就是它的本意,但你没有得到那个?如果是这样,您可以发布您的代码生成的内容吗?
-
是的,这就是它应该如何的示例。我得到 UIWUMTOUVWHHJHJFQGIXWOSDCWQKIYIV 和解密更糟
-
没有理由期望加密文本与示例相同,除非您使用与创建示例的人相同的编译器和运行时库。当然,您应该能够取回原始文本。
-
请注意,
srand()和rand()不适合进行严重加密。如果您进行非严重加密,则可以。 -
@KeithThompson 实际上是作业
标签: c++ encryption random codeblocks srand