【问题标题】:I am working with the source code of RSA Algorithm.It compiles correctly with no errors but the output is not correct [closed]我正在使用 RSA 算法的源代码。它可以正确编译,没有错误,但输出不正确 [关闭]
【发布时间】:2015-12-30 11:59:35
【问题描述】:

我正在使用 RSA 算法的源代码,并且在以下代码编译中没有错误,但是关于不正确的标志的警告很少。但是输出不是应该的。 源代码:

#include<stdio.h>
int phi,M,n,e,d,C,FLAG;
int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
 FLAG = 1;
 return 0;
}
FLAG = 0;
return 0;
}

void encrypt()
{
int i;    
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}
void decrypt()
{
int i;
 M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}

int main()
{    system("clear");
 int p,q,s;
 printf("Enter Two Relatively Prime Numbers\t: ");
 scanf("%d%d",&p,&q);
 n = p*q;
 phi=(p-1)*(q-1);
 printf("\n\tF(n)\t= %d",phi);
 do
 {
  printf("\n\nEnter e(less than F(n)\t: ");
  scanf("%d",&e);
  check();
 }while(FLAG==1);
 d = 1;
 do
  {
     s = (d*e)%phi;
     d++;
  }while(s!=1);     
  d = d-1;
   printf("\n\tPublic Key\t: {%d,%d}",e,n);
   printf("\n\tPrivate Key\t: {%d,%d}",d,n);
   printf("\n\nEnter The Plain Text\t: ");
   scanf("%d",&M);
   printf("\n \n");
   printf("\n\nEnter the Cipher text\t: ");
   scanf("%d",&C);
   encrypt();
   decrypt();
   getchar();
   }

上面代码的输出 sn -p -

有解决错误输出响应的建议吗?

【问题讨论】:

  • 请将会话记录粘贴到演讲中,而不是链接到图像。
  • 代码可以使用一点连贯的缩进。

标签: c linux gcc cryptography rsa


【解决方案1】:

您的程序需要一个数字作为纯文本。你传递字符串"hi"。你预计会发生什么?

下次考虑检查scanf的返回值。它指示有多少项目已成功扫描。使用它来确定用户是否真的输入了一个数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2016-03-03
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多