RC4算法示例
来源:互联网 发布:淘宝买家延期收货 编辑:程序博客网 时间:2024/05/16 13:01
//key为密钥~
#include<iostream>
#include<string>
using namespace std;
int s[256];
int t[256];
string key="0123456789";
void swap(int* s1,int* s2){
int temp=*s1;
*s1=*s2;
*s2=temp;
}
void init(){
//初始化s和t数组
for(int i=0;i<256;i++){
s[i]=i;
t[i]=atoi(&(const char)(key.at(i%key.length()))); // t保存char的地址?
}
int j=0;
for(int i=0;i<256;i++){
j=(j+s[j]+t[j])%256;
swap(s+i,s+j); // 打乱S数组?
}
}
void encode(int* start,int length){
int pos=0;
int i=0,j=0;
while(pos++<length){
i=(i+1)%256;
j=(j+s[i])%256;
swap(s+i,s+j); //再次打乱?
int temp=(s[i]+s[j])%256;
int k=s[temp];//这里取值
*start^=k;//异或
start++;
}
}
void decode(int* start,int length){
encode(start,length);//重复做一遍即可解密
}
void print(int* a,int length,string startMessage=""){
cout<<startMessage<<" ";
for(int i=0;i<length;i++){
cout<<*(a+i)<<" ";
}
cout<<endl;
}
int main(){
init();
int a[10]={0,1,2,3,4,5,6,7,8,9};
print(a,10,"原数组");
encode(a,10);
print(a,10,"加密后");
init();
decode(a,10);
print(a,10,"解密后");
}
- RC4算法示例
- RC4算法
- RC4算法
- RC4算法
- RC4算法
- RC4算法
- RC4 算法
- rc4算法
- RC4算法
- RC4 算法 VB 实现
- RC4加解密算法
- RC4加解密算法
- rc4算法分析
- RC4算法Python实现
- Rc4 算法原理
- RC4密码算法实现
- RC4算法实现
- RC4算法实现
- TCP/IP协议族简介 — TCP/IP协议栈之UDP协议
- Win7 配置Apache+PHP+Mysql环境
- 感言
- 结构体与TByteDynArray转换
- Visual Studio使用小技巧5 – 区块选择(box selection)的拷贝(copy)和粘贴(paste)
- RC4算法示例
- Android入门讲解(一)
- java处理paypal返回值
- 当前安全设置禁止运行该页中的ActiveX 控件 解决方法
- 编辑器块选择
- 什么是FPGA? MCU?DSP?
- SQL Server数据库创建基础
- 2011最伤
- 心情不错