信安实验-RC4加密算法

                                                                        RC4加密算法

  算法具体就不介绍了,应信安老师要求整理及掌握。

#include<bits/stdc++.h>
using namespace std;
const int N=256;
char m[N]= {"this is a test"}; //明文与密文
int len,s[N],t[N],c[N],key[N];
//本次试验n=3,故随机选取密钥5 6 7对t数组依次填充
void init()//初始化
{
    int j=0;
    for(int i=0; i<N; i++) s[i]=i; //对s-box线性填充
    for(int i=0; i<N; i++) //依次填充辅助数组
    {
        if(i%3==0)  t[i]=5;
        else if(i%3==1) t[i]=6;
        else t[i]=7;
        j=(j+s[i]+t[i])%N;
        swap(s[j],s[i]);
    }
}
void RC4_add()
{
    int i=0,j=0;
    for(int k=0; k<len; k++)
    {
        i=(i+1)%N;
        j=(j+s[i])%N;
//        swap(s[i],s[j]);
        int tmp=(s[i]+s[j])%N;
        m[k]^=s[tmp];
        printf("%c",m[k]);
    }
}
int main()
{
    len=strlen(m);
    init();
    printf("明文:%s\n",m);
    printf("加密:");
    RC4_add();
    printf("\n解密: ");
    RC4_add();
}
 










转载于:https://www.cnblogs.com/nyist-TC-LYQ/p/7208100.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值