加密程序错误

发布于 2024-10-11 04:14:04 字数 1814 浏览 1 评论 0原文

#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”:对重载函数的不明确调用

#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;
    }

now the error coming
error C2297: '%' : illegal, right operand has type 'double'
error C2668: 'pow' : ambiguous call to overloaded function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2024-10-18 04:14:04
  1. 您忘记了 #include 来声明 strlen
  2. int ciphertext = g % n; in REncryptText 不起作用,因为您无法对 double 进行整数模运算。你需要找到一种更好的方法来进行模幂运算;搜索网络或拿起一本好的算法教科书,这并不是一个看起来那么微不足道的问题。
  1. You forgot to #include <cstring> to get strlen declared
  2. The line int ciphertext = g % n; in REncryptText won't work since you can't do integer modulo on a double. You need to find a better way to do modular exponentiation; search the web or pick up a good algorithms textbook, this isn't as trivial a problem as it may seem.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文