2011-01-08 2 views
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 오는 : '%': '펑': 불법, 오른쪽 피연산자는 입력 '더블' 오류 C2668이 오버로드 된 함수에 대한 모호한 전화를오류가

+0

무엇이 질문입니까? 여기에 몇 가지 소스 코드를 복사하는 것보다 더 노력해야한다. 예를 들어 무엇을하려고하는지, 예상되는 결과는 무엇인지, 실제 결과는 무엇인지, 어떻게 다른지, 등등 ... –

+0

컴파일 중 erreor가 올 것이다. 마지막으로 – Raja

+0

을 참조하십시오. 이것은 최고의 질문은 아니지만 컴파일러 오류에 대한 OP의 의견은 충분히 명확하게 설명합니다. –

답변

2
  1. 당신은 #include <cstring>을 잊어 버렸습니다. strlen을 선언했습니다.
  2. double에 모듈로를 입력 할 수 없기 때문에 int ciphertext = g % n;REncryptText에 있습니다. 모듈화 된 지수 연산을 수행하는 더 좋은 방법을 찾아야합니다. 웹을 검색하거나 좋은 알고리즘 교과서를 선택하십시오. 이것은 사소한 문제가 아닌 것처럼 보입니다.
+0

참이지만 * 매우 * 작은 피연산자의 경우'pow'를 사용하여 빠져 나가고 결과를 int로 캐스트 할 수 있습니다. –

+0

부분을 편집했습니다 (int i = 0; i (s)); \t \t} 이제 문제는 pow 함수에 있습니다. 암호 해독을 두 번 변경하면 problm이 올 것입니다. 어떻게 해결할 수 있습니까? – Raja

+1

주의! 'pow'의 결과를 int로 캐스트해야합니다. 그리고'pow '를 사용하는 것은 작은 피연산자에 대해서만 사용할 수 있으므로 실수입니다. [this] (https://secure.wikimedia.org/wikipedia/en/wiki/Modular_exponentiation)와 같은 alorithm을 사용해야합니다. –