【发布时间】:2021-12-27 02:09:15
【问题描述】:
我正在编写一个可以使用密码 RC4 加密文件的程序, 在我的文件“test.txt”中是一个单词“Plaintext”, 该程序应该加密并将其保存在一个文件中(我使用“cout”进行测试)
我觉得代码的最后一部分有问题
while ( plik.read(&x,1) )
{
i = ( i + 1 ) % 256;
j = ( j + S [ i ] ) % 256;
swap( S [ i ], S [ j ] );
temp = S [ ( S [ i ] + S [ j ] ) % 256 ] ^ x;
cout << temp;
}
没有错误也没有警告, 我应该改变什么?
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
unsigned char S[256];
int i = 0;
for ( i = 0; i < 256; i++ )
S [ i ] = i;
string Key;
cout << "Enter the key: ";
cin >> Key;
int j = 0;
for ( i = 0; i < 256; i++ )
{
j = ( j + S [ i ] + Key.at( i % Key.length() ) ) % 256;
swap( S [ i ], S [ j ] );
}
ifstream plik;
string path = "tekst.txt";
plik.open( path );
char tekst;
plik >> tekst;
string printFile = "Encryption_" + path;
ofstream outfile( printFile );
char x;
j = 0;
i = 0;
string temp;
while ( plik.read(&x,1) )
{
i = ( i + 1 ) % 256;
j = ( j + S [ i ] ) % 256;
swap( S [ i ], S [ j ] );
temp = S [ ( S [ i ] + S [ j ] ) % 256 ] ^ x;
cout << temp;
}
plik.close();
outfile.close();
return 0;
}
我的钥匙是:“钥匙”,结果应该是
【问题讨论】:
-
是的,我正在控制台中写入结果(仅用于测试,我将在最后更改),我编辑了一个帖子,您可以看到我的结果
-
您可以找到参考测试示例here in wiki。
标签: c++ encryption