运行x509的库做TSL加密的时候报错“调用 SSPI 失败,请参见内部异常”。

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

运行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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值