簡體   English   中英

.net RSA - 更改私鑰

[英].net RSA - Changing private key

我正在測試RSA algorthm,只是為了嘗試測試用錯誤的私鑰(D param)解密時發生的事情。

我正在使用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;
            }
        }
    }
}

RSAParameters包含可用於使用中國余數定理加速RSA解密的附加參數。 解密這種方式不需要D.它只需要Dp和Dq。 因此,如果您更改這兩個參數中的一個,那么我預計解密會失敗。

當然,為了良好的安全性,如果.net還提供一致性檢查將是很好的,因此可以檢測到具有不一致參數的這種私鑰。 (不確定是否未執行此類一致性檢查,或者我是否找不到它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM