简体   繁体   English

.net RSA - 更改私钥

[英].net RSA - Changing private key

I'm testing RSA algorthm and just for trying tested what happend when decrypting with the wrong private key (D param). 我正在测试RSA algorthm,只是为了尝试测试用错误的私钥(D param)解密时发生的事情。

I'm using RSACryptoServiceProvider with default constructor (no params). 我正在使用RSACryptoServiceProvider和默认构造函数(没有参数)。 I encrypt an array of bytes and then change the private key. 我加密一个字节数组,然后更改私钥。 For this I export to a RSAParameters object modify the D param and then import again. 为此,我导出到RSAParameters对象修改D参数然后再次导入。 Then I decrypt the info and the result is the original data!! 然后我解密信息,结果是原始数据!!

So there should be something I'm missing in how this works. 所以应该有一些我不知道它是如何工作的。 Here is the code. 这是代码。

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 contains additional parameters that can be used to speed up RSA decryption using the Chinese remainder theorem. RSAParameters包含可用于使用中国余数定理加速RSA解密的附加参数。 Decrypting this way does not need D. It just needs Dp and Dq. 解密这种方式不需要D.它只需要Dp和Dq。 So if you change one of these two parameters then I'd expect that decryption fails. 因此,如果您更改这两个参数中的一个,那么我预计解密会失败。

Of course, for good security it would be nice if .net would also provide a consistency check, so that such private keys with inconsistent parameters can be detected. 当然,为了良好的安全性,如果.net还提供一致性检查将是很好的,因此可以检测到具有不一致参数的这种私钥。 (Not sure if such a consistency check is not implemented or if I just can't find it). (不确定是否未执行此类一致性检查,或者我是否找不到它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM