谁能给我发个置换密码的加密解密源代码 还有关于置换密码实验的...5
发布网友
发布时间:2024-03-02 23:05
我来回答
共1个回答
热心网友
时间:2024-03-30 13:41
#include<stdio.h>
#define N 3
main()
{char choose;
char p[N+1];
char c[N+1];
int key[2][N];
int dekey[2][N];
int i,j;
int k;
printf("encryption press E or e\ndecyption press D or d\n");
choose=getchar();
getchar();
if(choose=='E'||choose=='e')
{printf("输入密钥置换阵:");
for(i=0;i<2;i++)
for(j=0;j<N;j++)
scanf("%d",&key[i][j]);
getchar(); /*接收回车符*/
printf("加密密钥置换阵为:\n");
printf("-------------------------------------------------------------\n");
for(i=0;i<N;i++)
printf("%5d",key[0][i]);
printf("\n-------------------------------------------------------------\n");
for(i=0;i<N;i++)
printf("%5d",key[1][i]);
printf("\n-------------------------------------------------------------\n");
printf("明文:");
gets(p);
printf("明文:%s\n",p);
for(i=0;i<N;i++)
{k=key[1][i];
c[i]=p[k];
}
c[N]='\0';
printf("密文:%s\n",c);
}
if(choose=='D'||choose=='d')
{
printf("输入密钥置换阵:");
for(i=0;i<2;i++)
for(j=0;j<N;j++)
scanf("%d",&key[i][j]);
getchar();/*接收回车符*/
printf("加密密钥置换阵为:\n");
printf("-------------------------------------------------------------\n");
for(i=0;i<N;i++)
printf("%5d",key[0][i]);
printf("\n-------------------------------------------------------------\n");
for(i=0;i<N;i++)
printf("%5d",key[1][i]);
printf("\n-------------------------------------------------------------\n");
for(i=0;i<N;i++)
dekey[0][i]=i;
for(j=0;j<N;j++)
for(k=0;k<N;k++)
if(key[1][k]==j)
dekey[1][j]=k;
printf("解密密钥置换阵为:\n");
printf("-------------------------------------------------------------\n");
for(i=0;i<N;i++)
printf("%5d",dekey[0][i]);
printf("\n-------------------------------------------------------------\n");
for(i=0;i<N;i++)
printf("%5d",dekey[1][i]);
printf("\n-------------------------------------------------------------\n");
printf("密文:");
gets(c);
printf("密文:%s\n",c);
for(i=0;i<N;i++)
{k=dekey[1][i];
p[i]=c[k];
}
p[N]='\0';
printf("明文:%s\n",p);
}
}