2012-01-15 10 views
1

RSA解読の問題RSA解読の問題C#

C#RSAプログラムに問題があります。正しく解読されていません。私がd =(e^-1)%phiNを割り当てて、私の暗号文にdを適用すると、ばかばかしい10進数の答えが出ます。それは整数で始まるべきです。私はそれが私の数学の問題だと思う。アドバイスはありますか? 詳細やコードの残りが必要な場合は、お尋ねください。 また、このコードを改善するために使用できるパディングスキームはありますか?現在、このコードは周波数解析の脆弱性があります。

protected void decryptRSA(object sender, EventArgs ev) 

{ 
     double p = (double)Convert.ToInt64(P.Text);//I use 123 for testing 
     double q = (double)Convert.ToInt64(Q.Text);//127 
     double e = (double)Convert.ToInt64(E.Text);//133 
     double phiN = (p-1)*(q-1); 
     double n = p*q; 
     double d = Math.Pow(e, -1D); 
     d = d%phiN; 

     string cipherStr = outputBuffer.Text; 
     double[] cipherTextDouble = new double[100]; 
     string[]plainText = new string[cipherTextDouble.Length]; 

     cipherTextDouble = parser(cipherStr, 'D'); 
    for(int slot = 0; slot<cipherTextDouble.Length; slot++) 
     { 
    cipherTextDouble[slot] = (double)(Math.Pow((double)cipherTextDouble[slot],(double)d)%n); 
     } 
     for(int slot = 0; slot<cipherTextDouble.Length; slot++) 
     { 
      inputBuffer.Text += Convert.ToChar(cipherTextDouble[slot]) + ' ';//the spot were it dies 
//it doesn't like to convert from a decimal like 1.75 to a char. Of course I should never get a decimal like 1.75, which is the problem 
     } 
    } 
+1

ダブルを使用しないでください。 –

答えて

2

指数を正しく計算していません。数字dは、ed = 1 (mod phi)、つまり逆数がe (mod phi)となるようにする必要があります。これは、double d = Math.Pow(e, -1D);が計算した実数のeの逆数を計算してから、mod操作を実行するのと同じではありません。これは10進数で終わる理由です(この場合、%は実際にはC#では '剰余'演算子であり、そうでない場合は整数ではないため、1/133〜0.007と1/133%15372のままです)とにかく二重に働かないでください))。

逆モデφを計算するには、Euclidean Algorithmを使用する必要があります。

編集:GregSは、コンピュータの実装では、代わりにExtended Euclidean Algorithmを使用して、1回のパスでモジュラ逆行列を見つけることを正しく指摘しています。これは通常計算によって行われます。あなたはユークリッドアルゴリズム(通常は手で)でそれを行うことができますが、それは時間の無駄です。

+1

*拡張*ユークリッドアルゴリズム。 –