编写一个与我的密码程序类似的密码程序:•提示用户输入密钥并使用它来计算随机数生成器的种子•提示用户提供输入文件的名称和输出编码/解码文件• 使用从用户密钥获得的种子从随机数生成器创建随机字节序列。• 使用随机位 r 对位 x 进行编码,如下所示: x⊕ = r • 由于∀r ∈{0,1},r ⊕r = 0,使用相同的随机字节序列和相同的操作 x 执行解码⊕ = r。解码基于运算 x⊕r ⊕r = x⊕(r ⊕r) = x⊕0 = x
下面是用于加密的代码,但在解密时,密文仅包含 3 个字符。我无法弄清楚为什么解码不起作用。我正在使用 Dev-C++。您的帮助将不胜感激。
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
char n, line[1024], keystring[100]; char FitoEncr[100], NewFiCrypt[100];
char FiletobeDecrypted[100];
ifstream IS ("FitoEncr.txt", ios::in);
ofstream OS ("NewFiCrypt.txt", ios::out);
unsigned int psswd=0, number;
cout<<"Please, enter a secret key :";
cin.getline(keystring, 100);
for( int i=0;keystring[i]!='\0'; i++)
psswd=(psswd+3)*keystring[i];
cout<<"initial password: "<<keystring<<endl;
cout<<"encrypted password: "<<psswd<<endl;
cout<<"please, enter the name of the input file: ";
cin.getline(FitoEncr,20);
cout<<"please, enter the name of the output file: ";
cin.getline(NewFiCrypt,20);
srand(psswd); //not sure about creating the sequence of random bytes from a...
number=rand() % 255; //random number generator with the seed obtained from user's secret key
//ENCRYPTION
while(IS.get(n))
{
if(IS.get(n))
{
n^=rand();
OS<<n;
cout<<"character coded:"<<n;
}
}
IS.close();
OS.close();
//DECRYPTION
ifstream IS1;
ofstream OS1;
IS1.open("NewFiCrypt.txt", ios::in);
OS1.open("FilDecr.txt", ios::out);
while(IS1.get(n))
{
if(IS1.get(n))
{
n^=rand(); //I think the problem starts on this line...
OS1<<n;
cout<<"character coded:"<<n;
}
}
IS1.close();
OS1.close();
getch();
return 0;
}