Skip to content

Commit 05f0cf1

Browse files
author
dahall
committed
Restored accidently deleted CryptEncryptMessage
1 parent 8bf589b commit 05f0cf1

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

PInvoke/Cryptography/Crypt32/Wincrypt.Msg.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,78 @@ public static extern bool CryptDecryptMessage(in CRYPT_DECRYPT_MESSAGE_PARA pDec
939939
public static extern bool CryptDecryptMessage(in CRYPT_DECRYPT_MESSAGE_PARA pDecryptPara, [In, SizeDef(nameof(cbEncryptedBlob))] IntPtr pbEncryptedBlob,
940940
uint cbEncryptedBlob, [Out, SizeDef(nameof(pcbDecrypted), SizingMethod.Query)] IntPtr pbDecrypted, ref uint pcbDecrypted, IntPtr ppXchgCert = default);
941941

942+
/// <summary>The <c>CryptEncryptMessage</c> function encrypts and encodes a message.</summary>
943+
/// <param name="pEncryptPara">
944+
/// <para>A pointer to a CRYPT_ENCRYPT_MESSAGE_PARA structure that contains the encryption parameters.</para>
945+
/// <para>
946+
/// The <c>CryptEncryptMessage</c> function does not support the SHA2 OIDs, <c>szOID_DH_SINGLE_PASS_STDDH_SHA256_KDF</c> and <c>szOID_DH_SINGLE_PASS_STDDH_SHA384_KDF</c>.
947+
/// </para>
948+
/// </param>
949+
/// <param name="cRecipientCert">Number of elements in the rgpRecipientCert array.</param>
950+
/// <param name="rgpRecipientCert">
951+
/// Array of pointers to CERT_CONTEXT structures that contain the certificates of intended recipients of the message.
952+
/// </param>
953+
/// <param name="pbToBeEncrypted">A pointer to a buffer that contains the message that is to be encrypted.</param>
954+
/// <param name="cbToBeEncrypted">The size, in bytes, of the message that is to be encrypted.</param>
955+
/// <param name="pbEncryptedBlob">
956+
/// <para>A pointer to BLOB that contains a buffer that receives the encrypted and encoded message.</para>
957+
/// <para>
958+
/// To set the size of this information for memory allocation purposes, this parameter can be <c>NULL</c>. For more information, see
959+
/// Retrieving Data of Unknown Length.
960+
/// </para>
961+
/// </param>
962+
/// <param name="pcbEncryptedBlob">
963+
/// <para>
964+
/// A pointer to a <c>DWORD</c> that specifies the size, in bytes, of the buffer pointed to by the pbEncryptedBlob parameter. When
965+
/// the function returns, this variable contains the size, in bytes, of the encrypted and encoded message copied to pbEncryptedBlob.
966+
/// </para>
967+
/// <para>
968+
/// <c>Note</c> When processing the data returned in the buffer of the pbEncryptedBlob, applications need to use the actual size of
969+
/// the data returned. The actual size can be slightly smaller than the size of the buffer specified on input. (On input, buffer
970+
/// sizes are usually specified large enough to ensure that the largest possible output data will fit in the buffer.) On output, the
971+
/// variable pointed to by this parameter is updated to reflect the actual size of the data copied to the buffer.
972+
/// </para>
973+
/// </param>
974+
/// <returns>
975+
/// <para>If the function succeeds, the function returns nonzero ( <c>TRUE</c>).</para>
976+
/// <para>If the function fails, it returns zero ( <c>FALSE</c>). For extended error information, call GetLastError.</para>
977+
/// <para>
978+
/// <c>Note</c> Errors from calls to CryptGenKey, CryptEncrypt, CryptImportKey, and CryptExportKey can be propagated to this function.
979+
/// </para>
980+
/// <para>The GetLastError function returns the following error codes most often.</para>
981+
/// <list type="table">
982+
/// <listheader>
983+
/// <term>Return code</term>
984+
/// <term>Description</term>
985+
/// </listheader>
986+
/// <item>
987+
/// <term>ERROR_MORE_DATA</term>
988+
/// <term>
989+
/// If the buffer specified by the pbEncryptedBlob parameter is not large enough to hold the returned data, the function sets the
990+
/// ERROR_MORE_DATA code and stores the required buffer size, in bytes, in the variable pointed to by pcbEncryptedBlob.
991+
/// </term>
992+
/// </item>
993+
/// <item>
994+
/// <term>E_INVALIDARG</term>
995+
/// <term>
996+
/// The message encoding type is not valid. Currently only PKCS_7_ASN_ENCODING is supported. The cbSize in *pEncryptPara is not valid.
997+
/// </term>
998+
/// </item>
999+
/// </list>
1000+
/// <para>
1001+
/// If the function fails, GetLastError may return an Abstract Syntax Notation One (ASN.1) encoding/decoding error. For information
1002+
/// about these errors, see ASN.1 Encoding/Decoding Return Values.
1003+
/// </para>
1004+
/// </returns>
1005+
// https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptencryptmessage BOOL CryptEncryptMessage(
1006+
// PCRYPT_ENCRYPT_MESSAGE_PARA pEncryptPara, DWORD cRecipientCert, PCCERT_CONTEXT [] rgpRecipientCert, const BYTE *pbToBeEncrypted,
1007+
// DWORD cbToBeEncrypted, BYTE *pbEncryptedBlob, DWORD *pcbEncryptedBlob );
1008+
[DllImport(Lib.Crypt32, SetLastError = true, ExactSpelling = true)]
1009+
[PInvokeData("wincrypt.h", MSDNShortId = "927f2e9a-96cf-4744-bd57-420b5034d28d")]
1010+
[return: MarshalAs(UnmanagedType.Bool)]
1011+
public static extern bool CryptEncryptMessage(in CRYPT_ENCRYPT_MESSAGE_PARA pEncryptPara, uint cRecipientCert, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] PCCERT_CONTEXT[] rgpRecipientCert,
1012+
[In, SizeDef(nameof(cbToBeEncrypted))] IntPtr pbToBeEncrypted, uint cbToBeEncrypted, [Out, SizeDef(nameof(pcbEncryptedBlob), SizingMethod.Query)] IntPtr pbEncryptedBlob, ref uint pcbEncryptedBlob);
1013+
9421014
/// <summary>
9431015
/// The <c>CryptGetMessageCertificates</c> function returns the handle of an open certificate store containing the message's
9441016
/// certificates and CRLs. This function calls CertOpenStore using provider type CERT_STORE_PROV_PKCS7 as its lpszStoreProvider parameter.

0 commit comments

Comments
 (0)