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;
}
}
}
}
好電話傑克,這可能是! –