2011-01-08 61 views
0

編集: 私はアルゴリズムをC++で実装しています。暗号化(C++)

#include<iostream> 
#include<math.h> 
#include<string> 

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(char m) 
    { 


     int p = 11, q = 3; 
     int e = 3; 
     int 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(m,e); 
     int ciphertext = g %n; 
     // 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 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(s); 
     } 
for (int i =0 ; i<l ; i++) 
    { 
    cout<<"the encryption of string"<<endl; 
     cout<<str[i]; 
    } 


return 0; 
} 

エラーメッセージ エラーC2296: '%':違法、左のオペランドが 'ダブル'

+2

'REncryptText'はダブルダブル、および出力を取るのはなぜ?文字列を取り込んで文字列を出力するべきではありませんか? –

+0

@matt ellen、私のfuctionはstringの最初の値をとり、一度に全体の代わりにそのcypherコードを変換します。私はそれぞれの値をforループを使って1つずつ渡したいだけです。すべてのことを最終的な1つのサイファーコードにすべて追加したいと考えています。 – Raja

+2

@Raja:なぜあなたの関数はdoubleを返してdoubleを返しますか?ダブルスは文字列や文字ではありません。 –

答えて

2

を入力している、これを試してみてください。

Simple 2 way encryption for C#

+0

私はそれのために建てられたfuctionで何もしたくない。私は自分の暗号化のテクニックを使用したい.plz私の質問に答えてください – Raja

+1

@ラジャ: "そのようにしないでください"時には質問に最適な答えです。 Ans plzは丁寧です。 –

+0

@henkおかしな言葉をおかけして申し訳ありません。 – Raja