我正在使用带有以下参数的makecert创建x509证书:
makecert -r -pe -n"CN = Client"-ss MyApp
我想使用此证书使用RSA算法加密和解密数据.我期待在Windows证书库中生成证书,一切似乎都好(它有一个私钥,公钥是一个1024位的RSA密钥等等)
现在我使用这个C#代码来加密数据:
X509Store store = new X509Store("MyApp", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "Client", false);
X509Certificate2 _x509 = certs[0];
using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)_x509.PublicKey.Key)
{
byte[] dataToEncrypt = Encoding.UTF8.GetBytes("hello");
_encryptedData = rsa.Encrypt(dataToEncrypt, true);
}
Run Code Online (Sandbox Code Playgroud)
执行Encrypt方法时,我收到带有消息"Bad key"的CryptographicException.
我认为代码很好.可能我没有正确创建证书.任何意见?谢谢
----------------编辑--------------
如果有人知道如何使用OpenSsl创建证书,它对我来说也是一个有效的答案.
是否可以使用makecert创建具有特定密钥用法属性的自签名证书?
我需要生成一个自签名证书进行测试.它必须在"密钥用法"属性上具有"数字签名"和"不可否认"值,如RFC 3280第4.2.1.3节中所述.
我尝试了一些使用"-sky"选项的变体,例如"3"(位0和位1设置)和"1,2".第一个不被接受,第二个创建证书但它似乎没有设置"KeyUsage"属性.
请注意,这不是指"-eku"(扩展密钥用法).
这是我正在使用的脚本:
makecert -r -pe -n "CN=cte-dev-CA" -ss CA -sr CurrentUser -a sha1 -sky signature -sv cte-dev-CA.pvk cte-dev-CA.cer
certutil -user -addstore Root cte-dev-CA.cer
makecert -pe -n "CN=cte-dev-SPC" -eku 1.3.6.1.5.5.7.3.2,1.3.6.1.5.5.7.3.4 -a sha1 -sky signature -ic cte-dev-CA.cer -iv cte-dev-CA.pvk -sv cte-dev-SPC.pvk cte-dev-SPC.cer
pvk2pfx -pvk cte-dev-SPC.pvk -spc cte-dev-SPC.cer -pfx cte-dev-SPC.pfx -po my-password
Run Code Online (Sandbox Code Playgroud) 我使用的是 Windows 10。我没有 makecert.exe,当我尝试运行命令来生成证书时我才知道,就像
makecert.exe
我收到错误一样:
'makecert' 不是内部或外部命令,也不是可运行的程序或批处理文件。
我已经为 Windows 10 安装了 Windows SDK。
我在C#中使用Bouncy Castle库来签署SHA-256,我想在测试自生成证书而不是智能卡读卡器时使用它们.
使用之前使用的自我证书,我有一个加密例外:
指定的算法无效
如果我使用具有相同自我证书的SHA-1签名,那就顺利了.使用智能卡,相同的代码成功运行.
什么是makecert参数?
我使用SslStream来构建Web服务器。但是,下面的代码在AuthenticateAsServer时引发异常。
static X509Certificate cert;
protected virtual Stream GetStream(TcpClient client)
{
var ss = new SslStream(client.GetStream(), false);
if (cert == null)
{
cert = X509Certificate2.CreateFromCertFile("test.cer");
}
ss.AuthenticateAsServer(cert, false, System.Security.Authentication.SslProtocols.Tls, true);
return ss;
}
Run Code Online (Sandbox Code Playgroud)
我已经使用X509Certificate2加载cert文件,为什么它仍然引发异常(服务器模式SSL必须使用带有关联私钥的证书)?
cert文件是使用以下命令创建的:
makecert
-pe Exportable private key
-n "CN=localhost" Subject name
-ss my Certificate store name
-sr LocalMachine Certificate store location
-a sha1 Signature algorithm
-sky signature Subject key type is for signature purposes
-r Make a self-signed cert
"test.cer" Output filename
Run Code Online (Sandbox Code Playgroud) 我购买了一个真正的SSL证书来验证我的计算机和我正在托管一些WCF服务的域.
如何使用MakeCert创建由此正式证书签名的客户端证书,以便在客户端计算机上使用它来验证它们是否连接到正确的(我的)服务器?
客户端证书将用于验证在我的服务器上使用WCF服务.
我想创建一个X509证书用于测试目的.该证书必须由3台开发人员在其本地计算机上共享(即所有共享相同的证书,以便我们可以使用相同的指纹).
因此,此证书中的私钥必须是可导出的.
我使用以下命令创建证书:
makecert -r -pe -n "CN=mytestsite.local" -b 01/01/2000 -e 01/01/2036 -ss my -sr localMachine -sky exchange localhost.cer
Run Code Online (Sandbox Code Playgroud)
此证书工作正常,但问题是调用Certificates时isValid参数必须为false.查找...
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(
X509FindType.FindByThumbprint,
Config.PdfCertificateThumbprint,
false //********************* This has to be false.
).OfType<X509Certificate>().FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
只要我将IsValid属性设置为True,Find方法就不再返回我的证书.为什么makecert会生成"无效"证书?或者我如何弄清楚证书被认为无效的原因?
我想把我的证书放到特定的商店,但我现在不知道这个商店的名称作为makecert.exe的-ss选项的参数.在哪里可以找到该工具的每个选项的所有可能参数?既不是MSDN也不是 - ?(该工具的帮助选项)可以提供帮助.
例如,"受信任的根证书颁发机构"的-ss等价物是什么?还有另一个证书商店吗?
到目前为止,我能够在证书管理工具上导入证书的唯一方法是使用.pfx文件.我正在使用Makecert.exe实用程序来生成证书.
第一步 - 我使用以下命令生成证书颁发机构:
makecert.exe -n "CN=TestCA" -r -sv TestCA.pvk TestCA.cer
Run Code Online (Sandbox Code Playgroud)
第二步 - 我生成了私钥文件和证书文件
makecert.exe -n "CN=TestCert" -sv TestCert.pvk -iv TestCA.pvk -ic TestCA.cer TestCert.cer -sky signature -pe
Run Code Online (Sandbox Code Playgroud)
现在我能够看到我生成了4个文件,TestCA.pvk,TestCA.cer,TestCert.pvk,TestCert.cer
导入时我无法从证书管理工具中选择pvk文件.
我正在尝试生成一个自签名证书,但无法完成它。我正在使用以下命令来创建自签名证书颁发机构:
makecert -n "CN=PowerShell Local Certificate Root" -a sha1 -eku 1.3.6.1.5.5.7.3.3 -r -sv root.pvk root.cer -ss Root -sr localMachine
Run Code Online (Sandbox Code Playgroud)
从所有方面来看,这都是可行的,我可以在“受信任的根证书颁发机构”下查看新条目。
接下来,我使用以下命令创建签名证书:
makecert -pe -n "CN=PowerShell User" -ss MY -a sha1 -eku 1.3.6.1.5.5.7.3.3 -iv root.pvk -ic root1.cer
Run Code Online (Sandbox Code Playgroud)
但是命令失败并显示:
Error: Can't load the issuer certificate ('root1.cer')
Failed
Run Code Online (Sandbox Code Playgroud)
我的印象是该-ic开关将创建root1.cer文件,但该错误似乎表明它无法加载?我在哪里错呢?
makecert ×10
certificate ×4
c# ×3
bouncycastle ×1
encryption ×1
iis ×1
openssl ×1
private-key ×1
rsa ×1
security ×1
self-signed ×1
sha256 ×1
signature ×1
ssl ×1
sslstream ×1
wcf ×1
windows ×1
windows-10 ×1