lab3 code.txt
资源名称:rc4code.rar [点击查看]
上传用户:jbtbattery
上传日期:2022-08-06
资源大小:1k
文件大小:2k
源码类别:
加密解密
开发平台:
Visual C++
- /*RC4的加密程序*/
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- void swap(unsigned char &s,unsigned char &t) //交换函数
- {
- char temp;
- temp=s;
- s=t;
- t=temp;
- }
- void Encrypt(char *inputfile, char *outputfile, char *key) //加密函数
- {
- unsigned char S[256];
- unsigned char T[256];
- unsigned char temp = 0;
- unsigned char plaint,cipher;
- long filelength = 0;
- int i=0;
- int j = 0;
- int t;
- unsigned char k;
- t=k = 0;
- for (i=0; i<256; i++) //初始化S
- {
- S[i] = i;
- T[i] = key[i % strlen(key)];
- }
- for ( i=0; i<256; i++) //用T产生的S的初始置换
- {
- j = (j + S[i] + T[i] ) % 256;
- swap(S[i],T[i]); //S[i]和T[i]进行交换
- }
- i = j =0;
- FILE *sourFile, *destFile;
- sourFile = fopen(inputfile, "rb"); //打开文件
- destFile = fopen(outputfile, "wb"); //打开文件
- fseek(sourFile, 0L, SEEK_END); //把sourFile指针移动到文件尾部
- filelength = ftell(sourFile); //计算文件长度
- fseek(sourFile, 0L, SEEK_SET); //把sourFile指针移动到文件开头
- for (i=0; i<filelength; i++) //密钥流生成
- {
- j = (i+1) % 256;
- j = (j + S[i]) % 256;
- swap(S[i],S[j]); //S[i]和S[j]进行交换
- t= (S[i] + S[j]) % 256;
- k = S[t];
- fread(&plaint, 1,1, sourFile); //读取文件
- cipher = plaint ^ k;
- fwrite(&cipher, 1,1, destFile); //写入文件操作
- }
- fclose(sourFile); //关闭文件sourFile
- fclose(destFile); //关闭文件destFile
- }
- int main(int argc ,char *argv[])
- {
- if(argc!=6)
- {
- puts("参数是:s-des cipher -e/-d key inputfile outputfile");
- return 0;
- }
- Encrypt(argv[4],argv[5],argv[3]);
- return 0;
- }