C语言实现RC4对称加密/解密算法--以前写的,整理了一下

自已写的,代码质量肯定不是很高,存在优化空间是肯定的,可能也存在隐藏的漏洞。

 

以下代码侧重于对数据的加密和解密的算法部分,其他部分的问题没处理,仅供参考。

 

编译器用的是:C-Free 5.0

 

直接源代码:

//RC4算法对数据的加密和解密

#include 
#include 
#include 
#include 
#include 

using namespace std;

/*函数声明*/
void InitSbox(unsigned char sbox[]); 
void KeyExpansion(unsigned char key[], char *k, int len);
void UpsetSbox(unsigned char sbox[], unsigned char key[]);
void DataProcess(unsigned char sbox[], FILE *fp1, FILE *fp2);
void DataEncrypt(char *k, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2);
void DataDecrypt(char *k1, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2);

/*初始化S盒*/
void InitSbox(unsigned char sbox[]){
	for(int i = 0; i < 256; i++)  sbox[i] = i;
}

/*密钥填充256数组*/
void KeyExpansion(unsigned char key[], char *k, int len){
	if(len <= 256){
		for(int i = 0; i < 256; i++) key[i] = k[i % len];
	}
	if(len > 256){
		for(int i = 0; i < 256; i++) key[i] = k[i];
	}
}

/*打乱S盒*/ 
void UpsetSbox(unsigned char sbox[], unsigned char key[]){
	int j = 0;
	unsigned char temp;
	int n;
	for(int i = 0; i < 256; i++){
		n = j + (int)sbox[i] + (int)key[i];
		j = n % 256;
		temp = sbox[i];
		sbox[i] = sbox[j];
		sbox[j] = temp;
	}
}

/*加解密数据*/ 
void DataProcess(unsigned char sbox[], FILE *fp1, FILE *fp2){
	int i, j;
	i = 0; j = 0;
	char ch = fgetc(fp1);
	while(ch != EOF){
		i = (i + 1) % 256;
		int temp2 = j + (int)sbox[i];
		j = temp2 % 256;
		unsigned char temp;
		temp = sbox[i];
		sbox[i] = sbox[j];
		sbox[j] = temp;
		int temp1 = (int)sbox[i] + (int)sbox[j];
		int t = temp1 % 256;
		char k = sbox[t];
		char cipherchar = ch ^ k;
		fputc(cipherchar, fp2);
		ch = fgetc(fp1);
	}
}

/*加密总函数*/
void DataEncrypt(char *k, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2) {
	int len = strlen(k);
    KeyExpansion(key, k, len);
    InitSbox(sbox);
    UpsetSbox(sbox, key);
    DataProcess(sbox, fp1, fp2);
    fclose(fp1);
    fclose(fp2);
    printf("\n加密成功!\n\n");
}

/*解密总函数*/ 
void DataDecrypt(char *k1, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2) {
	int len = strlen(k1);
    KeyExpansion(key, k1, len);
    InitSbox(sbox);
    UpsetSbox(sbox, key);
    DataProcess(sbox, fp1, fp2);
    fclose(fp1);
    fclose(fp2);
    printf("\n解密成功!\n\n");
}

main(){
	char *k = (char *)malloc(25 * sizeof(char));
	char *k1 = (char *)malloc(25 * sizeof(char));
	unsigned char key[256] = {0x00};
	unsigned char sbox[256] = {0x00};
	FILE *fp1, *fp2;
    int flag = 1;
    int choice;
    do{
    	printf("*****************************RC4加密解密文件************************************");
        printf("\n"); 
        printf("                             1.加密文件\n\n");
        printf("                             2.解密文件\n\n");
	    printf("                             3.退出\n\n"); 
	    printf("请选择要进行的操作:");
	    scanf("%d",&choice);
	    switch(choice){
		    case 1: fp1 = fopen("源文件.txt","r");
		            if(fp1 == NULL){
    	                printf("打开源文件失败!\n");
    	                getchar();
    	                exit(0);
                    }
                    fp2 = fopen("加密后文件.txt","w");
                    if(fp2 == NULL){
    	                printf("打开加密后文件失败!\n");
    	                getchar();
    	                exit(0);
                    }
			        printf("\n请输入加密密钥:");
	                cin >> k;
	                DataEncrypt(k, key, sbox, fp1, fp2);
                    break;
			case 2: fp1 = fopen("加密后文件.txt","r");
			        if(fp1 == NULL){
    	                printf("打开加密后文件失败!\n");
    	                getchar();
    	                exit(0);
                    }
			        fp2 = fopen("解密后文件.txt","w");
			        if(fp2 == NULL){
    	                printf("打开解密后文件失败!\n");
    	                getchar();
    	                exit(0);
                    }
                    printf("\n请输入解密密钥:");
                    cin >> k1;
                    DataDecrypt(k1, key, sbox, fp1, fp2);
                    break;
            case 3: flag = 0;break;
            default : printf("\n操作不合法!\n\n");
	    }
    }while(flag);
}


 

运行结果:

1.源文件为:

C语言实现RC4对称加密/解密算法--以前写的,整理了一下_第1张图片

2.进入加密模块,输入加密密钥:helloword

C语言实现RC4对称加密/解密算法--以前写的,整理了一下_第2张图片

3.加密后文件:

C语言实现RC4对称加密/解密算法--以前写的,整理了一下_第3张图片

4.进入解密模块,输入解密密钥:helloword

C语言实现RC4对称加密/解密算法--以前写的,整理了一下_第4张图片

5.解密后文件:

C语言实现RC4对称加密/解密算法--以前写的,整理了一下_第5张图片

 

你可能感兴趣的:(C)