标题:哪个帅哥,给我改改 输出的S【i】要二进制的 本人有点笨
只看楼主
只应为你
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-3-16
 问题点数:0 回复次数:0 
哪个帅哥,给我改改 输出的S【i】要二进制的 本人有点笨
#include<stdio.h>

#include<string.h>

typedef unsigned long ULONG;

void rc4_init(unsigned char *s, unsigned char *key, unsigned long Len) //初始化函数

{

int i =0, j = 0;

char k[256] = {0};

unsigned char tmp = 0;

for(i=0;i<256;i++)

{

s[i]=i;

k[i]=key[i%Len];

}

for (i=0; i<256; i++)

{

j=(j+s[i]+k[i])%256;

tmp = s[i];

s[i] = s[j]; //交换s[i]和s[j]

s[j] = tmp;

}

}

void rc4_crypt(unsigned char *s, unsigned char *Data, unsigned long Len) //加解密

{

int i = 0, j = 0, t = 0;

unsigned long k = 0;

unsigned char tmp;

for(k=0;k<Len;k++)

{

i=(i+1)%256;

j=(j+s[i])%256;

tmp = s[i];

s[i] = s[j]; //交换s[x]和s[y]

s[j] = tmp;

t=(s[i]+s[j])%256;

Data[k] ^= s[t];

}

}

void main()

{

unsigned char s[256] = {0},s2[256] = {0};    //S-box

char key[256] = {"just for test"};

char pData[512] = "这是一个用来加密的数据Data";

ULONG len = strlen(pData);

printf("pData = %s\n",pData);

printf("key = %s, length = %d\n\n",key,strlen(key));

rc4_init(s,(unsigned char *)key,strlen(key));   //已经完成了初始化

printf("完成对S[i]的初始化,如下:\n\n");

for (int i=0; i<256; i++)

{

printf("%-3d ",s[i]);

}

printf("\n\n");

for(i=0;i<256;i++)//用s2[i]暂时保留经过初始化的s[i],很重要的!!!

{

s2[i]=s[i];

}

printf("已经初始化,现在加密:\n\n");

rc4_crypt(s,(unsigned char *)pData,len);//加密

printf("pData = %s\n\n",pData);

printf("已经加密,现在解密:\n\n");

rc4_init(s,(unsigned char *)key, strlen(key));   //初始化密钥

rc4_crypt(s2,(unsigned char *)pData,len);//解密

printf("pData = %s\n\n",pData);

}
搜索更多相关主题的帖子: 二进制 include 帅哥 
2017-03-16 15:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-475144-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.110817 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved