RC4算法和标准结果不一样,大家帮忙看看哪里错了
以下是我自己写的rc4的代码,以"abcdefgh"为密钥和明文,结果是 3364d84e0e72bfdcopenssl里的rc4,以"abcdefgh"为密钥和明文,结果是 3065df4f0d73b0b4
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 256
#define Bufsize 1024
unsigned char Box_256[N];
unsigned char Key_256[N];
void usage(void);
void Init();
void Init_Box_256();
void Init_Key_256();
void Encrypt();
unsigned char GetKey(int i);
template <typename T>
void Swap(T &a,T &b);
int main()
{
usage();
Init();
Encrypt();
system("pause");
return 0;
}
void usage(void)
{
printf(" hahaha \n");
}
void Init()
{
Init_Key_256();
Init_Box_256();
// return 0;
}
void Init_Box_256()
{
int i,j;
for(i=0;i<N;i++)//填充0-255
Box_256[i]=i;
for(i=0,j=0;i<N;i++)//按算法交换
{
j=(j+Box_256[i]+Key_256[i])%N;
Swap(Box_256[i],Box_256[j]);
}
}
void Init_Key_256()
{
int i,len;
unsigned char *Key;
Key=(unsigned char *)malloc(sizeof(unsigned char)*Bufsize);
printf("Please input the Key:");
scanf("%s",Key);
len=strlen((const char *)Key);
// j=strlen(Key);
while(len>N)
{
printf("too long for Key\n.Please input the Key:");
memset(Key,0,Bufsize);
scanf("%s",Key);
len=strlen((const char *)Key);
}
for(i=0;i<N;i++)//密钥填充满256字节
Key_256[i]=Key[i%len];
free(Key);
printf("11\n");
}
template <typename T>
void Swap(T &a,T &b)
{
T temp;
temp=a;
a=b;
b=temp;
}
void Encrypt()
{
int i,j,k,len;
unsigned char Key_temp;
unsigned char *ming;
ming=(unsigned char *)malloc(sizeof(unsigned char)*Bufsize);
printf("input data for encrypt:");
scanf("%s",ming);
len=strlen((const char *)ming);
for(i=0,j=0,k=0;k<len;k++)
{
// GetKey[k]
i=(i+1)%N;
j=(j+Box_256[i])%N;
Swap(Box_256[i],Box_256[j]);
Key_temp=(Box_256[i]+Box_256[j])%N;
Key_temp=Box_256[Key_temp];
printf("%02x",ming[i]^Key_temp);
}
free(ming);
}
void Decrypt()
{
Encrypt();
}








