#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int power(int j)
{
int i=0,result=1;
while(i++!=j)
result*=2;
return result;
}
void keyScheduling (vector<int> & seed, vector<int> & key)
{
int i, j;
for(i = 0; i <= 255; i++)
{
seed[i] = i;
}
j = 0;
for( i = 0; i <= 255; i++)
{
j = (j + seed[i] + key[i % key.size()]) % 256;
swap(seed[i], seed[j]);
}
}
void pseudoRandom (vector<int> & seed, int outputLength)
{
int i = 0;
int j = 0;
while (outputLength > 0)
{
i = (i + 1) % 256;
j = (j + seed[i]) % 256;
swap(seed[i], seed[j]);
cout << hex << (seed[(seed[i] + seed[j]) % 256]);
outputLength--;
}
}
void vecToBits (vector<int> & ivec, vector<int> & key)
{
int buf;
for (int i = 0; i <= ivec.size()-1; i++)
{
for (int j = 7; j >= 0; j--, ivec[i]>>=1)
key.push_back(0x01 & ivec[i]);
for (int j = 0; j <= 3; j++)
{
buf = key[8*i+j];
key[8*i+j] = key[8*i+7-j];
key[8*i+7-j] = buf;
}
}
}
int main()
{
string asciiKey;
int outputLength;
vector<int> seed, castVec, key;
seed.resize(256);
cout << "Please enter your key (Ascii 1~32 characters) and output length (int in bytes):" << endl;
cin >> asciiKey >> outputLength;
castVec.resize(asciiKey.size());
for (int i = 0; i <= asciiKey.size()-1; i++)
{
castVec[i] = static_cast<int> (asciiKey[i]);
}
vecToBits(castVec, key);
keyScheduling(seed, key);
pseudoRandom(seed, outputLength);
return 0;
}
RC4.zip_RC4 Stream CIpher_rc4
版权申诉
161 浏览量
2022-09-20
20:17:48
上传
评论
收藏 1KB ZIP 举报
APei
- 粉丝: 63
- 资源: 1万+
最新资源
- 基于python+JavaScript实现的医学影像分割+源代码+文档说明+截图演示+数据(高分毕业设计)
- 基于U-net+pytorch实现的医学影像分割python源码+文档说明+数据+界面截图+博客介绍
- 课程设计-基于Pytorch实现MNIST数据集的手写数字识别源码+数据(Gui界面)+文档说明+模型
- 软件开发国家标准.xls
- pytorch-CNN-SBATM-ubuntudemo
- matplotlibdemo
- pytorch-CNN-dht11温湿度传感器笔记
- 软件研发管理体系文件清单(推荐文档).xls
- 软件资产管理台账(精品文档).xls
- shenling-master.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈