rc4 java实现_实现Rc4算法

我需要实现一个种子的Rc4算法:1 2 3 6和纯文本密码学 . 我遵循我们在课堂上提供的这个准则,但它没有正确初始化S.

rUdMB.png

我的输出是

aea2bcd2603629f6adf95ea95e060bea.png

并且需要

Ci3me.png

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

#include

#include

#include

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值