修改下面代码解密不正确问题:#include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<time.h> char s[100],*c; int n,e,d,i,C,j,k=0,len; int str[100],b[30],ming[100]; unsigned ged(unsigned a,unsigned b) { if(a%b==0) return b; else return ged(b,a%b); } void Egcd(int a,int b,int&x,int &y) { if(b==0||a==0) { x=1; y=0; return; } if(a<b) { Egcd(a,b%a,x,y); x=(int)(b*y+1)/a; } else { Egcd(a%b,b,x,y); y=(int)(a*x-1)/b; } } void RSA() { int p,q,N,Y; printf("请输入素数p和q:"); scanf("%d%d",&p,&q); n=p*q; N=(p-1)*(q-1); srand((unsigned)time(NULL)); while(1) { e=rand()%N; if(e==0) continue; if(ged(N,e)==1) { break; } } Egcd(e,N,d,Y); } void encrypt() { len=strlen(s); for(i=0;i<len;i++) { ming[i]=s[i]; } printf("\n"); printf("加密开始………………………………\n"); for(i=0;i<len;i++) { C=1; for(int j=0;j<e;j++) { C=(C*(s[i]-97))%n; } str[i]=C; } printf("加密结果为:\n"); for(int i=0;i<len;i++) printf("%d",s[i]); printf("\n加密结束………………………………\n"); } void decrypt() { c=(char*)malloc(len*sizeof(int)); for(i=0;i<len;i++) { C=1; for(int j=0;j<d;j++) { C=(C*(str[i]))%n; } c[i]=C+97; } c[i]='\0'; for(int z=0;z<k;z++) { for(i=0;i<len;i++) { if(i==b[z]) { for(j=len;j>i;j--) { c[j]=c[j-1]; } c[i]=' '; len++; b[z+1]=b[z+1]+(z+1); break; } } } c[len]='\0'; printf("\n解密开始……………………………\n"); printf("解密结果为:\n"); puts(c); printf("解密结束……………………………\n"); } int function() { int choice; printf("====================================\n"); printf(" 1--加密 \n"); printf(" 2--解密 \n"); printf(" 3--退出 \n"); printf("====================================\n"); printf("请输入要选择的功能:"); scanf("%d",&choice); return choice; } int main() { int function(); int fc; printf("请输入初始明文:"); gets(s); RSA(); while(1) { fc=function(); if(fc==1) encrypt(); else if(fc==2) decrypt(); else if(fc==3) break; else printf("输入错误,请重新输入!/n"); } return 0; }
时间: 2023-12-02 16:03:08 浏览: 28
1. 将 `gets(s);` 修改为 `fgets(s, 100, stdin);`,避免输入字符串超出数组范围的问题。
2. 在加密结果输出时,应该输出 str[i] 而不是 s[i],即将 `printf("%d",s[i]);` 修改为 `printf("%d",str[i]);`。
3. 在解密时,应该先将字符串长度 len 赋值给变量 k,即将 `int k=0,len;` 修改为 `int len=k=strlen(s);`。
4. 在解密时,应该先将字符串转换为数字,即将 `ming[i]=s[i];` 修改为 `ming[i]=s[i]-97;`。
5. 在解密时,应该将解密结果存储在 c 数组中,即将 `char s[100],*c;` 修改为 `char s[100],c[100];`,并在解密函数中将 `c=(char*)malloc(len*sizeof(int));` 删除。
6. 在解密时,应该在输出解密结果前将末尾的空格去掉,即将 `c[len]='\0';` 修改为 `c[len-k]='\0';`。
7. 在解密时,应该在输出解密结果前先进行空格还原,即将解密函数中的 for 循环和 if 判断改为以下代码:
```
for (int z = 0; z < k; z++) {
for (i = 0; i < len; i++) {
if (i == b[z]) {
for (j = len; j > i; j--) {
c[j] = c[j - 1];
}
c[i] = ' ';
len++;
for (int p = z + 1; p < k; p++) {
b[p]++;
}
break;
}
}
}
```
完整代码如下:
相关推荐
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)