📄 substitution.c
字号:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
//输入文为hello world
#define MAXROW 50
#define MAXCOL 5
#define MAXSIZE (MAXROW-1)*MAXCOL
#define SUCCESS 1
#define FILLCHAR '#'
//#define NULL 0
void randMatrix(int matrix[]);
void searchEndPosition(int *row,int *column,char array[][MAXCOL]);
int setString(const char str[],char buff[][MAXCOL]);
int getString(char array[][MAXCOL],char str[]);
int Decryption(const int matrix[],const char encryptStr[][MAXCOL],char decryptStr[][MAXCOL]);
int Encryption(const int matrix[],const char buff[][MAXCOL],char encryptStr[][MAXCOL]);
void main()
{
int i=0;
int matrix[MAXCOL];//={1,3,2,4,0};
//char *str="hello world";
char buff[MAXROW][MAXCOL]={0};
char encryptStr[MAXROW][MAXCOL]={0};
char decryptStr[MAXROW][MAXCOL]={0};
char *str=malloc(MAXSIZE);
randMatrix( matrix );
printf("The substitution matrix is:");
for(i=0;i<MAXCOL;i++)
printf("%d ",matrix[i]);
printf("\nInput source string :");
//here the function "gets()" should be used but "scanf()",
// expecting the input character is '\n'
gets(str);
if(setString(str,buff))
printf("The source string is:%s\n",buff);
if(Encryption(matrix,buff,encryptStr))
printf("The encryption is:%s\n",encryptStr);
if(Decryption(matrix,encryptStr,decryptStr))
printf("The decryption is:%s\n",decryptStr);
getString(decryptStr,str);
printf("The destination string is:%s\n",str);
free(str);
}
///////////////////////////////////////////////////////
//setString is uesed for deviding string to several arrays
int setString(const char str[],char buff[][MAXCOL]){
int i=0,j=0,k=0;
int m,n;
while(str[k]!='\0'){
buff[i][j++]=str[k++];
if(j==MAXCOL){
j=j % MAXCOL;
i++;
}
}
/*for(m=0;m<=i;m++)
for(n=0 ;n<MAXCOL;n++){
if(buff[m][n]=='\0')
break;
printf("%c",buff[m][n]);
}
printf("\n");*/
//fill in the gap!
while(j!=MAXCOL){
buff[i][j++]=FILLCHAR;
}
buff[i][j]='\0';
return SUCCESS;
}
//////////////////////////////////////////////
int Encryption(const int matrix[],const char buff[][MAXCOL],char encryptStr[][MAXCOL]){
int i,j,m,n;
/*while(buff[i][j]!='\0'){
j++;
if(j==5){
j=1;
i++;
}
}*/
//printf("%d%d\n",i,j);
searchEndPosition(&i,&j,buff);
for(m=0;m<i;m++)
for(n=0;n<MAXCOL;n++){
encryptStr[m][n]=buff[m][matrix[n]];
}
return SUCCESS;
}
/////////////////////////////////////////////
int Decryption(const int matrix[],const char encryptStr[][MAXCOL],char decryptStr[][MAXCOL]){
int i,j,m,n;
/*while(encryptStr[i][j]!='\0'){
j++;
if(j==5){
j=1;
i++;
}
}*/
searchEndPosition(&i,&j,encryptStr);
for(m=0;m<i;m++)
for(n=0;n<MAXCOL;n++){
decryptStr[m][matrix[n]]=encryptStr[m][n];
}
return SUCCESS;
}
//////////////////////////////////////////////
//find out the end postion of a string
void searchEndPosition(int *row,int *column,char array[][MAXCOL]){
*row=0;
*column=0;
while(array[*row][*column]!='\0'){
++*column;
if(*column==MAXCOL){
*column=1;
++*row;
}
}
}
/////////////////////////////////////////////
//resume the sourc string
int getString(char array[][MAXCOL],char str[]){
int i=0,j=0,k=0;
while(array[i][j]!=FILLCHAR){
str[k++]=array[i][j++];
if(j==MAXCOL){
j=j % MAXCOL;
i++;
}
}
str[k]='\0';
return SUCCESS;
}
/////////////////////////////////////////////
//produce a substitution matrix at random
void randMatrix(int matrix[]){
int i,j,k,flag;
srand((unsigned)time(NULL));
for(j=0;j<MAXCOL;j++){
while(1){
i = rand() % MAXCOL;
flag=0;
for(k=0;k<j;k++)
if(i==matrix[k]){
flag=1;
break;
}
if(flag==0){
matrix[j]=i;
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -