2012-12-10 68 views
0

我測試RSA algorthm,只是爲了嘗試使用錯誤的私鑰(D參數)解密時測試了什麼發生。.net RSA - 更改私鑰

我使用RSACryptoServiceProvider默認構造函數(沒有參數)。我加密一個字節數組,然後更改私鑰。爲此,我輸出到RSAParameters對象修改D參數,然後再次輸入。然後我解密信息,結果是原始數據!

所以應該有一些我在這個工程中缺少的東西。這是代碼。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Security.Cryptography; 
using Apoyo; 

namespace PruebaRSA 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Ayuda ayuda = new Ayuda(); 
      byte[] datosOriginales = new byte[10]; 
      byte[] datosCifrados; 
      byte[] datosDescifrados; 

      CrearArrayDatos(datosOriginales); 

      RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider(); 
      datosCifrados = rsaCSP.Encrypt(datosOriginales, false); 



      //-------------------------------------------------------------- 
      //Decrypt with the original Private Key 

      datosDescifrados = rsaCSP.Decrypt(datosCifrados, false); 

      Console.WriteLine("Texto Cifrado:"); 
      ayuda.WriteHex(datosCifrados, datosCifrados.Length); 
      Console.WriteLine("Texto Descifrado:"); 
      ayuda.WriteHex(datosDescifrados, datosDescifrados.Length); 

      //Change the Private Key 
      RSAParameters rsaParameters = rsaCSP.ExportParameters(true); 
      byte[] newD = new byte[rsaParameters.D.Length]; 
      CrearArrayDatos(newD); 
      rsaParameters.D = newD; 
      rsaCSP.ImportParameters(rsaParameters); 

      //Decrypt with the new Private Key 
      datosDescifrados = rsaCSP.Decrypt(datosCifrados, false); 
      Console.WriteLine("Texto Descifrado:"); 
      ayuda.WriteHex(datosDescifrados, datosDescifrados.Length); 

      rsaParameters = rsaCSP.ExportParameters(true); 
      Console.WriteLine("Clave privada utilizada: "); 
      ayuda.WriteHex(rsaParameters.D, rsaParameters.D.Length); 


      //____________________________________________ 

      Console.Write("Presionar Tecla"); 
      Console.Read(); 

     } 

     private static void CrearArrayDatos(byte[] datos) 
     { 
      for (byte i = 0; i < datos.Length; i++) 
      { 
       datos[i] = i; 
      } 
     } 
    } 
} 

回答

2

RSAParameters包含額外的參數,可以用來加速RSA解密使用中國餘數定理。解密這種方式並不需要D.它只需要Dp和Dq。所以如果你改變這兩個參數中的一個,那麼我預計解密將失敗。

當然,爲了更好的安全性,如果.net也會提供一致性檢查,這樣就可以檢測到具有不一致參數的私鑰。 (不知道這樣的一致性檢查沒有執行,或者我無法找到它)。

+0

好電話傑克,這可能是! –