Encryption Rc4算法的实现

Encryption Rc4算法的实现,encryption,cryptography,rc4-cipher,Encryption,Cryptography,Rc4 Cipher,我需要实现一个带有种子的Rc4算法:1 2 3 6和纯文本密码学。我遵循课堂上提供的指导原则,但它没有正确初始化 我的输出是 而且需要 我的代码以前打印负值,不知道为什么,但我设法修复了这个错误。我以为一切都很好,但事实并非如此。对于这些图片我感到很抱歉,我认为解释我的代码结构更容易。我是mod 4种子,因为它包含4个字符,这可能是我的错误吗 #include <iostream> #include <string> #include <string.h>

我需要实现一个带有种子的Rc4算法:1 2 3 6和纯文本密码学。我遵循课堂上提供的指导原则,但它没有正确初始化

我的输出是

而且需要

我的代码以前打印负值,不知道为什么,但我设法修复了这个错误。我以为一切都很好,但事实并非如此。对于这些图片我感到很抱歉,我认为解释我的代码结构更容易。我是mod 4种子,因为它包含4个字符,这可能是我的错误吗

#include <iostream>
#include <string>
#include <string.h>


using std::endl;
using std::string;

void swap(unsigned int *x, unsigned int *y);



int main()
{
string plaintext = "cryptology";

char cipherText[256] = { ' ' };
unsigned int S[256] = { 0 };
unsigned int t[256] = { 0 };
unsigned int seed[4] = { 1, 2, 3, 6 };      // seed used for test case 1 
unsigned int temp = 0;
int runningTotal = 0;

unsigned int key = 0;



// inilializing s and t
for (int i = 0; i < 256; i++)
{

    S[i] = i;
    t[i] = seed[i % 4];
}

for (int i = 0; i < 256; i++)
{
    runningTotal += S[i] + t[i];
    runningTotal %= 256;
    swap(&S[runningTotal], &S[i]);

     std::cout  << S[i] <<" ";
}
runningTotal = 0;

for (int i = 0; i < plaintext.size(); i++)
{
    runningTotal %= 256;
    swap(&S[i], &S[runningTotal]);

    temp = (unsigned int)S[i] + (unsigned int)S[runningTotal];
    temp %= 256;

    key = S[temp];
    std::cout << endl;
    cipherText[i] = plaintext[i] ^ key;



}
std::cout << " this is cipher text " << endl;
std::cout << cipherText << endl;






system("pause");
return 0;
}
void swap(unsigned int *x, unsigned int *y)
{
unsigned int temp = 0;

temp = *x;
*x = *y;
*y = temp;






}
#包括
#包括
#包括
使用std::endl;
使用std::string;
无效交换(无符号整数*x,无符号整数*y);
int main()
{
字符串明文=“密码学”;
字符密文[256]={''};
无符号整数S[256]={0};
无符号整数t[256]={0};
unsigned int seed[4]={1,2,3,6};//用于测试用例1的seed
无符号整数温度=0;
int runningTotal=0;
无符号整数键=0;
//对s和t进行初始化
对于(int i=0;i<256;i++)
{
S[i]=i;
t[i]=种子[i%4];
}
对于(int i=0;i<256;i++)
{
运行总数+=S[i]+t[i];
runningTotal%=256;
掉期(&S[runningTotal],&S[i]);

std::cout事实上,我认为您正确地生成了
S[]
。我只能假设您应该对密钥执行不同的操作。(可能是ASCII字符串而不是四字节值?请检查您的作业说明。)

但是,稍后会有一个问题。在流生成循环中,您应该这样做

for(int k=0;k
注意:虽然与您的问题无关,但使用
无符号字符
值而不是
int
s可以使代码更加整洁。这将消除到处乱放的
%256
指令。(但在初始化过程中要小心,因为
i