RSA密钥值和公用/专用密钥中的模数 [英] RSA Key Values and Modulus in Public/Private Keys

查看:405
本文介绍了RSA密钥值和公用/专用密钥中的模数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用PKI来保护电子邮件,文件等的应用程序. 使用System.Cryptography命名空间,我正在使用RSACryptoServiceProvider生成新的密钥对.

方法是:

public static void GenerateKeys(int keySize, out string publicKey, out string privateKey)
{
    using (var provider = new RSACryptoServiceProvider(keySize))
    {
        publicKey = provider.ToXmlString(false);
        privateKey = provider.ToXmlString(true);
    }
}

keySize =2048.

这将导致生成一个公钥,例如(为了简洁起见,已使用"----- START/END PUBLIC KEY BLOCK -----"包装器对其进行了修剪/填充.

-----START PUBLIC KEY BLOCK-----
<RSAKeyValue><Modulus>xs1GwyPre7/knVd3CAO1pyk++yp/qmBz2TekgrehYT
WU7hs8bUCeVQrL2OB+jm/AgjdPMohWHD/tLcJy35aZgVfPI3Oa3gmXxdoLZrfNRb
nrCm3Xr1MR7wnhMyBt5XXyU/FiF46g5qJ2DUIUg7teoKDNUSAN81JTIoH0KC+rZB
oO3tu9PR7H75K5G2eT6oUWkWKcZZU/4WNCDasNtizTe41Jy99BjrChww5r2ctqG8
LvIv7UeeFaK1vhxGKaNH/7JvKJI9LbewWNtmb/nRzQg9xK3e0OhblbW+o6zg5pTw
+n37fS7pkXK7lbRfUfaQmhoGy6ox4UWGmOgm8yPu8S4Q==</Modulus><Exponen
t>AQAB</Exponent></RSAKeyValue>
-----END PUBLIC KEY BLOCK-----`

当我查看基于PGP的公共(或私有)密钥时,密钥内部没有<RSAKeyValue><Modulus><Exponent>值.

我做错什么了吗?我错过了什么吗?如果我分发此密钥,这是一个安全问题吗?

对我来说,加密是一个新的令人兴奋的领域,因此,我非常感谢这里的任何指导.我担心自己搞砸了-加密密钥工作并用私钥工作解密-我只是想知道PGP/GPG密钥的外观差异如何,以及我该怎么做才能纠正? >

提前谢谢!

解决方案

因为您使用了

分发公钥时,通常是通过颁发签名的X509证书来完成的,该证书包含公钥,将该密钥链接到实体的标识信息以及来自可信机构的签名.

如果您以XML String格式给出公钥,则接收方必须使用FromXmlString()方法来使用它.除非您亲自将公用密钥提供给公用密钥(或使用上面的证书方法),否则接收者也无法知道真正发送公用密钥的是您.

I am writing an application that used PKI to secure email, files etc. Using the System.Cryptography namespace, I am generating a new key pair using RSACryptoServiceProvider.

The method is:

public static void GenerateKeys(int keySize, out string publicKey, out string privateKey)
{
    using (var provider = new RSACryptoServiceProvider(keySize))
    {
        publicKey = provider.ToXmlString(false);
        privateKey = provider.ToXmlString(true);
    }
}

keySize = 2048.

This results in a public key like (this has been trimmed/padded with "-----START/END PUBLIC KEY BLOCK-----" wrappers for neatness.

-----START PUBLIC KEY BLOCK-----
<RSAKeyValue><Modulus>xs1GwyPre7/knVd3CAO1pyk++yp/qmBz2TekgrehYT
WU7hs8bUCeVQrL2OB+jm/AgjdPMohWHD/tLcJy35aZgVfPI3Oa3gmXxdoLZrfNRb
nrCm3Xr1MR7wnhMyBt5XXyU/FiF46g5qJ2DUIUg7teoKDNUSAN81JTIoH0KC+rZB
oO3tu9PR7H75K5G2eT6oUWkWKcZZU/4WNCDasNtizTe41Jy99BjrChww5r2ctqG8
LvIv7UeeFaK1vhxGKaNH/7JvKJI9LbewWNtmb/nRzQg9xK3e0OhblbW+o6zg5pTw
+n37fS7pkXK7lbRfUfaQmhoGy6ox4UWGmOgm8yPu8S4Q==</Modulus><Exponen
t>AQAB</Exponent></RSAKeyValue>
-----END PUBLIC KEY BLOCK-----`

When I look at PGP based public (or private) keys, there is no <RSAKeyValue>, <Modulus> or <Exponent> values inside the key.

Am I doing something wrong? Have I missed something? If I distribute this key, is this a security issue?

Crypto is a new and exciting field to me so I would REALLY appreciate any guidance here. I'm concerned I've screwed up - encrypting to the key works and decrypting the with the private key works - I was only wondering how PGP/GPG keys differ in appearance so much and what I need to do to correct this?

Thank you in advance!

解决方案

The RSAKeyValue, Modulus, and Exponent tags are in there because you used the method ToXmlString().

An RSA public key is made up of the modulus and the public exponent. There is no security issue with distributing these 2 items. HOWEVER, you do NOT want to distribute any of the other items in the Private Key. The private key has these fields:

<RSAKeyValue>
   <Modulus>…</Modulus>
   <Exponent>…</Exponent>
   <P>…</P>
   <Q>…</Q>
   <DP>…</DP>
   <DQ>…</DQ>
   <InverseQ>…</InverseQ>
   <D>…</D>
</RSAKeyValue>

Do not distribute anything other than the Modulus and Public Exponent, which are found in both the Private and Public key.

When public keys are distributed, they are usually done by giving out a signed X509 certificate, which contains the public key, identification information linking that key to an entity, and a signature from a trusted authority.

If you give out the public key in the XML String format, the receiver must then use the FromXmlString() method to use it. The receiver also has no way to know if it is you who really sent the public key unless you give it to them in person (or use the certificate method above).

这篇关于RSA密钥值和公用/专用密钥中的模数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆