私はRSAアルゴリズムをテストしていますが、間違った秘密鍵(Dパラメーター)で復号化したときに何が起こったのかをテストしようとしています.
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;
}
}
}
}