c++ - 加密程序错误

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

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

最佳答案

  1. 你忘了#include <cstring>得到strlen声明
  2. 线路int ciphertext = g % n;REncryptText将无法工作,因为您不能对 double 进行整数模运算.您需要找到一种更好的方法来进行模幂运算;搜索网络或挑选一本好的算法教科书,这并不是一个看起来微不足道的问题。

关于c++ - 加密程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4634140/

相关文章:

c++ - 在以下情况下从套接字读取数据已损坏?

c++ - 取消引用无效指针时如何在 Visual C++ 2017 调试器中出错?

c++ - 传递给调整大小函数后使用 new 创建的动态数组出错

mysql - 通过使用公钥和受用户自己的密码保护的私钥配对来保护 Web 应用程序数据库中的 PII?

c++ - 转发声明 typedef 保护

c# - C++ 调用 Java 代码与 C# 代码的性能

c++ - typedef 单例作为成员变量

algorithm - 易受预成像攻击的简单有缺陷的哈希算法示例

c# 加密,得到 .der 和 .pem 文件作为输入

c++ - 未定义对 `inflate' 的引用