/// <summary> /// Signs the plaintext message and returns the signature as an array of bytes. /// </summary> /// <param name="plainTextMessage"> /// Plaintext message to sign. /// </param> /// <returns></returns> public byte[] SignMessage(string plainTextMessage) { if (this.PrivateKey == null) { throw new NullReferenceException(INVALID_PRIVATE_KEY_ERROR_MESSAGE); } RSAPKCS1SignatureFormatter RSAformatter = new RSAPKCS1SignatureFormatter(this.PrivateKey); RSAformatter.SetHashAlgorithm(this.HashAlgorithm.ToString()); var hash = SimplerHasher.Hash(plainTextMessage, this.HashAlgorithm); return(RSAformatter.CreateSignature(hash)); }
/// <summary> /// Verify the plaintext message against the signature block. /// </summary> /// <param name="plainTextMessage"></param> /// <param name="signatureBlock"> /// The signature block as an array of bytes. /// </param> /// <returns></returns> public bool VerifySignature(string plainTextMessage, byte[] signatureBlock) { //Throw an exception for invalid keys. if (this.PublicKey == null) { throw new NullReferenceException(INVALID_PUBLIC_KEY_ERROR_MESSAGE); } try { //Hash the plain text message. var hash = SimplerHasher.Hash(plainTextMessage, this.HashAlgorithm); //Compare it to the signature block. var deformatter = new RSAPKCS1SignatureDeformatter(this.PublicKey); deformatter.SetHashAlgorithm(HashAlgorithm.ToString()); return(deformatter.VerifySignature(hash, signatureBlock)); } catch (Exception) { return(false); } }