/// <summary> /// This takes the given message and verifies it using SSPI(VerifySignature). The given message /// should be formatted as follow: /// |MESSAGE_LENGTH(4 bytes)|MESSAGE /// </summary> /// <param name="messageToBeVerified">Signed message to be verified</param> /// <param name="signature">Signature</param> /// <returns>If true, verify successful, otherwise failed.</returns> /// <exception cref="SspiException">If verify fail, this exception will be thrown.</exception> /// <exception cref="ArgumentNullException">If messageToBeVerified is null, this exception will be thrown. /// </exception> /// <exception cref="ArgumentException">If messageToBeVerified is not formatted as /// "MESSAGE_LENGTH(4 bytes)|MESSAGE", this exception will be thrown.</exception> public bool VerifyMessage(byte[] messageToBeVerified, byte[] signature) { if (!SspiUtility.VerifyMessageHeader(messageToBeVerified)) { throw new ArgumentException( "Value of message header is not consistent with the actual length of message.", "messageToBeVerified"); } //Remove header. byte[] messageBody = new byte[messageToBeVerified.Length - sizeof(int)]; Array.Copy(messageToBeVerified, sizeof(int), messageBody, 0, messageBody.Length); return(Verify(messageBody, signature)); }
/// <summary> /// Decrypts the encrypted message(contains message header) and returns decrypted message. /// Schannel is not supported. /// The given message must be formatted as follow: /// MESSAGE_LENGTH(4 bytes)|MESSAGE /// </summary> /// <param name="messageToBeDecrypted">Message to be decrypted</param> /// <returns>Decrypted message</returns> /// <exception cref="ArgumentNullException">If messageToBeDecrypted is null, this exception will be thrown. /// </exception> /// <exception cref="ArgumentException">If messageToBeDecrypted is not formatted as /// "MESSAGE_LENGTH(4 bytes)|MESSAGE", this exception will be thrown.</exception> public byte[] DecryptMessage(byte[] messageToBeDecrypted) { if (!SspiUtility.VerifyMessageHeader(messageToBeDecrypted)) { throw new ArgumentException( "Value of message header is not consistent with the actual length of message.", "messageToBeDecrypted"); } //Remove message header int messageLength = BitConverter.ToInt32(messageToBeDecrypted, 0); byte[] message = ArrayUtility.SubArray(messageToBeDecrypted, sizeof(int), messageLength); byte[] signature = ArrayUtility.SubArray(messageToBeDecrypted, sizeof(int) + messageLength); return(Decrypt(message, signature)); }