繁体   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