RC4加密cpp算法
RC4 encryption cpp algorithm
我正在编写一个可以使用密码 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;
}
我的键是:“键”,结果应该是
Good Result
你的算法在数学上完全正确,我只是对你代码的其他部分做了一些小的修正,你的代码输出正确。
出于测试目的,让我们使用测试示例 here from Wiki,第一个示例。输入键将为 Key
,输入文件将为 Plaintext
,结果文件应为十六进制的 BB F3 16 E8 D9 40 AF 0A D3
。以下修改后的代码通过了此测试。
在下面的代码中,您从控制台输入了一个键,然后输入文件取自 tekst.txt
,输出文件写入 Encryption_tekst.txt
。最好不要将结果作为 ASCII 字符打印到控制台,而是在十六进制查看器中查看生成的文件,因为控制台会弄乱字符编码,最好以十六进制查看。
我添加了打印结果十六进制字符的控制台。另外 注意 在下面的代码中,第一个块将 Plaintext
写入 tekst.txt
,我这样做是为了举例,这样代码就可以复制粘贴了运行 没有任何额外的依赖。您必须删除写入 teks.txt
的第一个块,因为您有自己的输入 tekst.txt
,它将被覆盖。
当您 运行 以下示例在控制台提示符中输入 Key
时。
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdint>
using namespace std;
int main() {
{
ofstream out("tekst.txt");
out << "Plaintext";
}
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);
string printFile = "Encryption_" + path;
ofstream outfile(printFile);
char x;
j = 0;
i = 0;
char temp = 0;
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;
outfile << temp;
cout << std::hex << std::setw(2) << std::setfill('0')
<< uint32_t(uint8_t(temp)) << " ";
}
plik.close();
outfile.close();
return 0;
}
输入键(来自控制台):
Key
输入文件tekst.txt
:
Plaintext
控制台输出:
Enter the key: Key
bb f3 16 e8 d9 40 af 0a d3
在十六进制查看器中使用代码页 CP1252
:
查看的输出 Encryption_tekst.txt
我正在编写一个可以使用密码 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;
}
我的键是:“键”,结果应该是
Good Result
你的算法在数学上完全正确,我只是对你代码的其他部分做了一些小的修正,你的代码输出正确。
出于测试目的,让我们使用测试示例 here from Wiki,第一个示例。输入键将为 Key
,输入文件将为 Plaintext
,结果文件应为十六进制的 BB F3 16 E8 D9 40 AF 0A D3
。以下修改后的代码通过了此测试。
在下面的代码中,您从控制台输入了一个键,然后输入文件取自 tekst.txt
,输出文件写入 Encryption_tekst.txt
。最好不要将结果作为 ASCII 字符打印到控制台,而是在十六进制查看器中查看生成的文件,因为控制台会弄乱字符编码,最好以十六进制查看。
我添加了打印结果十六进制字符的控制台。另外 注意 在下面的代码中,第一个块将 Plaintext
写入 tekst.txt
,我这样做是为了举例,这样代码就可以复制粘贴了运行 没有任何额外的依赖。您必须删除写入 teks.txt
的第一个块,因为您有自己的输入 tekst.txt
,它将被覆盖。
当您 运行 以下示例在控制台提示符中输入 Key
时。
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdint>
using namespace std;
int main() {
{
ofstream out("tekst.txt");
out << "Plaintext";
}
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);
string printFile = "Encryption_" + path;
ofstream outfile(printFile);
char x;
j = 0;
i = 0;
char temp = 0;
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;
outfile << temp;
cout << std::hex << std::setw(2) << std::setfill('0')
<< uint32_t(uint8_t(temp)) << " ";
}
plik.close();
outfile.close();
return 0;
}
输入键(来自控制台):
Key
输入文件tekst.txt
:
Plaintext
控制台输出:
Enter the key: Key
bb f3 16 e8 d9 40 af 0a d3
在十六进制查看器中使用代码页 CP1252
:
Encryption_tekst.txt