【问题标题】:RC4 encryption cpp algorithmRC4加密cpp算法
【发布时间】: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;
}

我的钥匙是:“钥匙”,结果应该是

Good Result

【问题讨论】:

  • 是的,我正在控制台中写入结果(仅用于测试,我将在最后更改),我编辑了一个帖子,您可以看到我的结果
  • 注意明文和密钥的大小写。您发布的PlaintextKey(不是key)结果的密文,s。 hereHere 你可以在 C/C++ 中找到一个可行的 RC4 实现。也许您可以通过比较逻辑来确定问题。
  • 您可以找到参考测试示例here in wiki

标签: c++ encryption


【解决方案1】:

你的算法在数学上是完全正确的,我只是对你代码的其他部分做了一些小的更正,你的代码得到了正确的输出。

出于测试目的,让我们以测试示例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

Try it online!

#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

输出Encryption_tekst.txt 在十六进制查看器中查看,代码页为CP1252

【讨论】:

  • 非常感谢@Arty,我已经添加了您的更正,并且效果很好
  • @Wujas 感谢您的接受和支持 :) 您有一个非常有趣的问题。我第一次有机会研究这个 RC4 算法来了解它是如何工作的。非常简单的算法,可能很弱,但实现起来很简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
相关资源
最近更新 更多