运行x509的库报错
调用.net的库运行报错如下。
" at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)\r\n at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest, Boolean renegotiation)\r\n at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)\r\n at console.TLSServer.Main(String[] args) in C:\Users\admin\Desktop\Socket\ssl\Program.cs:line 103" 调用 SSPI 失败,请参见内部异常。
1.在使用 New-SelfSignedCertificate 命令创建自签名证书时,你可以通过 -KeyExportPolicy Exportable 参数来设置私钥可导出,然后你将能够为私钥设置密码。下面是具体步骤:
2.打开 PowerShell 作为管理员(右键单击 PowerShell,选择“以管理员身份运行”)。
3.使用以下命令创建自签名证书,并设置私钥可导出:
New-SelfSignedCertificate -DnsName “YourServerName” -CertStoreLocation “Cert:\LocalMachine\My” -KeyExportPolicy Exportable
请确保将 “YourServerName” 替换为你的服务器名称或域名。
4.命令执行成功后,它将创建一个新的自签名证书并将其存储在“个人”证书存储区域(My)中,并且私钥可导出。
5.现在,你可以为私钥设置密码。使用以下命令来为私钥设置密码:
$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Subject -eq “CN=YourServerName” }
$cert | Export-PfxCertificate -FilePath “C:\Path\To\ExportedCertificate.pfx” -Password (ConvertTo-SecureString -String “YourPassword” -Force -AsPlainText)
请将 “YourPassword” 替换为你想要设置的密码,并将输出的证书保存到指定路径。
这样,你就为自签名证书的私钥设置了密码。确保将密码保存在安全的地方,因为它将用于访问证书的私钥。在你的应用程序中,你需要提供这个密码以访问证书的私钥。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace console
{
class TLSServer
{
/// <summary>
/// 取整数的某一位
/// </summary>
/// <param name="_Resource">要取某一位的整数</param>
/// <param name="_Mask">要取的位置索引,自右至左为0-7</param>
/// <returns>返回某一位的值(0或者1)</returns>
public static
运行x509库报错原因分析与解决

博客讲述运行x509的库调用.net库报错情况,给出创建自签名证书并为私钥设置密码的步骤。同时分析报错原因,可能是证书、协议、密钥等问题,还提供诊断和解决问题的步骤,如检查配置、添加异常处理等。
最低0.47元/天 解锁文章
1465

被折叠的 条评论
为什么被折叠?



