?? 單表代換密碼(初具形式).cpp
字號:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char str[256],z[26],code[256],plaintext[256],ciphertext[256];
char *p=str; char *q=str,*r=z,*s=plaintext,*t=ciphertext;
char c,d,e,f;
int l,i;
for( i=0;i<26;i++)
z[i]=i+97;
printf("Array z is:\n");
for(i=0;i<26;i++)
printf("%c",z[i]);
loop2:printf("\nPlease input the key:\n");
gets(str);
l=strlen(str);
printf("the length of str(key) is:%d\n",l);
loop:for(i=0;i<l;i++)
{
int j=i+1;
for(;j<l;j++)
{
if(i!=j&&p[i]==q[j])
{
for(;j<l;j++)
q[j]=q[j+1];//寫成q[j+1]=q[j]; 是錯誤的!!!
l--;
goto loop;
}
else ;
}
}
printf("here the key is changed to:\n");
for(i=0;i<l;i++)
printf("%c",p[i]);
l=strlen(str);
printf(" now the length of Arrazy str(key) is changed to: %d\n",strlen(str));
//************
int n=0,k=l;
loop1:for(i=n;i<26;i++)
{
for(int j=0;j<k;j++)
{
if(p[j]==r[i])
{
n++;goto loop1;
}
}
str[k++]=z[i];
}
printf("\ntable done!\n");
printf("\n***************************\n");
for(i=0;i<26;i++)
printf("%c",r[i]); printf("\n");
for(i=0;i<26;i++)
printf("%c",p[i]); printf("\n");
printf("***************************\n");
printf("please input plaintext: ");
gets(plaintext);printf("\n");
puts(plaintext);
printf("You hava just input the plaintext,are you sure to do encryption?(y/n)");
c=getchar();
if(c=='n') exit(0);
else if(c!='y')
{
printf("input your choice again!");
c=getchar();
}
for(i=0;i<(int)strlen(plaintext);i++)
for(int j=0;j<26;j++)
if(s[i]==r[j])
code[i]=p[j];
for(i=0;i<(int)strlen(plaintext);i++)
printf("%c",code[i]);
printf("\ncontinue?(y/n)");
d=getchar();
if(d=='n') exit(0);
else if(c!='y')
{
printf("input your choice again!");
d=getchar();
}
printf("please input ciphertext");
gets(ciphertext);printf("\n");
puts(ciphertext);
printf("You hava just input the ciphertext,are you sure to do decryption?(y/n)");
e=getchar();
if(e=='n') exit(0);
else if(c!='y')
{
printf("input your choice again!");
e=getchar();
}
//********************decryption
for(i=0;i<(int)strlen(ciphertext);i++)
for(int j=0;j<26;j++)
if(t[i]==s[j])
code[i]=z[j];
for(i=0;i<(int)strlen(ciphertext);i++)
printf("%c",code[i]);
printf("continue?(y/n)");
f=getchar();
if(f=='n') exit(0);
else if(c!='y')
{
printf("input your choice again!");
f=getchar();
}
else goto loop2;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -