简体   繁体   English

具有密码安全性的PFX自签名x509证书

[英]PFX Self Signed x509 Certificate with password secure

After looking at how to generate self-signed digital signatures from Creating a self-signed certificate in C# , I can call CreateSelfSignCertificatePfx and get PXF data in a byte array back, which can then be used within an X509Certificate2 object to sign and verify. 在查看了如何通过在C#中创建自签名证书生成自签名数字签名之后,我可以调用CreateSelfSignCertificatePfx并获取字节数组中的PXF数据,然后可以在X509Certificate2对象中使用该数据进行签名和验证。 Example... 例...

byte[] pfx = Certificate.CreateSelfSignCertificatePfx("O=Company,CN=Firstname,SN=Lastname", DateTime.Now, DateTime.Now.AddYears(1), "password");

X509Certificate2 cert = new X509Certificate2(pfx, "password");
byte[] publicBytes = cert.RawData;

RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey;
byte[] signedData = rsa.SignData(new System.Text.UTF8Encoding().GetBytes("Test"), new SHA1CryptoServiceProvider());

RSACryptoServiceProvider rsa2 = (RSACryptoServiceProvider)new X509Certificate2(publicBytes).PublicKey.Key;

bool verified = rsa2.VerifyData(new System.Text.UTF8Encoding().GetBytes("Test"), new SHA1CryptoServiceProvider(), signedData);

This works. 这可行。 My concern though is the original bytes, byte[] pfx from above, need to be stored in a DB (to sign stuff). 不过我担心的是,原始字节byte [] pfx从上方需要存储在DB中(以进行签名)。 The question becomes, how secure are the bytes in this format? 问题变成了,这种格式的字节有多安全? I know you need the password to construct the new X509Certificate2 with a private key, but in a general sense, how secure are the bytes without the password? 我知道您需要密码来构造带有私钥的新X509Certificate2,但是从一般意义上讲,没有密码的字节有多安全? I have no problems encrypting these bytes as an added layer, but is that necessary? 我将这些字节作为附加层加密没有问题,但这是必要的吗?

According to X509Certificate2.X509Certificate2(Byte[], String) Constructor 根据X509Certificate2.X509Certificate2(Byte [],String)构造函数

Calling this constructor with the correct password decrypts the private key and saves it to a key container. 使用正确的密码调用此构造函数会解密私钥并将其保存到密钥容器中。

I just want to ensure the private key is safe without the password. 我只想确保没有密码的私钥是安全的。

In my eyes the question is not whether you should put the "bytes" in the database, but more, would you put the file with the private key in your file system. 在我看来,问题不在于是否应将“字节”放入数据库中,还可以将带有私钥的文件放入文件系统中。

In the way you're doing it, it's essentially the same thing. 就您做的方式而言,本质上是相同的。 You're just storing the bytes that make up the cert file. 您只是存储组成cert文件的字节。

I may be failing to understand the difference here, but they bytes and the file are essentially the same thing, the only difference being the fact that one has to gain access to the db to get them. 我可能无法理解这里的区别,但是字节和文件本质上是同一件事,唯一的区别是必须获得对db的访问权限才能获得它们。

The private keys in a PFX (PKCS#12) are stored encrypted, which is of course what the password is for. PFX(PKCS#12)中的私钥已加密存储,这当然是密码的用途。 Not all of a PFX is encrypted, the structural pieces stay plaintext to contain metadata about the contents (like what encryption algorithm was used). 并非所有PFX都经过加密,结构片段保持纯文本格式,以包含有关内容的元数据(例如使用哪种加密算法)。

Based on inspecting the file, as of Windows 7 the private keys are encrypted using 3-key (168-bit) 3DES. 基于检查文件,从Windows 7开始,私钥使用3键(168位)3DES加密。 The key is derived via a complex formula involving your password; 密钥是通过涉及密码的复杂公式得出的; there's nothing saved in the file which gives any indication as to what your password was, how long it was, et cetera. 文件中没有保存任何内容,它没有任何迹象表明您的密码是多少,密码有多久等等。

The password is usually proven correct by the addition of a MAC on the contents, which uses the same password for its key derivation function. 通常,通过在内容上添加MAC来证明该密码是正确的,该MAC将相同的密码用于其密钥派生功能。 In the possible case of the MAC password and the encryption password being different (which I've personally never seen) the password is verified by the structural information in the encrypted payload. 在MAC密码和加密密码可能不同的情况下(我个人从未见过),密码将通过加密有效负载中的结构信息进行验证。

DES' weakness mainly lay in the small keysize, it's easily brute forcable today. DES的弱点主要在于较小的密钥大小,今天很容易被强行强制使用。 A 3-key 3DES key has 112 more semantic bits than a (1)DES key, making it take 2^112 (~5 x 10^33) times longer to break. 3键3DES密钥比(1)DES密钥具有更多的语义位,使其破解花费2 ^ 112(〜5 x 10 ^ 33)倍的时间。

So, at the end of the day, the private key is cryptographically sound. 因此,归根结底,私钥是加密的。 But like anything with a password-based input, if you use a bad password that is easily guessed then it can be cracked by brute force. 但是,就像任何使用基于密码的输入一样,如果您使用容易猜到的错误密码,则可能会被蛮力破解。

Use a smartcard or token to store your private key. 使用智能卡或令牌存储您的私钥。

UPDATE: The Pvt key can be accessed by anyone who can access the machine. 更新:任何可以访问计算机的人都可以访问Pvt密钥。

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

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