protected RSACryptoServiceProvider GetRsaProvider() { var rsaParameters = new RSAParameters(); if (HasPrivateKey) { rsaParameters = PemUtilsHelper.ToRSAParameters(PrivateKey); } else { rsaParameters = PemUtilsHelper.ToRSAParameters(PublicKey); } var rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParameters); return(rsa); }
public bool ValidSignature(string data, string base64Data, HashAlgorithmName hashAlgorithm) { try { var sha = new SHA256CryptoServiceProvider(); var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(data)); var signature = Convert.FromBase64String(base64Data); using RSA rsa = RSA.Create(); var rsaParameters = PemUtilsHelper.ToRSAParameters(PrivateKey); rsa.ImportParameters(rsaParameters); return(rsa.VerifyHash(hash, signature, hashAlgorithm, RSASignaturePadding.Pss)); } catch (Exception ex) { throw new Exception("The public key could not be properly verified.", ex); } }
public string SignWithPrivateKey(string data, HashAlgorithmName hashAlgorithm, bool useIndent = false) { try { using RSA rsa = RSA.Create(); var rsaParameters = PemUtilsHelper.ToRSAParameters(PrivateKey); rsa.ImportParameters(rsaParameters); var sha = new SHA256CryptoServiceProvider(); var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(data)); var signature = rsa.SignHash(hash, hashAlgorithm, RSASignaturePadding.Pss); if (useIndent) { return(Convert.ToBase64String(signature, Base64FormattingOptions.InsertLineBreaks)); } return(Convert.ToBase64String(signature)); } catch (Exception ex) { throw new Exception("Data could not signing.", ex); } }