相关疑难解决方法(0)

如何从PKCS#12字节数组构造X509Certificate2抛出CryptographicException("系统找不到指定的文件.")?

我正在尝试X509Certificate2从字节数组中的PKCS#12 blob 构造一个并且得到一个相当令人费解的错误.此代码在具有Windows XP管理员权限的桌面应用程序中运行.

堆栈跟踪如下,但由于_LoadCertFromBlob已标记,我因试图进行故障排除而丢失[MethodImpl(MethodImplOptions.InternalCall)].

System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
  at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
  at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
  at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
  at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
Run Code Online (Sandbox Code Playgroud)

编辑: blob是由BouncyCastle为C#生成的真正的PKCS#12,包含RSA私钥和证书(自签名或最近注册CA) - 我要做的是转换私钥和证书从BouncyCastle库到System.Security.Cryptography库,从一个导出到另一个导入.此代码适用于已经尝试过的绝大多数系统; 我从未见过该构造函数抛出的特定错误.这个盒子可能是某种环境怪异.

编辑2:错误发生在不同城市的不同环境中,我无法在本地重现它,所以我可能最终不得不将其粉碎到破损的XP安装.

既然你问过,这里是有问题的片段.该代码采用BouncyCastle表示中的私钥和证书,从个人密钥库中删除相同专有名称的任何先前证书,并通过中间PKCS#12 blob将新私钥和证书导入个人密钥库.

// open the personal keystore
var msMyStore = new X509Store(StoreName.My);
msMyStore.Open(OpenFlags.MaxAllowed);

// remove any certs previously issued for the same DN
var …
Run Code Online (Sandbox Code Playgroud)

.net c# cryptography pkcs#12 x509certificate2

37
推荐指数
3
解决办法
3万
查看次数

Azure函数中的运行时错误加载证书

我想创建一个Azure函数(C#API通用HTTP)方法,将文件上载到Office365 Sharepoint文档库.

由于OneDrive API允许我上传大文件(使用守护程序进程和证书身份验证),因此我成功地使用C#控制台应用程序实现了目标.

现在的想法是将代码移动到Azure功能中.但是,在加载pfx证书时,我在函数运行期间收到错误.

public static async Task<bool> Run(HttpRequestMessage req, TraceWriter log)
{
   string certfile = System.IO.Path.Combine(Environment.ExpandEnvironmentVariable??s("%HOME%"), @"site\wwwroot\<functionname>\mykeyfile.pfx"); 

    X509Certificate2 cert = new X509Certificate2(certfile, "<myinsanepwd>");

    return true; //temporary 
}
Run Code Online (Sandbox Code Playgroud)

行X509Certificate2 cert = new X509Certificate2(certfile,""); 抛出异常System.Security.Cryptography.CryptographicException:系统找不到指定的文件.

这真的很奇怪,因为文件存在于指定的路径上(我在方法中使用File.Exists()检查:))这个错误可能与support.microsoft.com/en-us/kb/948154有关吗?我怎么解决这个问题?

此致,Jens

c# azure azure-functions

5
推荐指数
1
解决办法
3024
查看次数