2017-04-12 70 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

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