2017-04-12 67 views
0

我移植一个.NET 4.5的项目到.NET的核心,我被吸进的功能...移植x509证书和CryptoConfig从.NET 4.5到.NET核心

public bool IsMessageSignatureValid(byte[] bytes, string signature) { 
    X509Certificate2 certificate = GetX509Certificate(); 
    RSACryptoServiceProvider rsa = certificate.PublicKey.Key as RSACryptoServiceProvider; 
    return rsa.VerifyData(bytes, CryptoConfig.MapNameToOID("SHA1"), Convert.FromBase64String(signature)); 
} 
  • 有”再在System.Security.Cryptography.X509Certificates.PublicKey .Net的核心没有Key财产
  • 没有CryptoConfig类System.Security.Cryptography .Net的核心

任何人都可以给我一些想法如何到FI x它?非常感谢

回答

2

您错过了在.NET 4.6中对证书和RSA所作的增强。 .NET Core 1.0和1.1仅允许采用新的方式进行操作,因为它在非Windows系统上运行得更好。

public bool IsMessageSignatureValid(byte[] bytes, string signature) { 
    // New instance each call? If so, dispose this. 
    X509Certificate2 certificate = GetX509Certificate(); 

    // GetRSAPublicKey returns a new object each time, 
    // you should definitely using/Dispose it. 
    using (RSA rsa = certificate.GetRSAPublicKey()) 
    { 
     return rsa.VerityData(
      bytes, 
      Convert.FromBase64String(signature), 
      HashAlgorithmName.SHA1, 
      RSASignaturePadding.Pkcs1); 
    } 
} 
+0

是的,我刚刚发现那分钟前,反正谢谢! – user163695

+0

你救了我的一天。谢谢。 –