【问题标题】:error in encryption program加密程序错误
【发布时间】:2011-01-08 13:42:52
【问题描述】:
#include<iostream>
#include<math.h>
#include<string>
#include<cstring>

using namespace std;

 int gcd(int n,int m)
   {
   if(m<=n && n%m ==0)
   return m;
   if(n<m)
   return gcd(m,n);
   else
   return gcd(m,n%m);
  }

 int REncryptText(int m)
  {int b = double(m);
      int p = 11, q = 3;
      int e = 3;
      double n = p * q;
      int phi = (p - 1) * (q - 1);

      int check1 = gcd(e, p - 1);
      int check2 = gcd(e, q - 1);

      int check3 = gcd(e, phi);

            //     // Compute d such that ed ≡ 1 (mod phi)
            //i.e. compute d = e-1 mod phi = 3-1 mod 20
            //i.e. find a value for d such that phi divides (ed-1)
            //i.e. find d such that 20 divides 3d-1.
            //Simple testing (d = 1, 2, ...) gives d = 7

   //   double d = Math.Pow(e, -1) % phi;

      int d = 7;

      // public key = (n,e) // (33,3)
      //private key = (n,d) //(33 ,7)

      double g = pow(b,e);

      double ciphertext = g % n;  // here error
      // Now say we want to encrypt the message m = 7, c = me mod n = 73 mod 33 = 343 mod 33 = 13. Hence the ciphertext c = 13. 

      //double decrypt = Math.Pow(ciphertext, d) % n;
      return int(ciphertext);
  }

int main()
    {
    char plaintext[80],str[80];

    cout<<" enter the text you want to encrpt";
    cin.get(plaintext,79);

    int l =strlen(plaintext);
    for ( int i =0 ; i<l ; i++)
        { 
        char s = plaintext[i];
        str[i]=REncryptText(static_cast<char>(s));
        }

    for ( int i =0 ; i<l ; i++)
        { 
        cout<<"the encryption of string"<<endl;
            cout<<str[i];
        }


    return 0;
    }

现在错误来了 错误 C2297:“%”:非法,右操作数的类型为“double” 错误 C2668:“pow”:对重载函数的模糊调用

【问题讨论】:

  • 您的问题是什么?您应该比在此处复制粘贴一些源代码付出更多的努力,例如解释您要做什么,预期结果是什么,实际结果是什么,它们有何不同,...
  • 编译错误即将到来。查看最后一个
  • 反对关闭。这不是有史以来最好的问题,但 OP 关于编译器错误的评论足以说明这一点。

标签: c++ visual-c++ encryption rsa


【解决方案1】:
  1. 你忘了#include &lt;cstring&gt; 来声明strlen
  2. REncryptText 中的行 int ciphertext = g % n; 将不起作用,因为您不能对 double 进行整数模运算。您需要找到一种更好的方法来进行模幂运算;搜索网络或挑选一本好的算法教科书,这并不像看起来那么简单。

【讨论】:

  • 是的,但对于 非常 个小操作数,您可以使用 pow 并将结果转换为 int。
  • 我已经编辑了部分 for ( int i =0 ; i(s));现在问题出在 pow 函数中,如果我将其更改为两倍,我的解密问题就会出现。那么我该如何解决这个问题?
  • 注意!您必须将 pow 的结果转换为 int。并且使用pow 完全是一个错误,因为您只能将它用于微小的操作数。你应该使用像this这样的算法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 2019-03-29
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
相关资源
最近更新 更多