code.txt
资源名称:s-des.rar [点击查看]
上传用户:arge456321
上传日期:2022-08-05
资源大小:2k
文件大小:5k
源码类别:
加密解密
开发平台:
Visual C++
- // s-des.cpp : 定义控制台应用程序的入口点。
- //实现S-DES对任意文件的加解密
- #include "stdafx.h"
- #include <string.h>
- #include <windows.h>
- #include "stdafx.h"
- #include <stdio.h>
- int p10[10]={3,5,2,7,4,10,1,9,8,6}; //P10的置换表
- int p8[8]={6,3,7,4,8,5,10,9}; //P8的置换表
- int ip[8]={2,6,3,1,4,8,5,7}; //iP的置换表
- int ip_1[8]={4,1,3,5,7,2,8,6}; //iP_1的置换表
- int ep[8]={4,1,2,3,2,3,4,1}; //ep的置换表
- int P4[4]={2,4,3,1}; //P4的置换表
- int s0[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};
- int s1[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
- void Permutation(int in[],int out[], int table[], int tablelength)
- { //P10、p8、ip、ep、ip_1、p4的置换操作,结果保存在out中
- int i = 0;
- for ( i=0; i<tablelength; i++)
- out[i] = in[ table[i] -1 ];
- }
- void Amalgmate(int word[],int l_word[],int r_word[],int length) //合并两个数组,并保存在word中
- {
- int i;
- for(i=0;i<length;i++)
- {
- if(i<length/2)
- word[i]=l_word[i];
- else
- word[i]=r_word[i-length/2];
- }
- }
- void Subkey(int key[], int subkey[], int N) //循环左移 N次 (N为多少就左移几位,循环一次得到K1,循环2次得到K2)
- {
- int i,l_temp[5]={0},r_temp[5]={0};
- for(i=0;i<5;i++)
- {
- l_temp[i]=key[(i+N)%5];
- r_temp[i]=key[5+(i+N)%5];
- }
- Amalgmate(key,l_temp,r_temp,10);
- Permutation(key,subkey,p8,8); //P8操作,得到 subkey
- }
- void XOR(int key1[],int key2[],int length1) //异或操作,把结果保存在key1中
- {
- int i;
- for(i=0;i<length1;i++)
- key1[i]=key1[i]^key2[i];
- }
- void swap(int data[]) //SW交换函数,用于高4位于低4位进行交换
- {
- int temp =0;
- int i=0;
- for (i=0; i<4; i++)
- {
- temp = data[i+4];
- data[i+4] = data[i];
- data[i] = temp;
- }
- }
- void SBox(int input[], int output[]) //Sbox操作,结果保存在output中
- {
- int row,col,value;
- row = col = value = 0;
- row = input[0]*2+input[3]; //S0的操作
- col = input[1]*2 + input[2];
- value = s0[row][col];
- output[1] = value % 2;
- output[0] = value / 2;
- row = input[4]*2 + input[7]; //S1的操作
- col = input[5]*2 + input[6];
- value = s1[row][col];
- output[3] = value % 2;
- output[2] = value / 2;
- }
- long GetFileLength(char filepath[]) //读取文件长度
- {
- FILE *pFile;
- long filelength= 0;
- pFile =fopen(filepath, "rb");
- if ( pFile== NULL)
- return 0;
- else
- {
- fseek(pFile, 0, SEEK_END);
- filelength = ftell(pFile);
- }
- return filelength;
- }
- void Binary(unsigned char plain,int buff[],int N)
- {// 完成把十进制转化为二进制,N为表示的长度,结果保存在buff中
- int i;
- for(i=0;i<N;i++)
- {
- buff[N-i-1]=plain%2;
- plain=plain/2;
- }
- }
- void Dei_Bin(unsigned char *ciphertext,int bin[])
- { // 完成把二进制转化为十进制,结果保存在ciphertext中
- *ciphertext=bin[0]*128+bin[1]*64+bin[2]*32+bin[3]*16+bin[4]*8+bin[5]*4+bin[6]*2+bin[7];
- }
- void Fk(int input[], int subkey[], int output[]) //Fk函数
- {
- int i;
- int right[4] = { 0 };
- int out[8] = { 0 };
- int out1[4] = { 0 };
- for(i=0;i<4;i++)
- {
- right[i]=input[i+4];
- output[i+4] = input[i+4];
- }
- Permutation(right,out,ep, 8); //EP扩展操作,结果存于out中
- XOR(out,subkey,8); //和Key进行异或操作,结果存于数组out
- SBox(out, out1); //Sbox操作,结果存于out1
- Permutation(out1,output,P4,4); //P4置换,结果存于数组output
- XOR(output, input, 4);
- }
- int Encryption(char inputfilePath[],char outputfilePath[], char key[], int mode ) //加解密函数
- {
- FILE *inputfile;
- FILE *outputfile;
- unsigned char plaint,cipher;
- int i;
- long filelength = 0;
- int inkey[16]={0};
- int output[8]={0};
- int K1[8]={0};
- int buff[8]={0};
- int l_ip[5]={0},r_ip[5]={0};
- int K2[8]={0};
- int out[8]={0};
- int ciphertext[8]={0};
- char deci_p=0;
- plaint = cipher = 0;
- inputfile = fopen(inputfilePath, "rb"); //读取文件
- if ( inputfile == NULL)
- {
- printf("The file is not exist");
- return 0;
- }
- outputfile=fopen(outputfilePath,"wb"); //读写文件
- if ( outputfile == NULL)
- {
- printf("The file is not exist");
- return 0;
- }
- Binary(key[0],&inkey[0],8);
- Binary(key[1],&inkey[8],8);
- Subkey(inkey, K1,1); //得到K1
- Subkey(inkey, K2,2); //得到K2
- filelength = GetFileLength(inputfilePath);
- for(i=0; i<filelength; i++)
- {
- fread(&plaint, sizeof(unsigned char),1, inputfile);
- Binary(plaint,buff,8); //把十进制转化为二进制,并将结果存于数组buff中
- Permutation(buff,out, ip, 8);
- if (mode == 0)
- Fk(out,K1,output);
- else
- Fk(out,K2,output);
- swap(output);
- if (mode == 0)
- Fk(output, K2,out);
- else
- Fk(output, K1,out);
- Permutation(out,ciphertext, ip_1, 8); //IP逆置换,存于数组ciphertext中
- Dei_Bin(&cipher,ciphertext); //将二进制转化为十进制
- fwrite(&cipher, sizeof(unsigned char),1, outputfile);
- }
- fclose(inputfile); //关闭文件
- fclose(outputfile);
- }
- int main(int argc,char *argv[])
- {
- if(argc!=6)
- {
- puts("参数是:s-des cipher -e/-d key inputfile outputfile");
- printf("Usage: %s SourceFile TargetFilenTry Againn",argv[0]);
- exit(0);
- }
- if ( strcmp(argv[2],"-e") ==0)
- Encryption(argv[4], argv[5], argv[3], 0);
- else
- Encryption(argv[4], argv[5], argv[3], 1);
- return 0;
- }