Powershell 正在向BizTalk发送S/Mime消息-但BizTalk无法对其进行解码

Powershell 正在向BizTalk发送S/Mime消息-但BizTalk无法对其进行解码,powershell,biztalk,smime,Powershell,Biztalk,Smime,我试图向BizTalk发送S/Mime消息,但它似乎无法接收 我可以使用BizTalk证书和System.Security.Cryptography.Pkcs类对代码中的消息进行加密和解密(我在Powershell中工作,作为概念证明)。但是,当我试图将其直接传递给BizTalk时,我看到以下消息: There was a failure executing the receive pipeline: [... application name ... ] Source: "MIME/SMIM

我试图向BizTalk发送S/Mime消息,但它似乎无法接收

我可以使用BizTalk证书和System.Security.Cryptography.Pkcs类对代码中的消息进行加密和解密(我在Powershell中工作,作为概念证明)。但是,当我试图将其直接传递给BizTalk时,我看到以下消息:

There was a failure executing the receive pipeline: 
[... application name ... ]
Source: "MIME/SMIME decoder" 
Receive Port: "ReceiveEncryptedPort" 
URI: "FORMATNAME:DIRECT=OS:.\PRIVATE$\encrypted_queue"
Reason: There was an authentication failure. "Failed to decode the S/MIME message. The S/MIME message may not be valid.".  
如果我从BizTalk中创建消息(当然对人和野兽都没有用),并且发现消息的格式如下,则可以正确使用该消息:

Mime Message with Base64 Encoded encrypted content.
    => Decrypts to Mime Message with Base64 Encoded Unicode content.
    => Decodes to message content.
但是,当我使用PowerShell脚本重新创建相同的模式时,会出现上述异常。如果我发送在BizTalk中创建的工作消息的确切文本,它似乎工作正常,这意味着我在其他地方的编码有问题,但因为我已从工作消息复制了所有头,以便在非工作消息中使用(除了添加新的内容Id)我发现很难理解BizTalk失败的区别是什么

示例消息如下所示:

Content-ID: {28c96069-f9a4-4cb3-9587-f1cb229dd54b} 
Bcc: MIME-Version 1.0
Content-type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m"
Content-Transfer-Encoding: base64

MIICggYJKoZIhvcNAQcDoIICczCCAm8CAQAxgcgwgcUCAQAwLjAaMRgwFgYDVQQDEw93d3cuZGxy
    -- More Base64 Encoded Text --
FZ6L1V+AylyzI7H+P0pmhA9yRl2Q/OiqRnNQ6tmw0mXkZxinuVryVha5aPkVhF19LJiS+vbjVWTF
jCDLdfJh4jMmOHlAiVOPc+TAIA==
function encryptWithCms( $text,  $certPath="cert:\CurrentUser\TrustedPeople", $certName="CN=myCertificate" )
{
   Add-Type -assemblyName "System.Security";
   $cert = Get-ChildItem $certPath | Where-Object { $_.Subject -eq $certName };
   $unicode = new-object System.Text.UnicodeEncoding;
   #this part copied directly from the internal message that BizTalk will accept.
   $pretext = @'
Content-Type: text/plain; charset="utf-16"
Content-Transfer-Encoding: base64
Content-Description: body

'@;
   $pretext+= "`r`n";
    $text = [System.Convert]::ToBase64String( $unicode.GetBytes( $text ));
    $text = $pretext+$text;
    Write-Host $text;
   $encryptData = $unicode.GetBytes( $text );
   $contentInfo = new-object System.Security.Cryptography.Pkcs.ContentInfo (,$encryptData);
   $cmsRecipient = new-object System.Security.Cryptography.Pkcs.CmsRecipient $cert;
   $envelopedCms = new-object System.Security.Cryptography.Pkcs.EnvelopedCms $contentInfo;
   $envelopedCms.Encrypt($cmsRecipient);
   return  [System.Convert]::ToBase64String($envelopedCms.Encode());
}
我想知道是否有可能Powershell以某种方式对文本进行了错误编码——使用ASCII而不是Unicode或其他什么——但这似乎有点脆弱,使BizTalk看起来异常脆弱

我用于加密消息的代码如下所示:

Content-ID: {28c96069-f9a4-4cb3-9587-f1cb229dd54b} 
Bcc: MIME-Version 1.0
Content-type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m"
Content-Transfer-Encoding: base64

MIICggYJKoZIhvcNAQcDoIICczCCAm8CAQAxgcgwgcUCAQAwLjAaMRgwFgYDVQQDEw93d3cuZGxy
    -- More Base64 Encoded Text --
FZ6L1V+AylyzI7H+P0pmhA9yRl2Q/OiqRnNQ6tmw0mXkZxinuVryVha5aPkVhF19LJiS+vbjVWTF
jCDLdfJh4jMmOHlAiVOPc+TAIA==
function encryptWithCms( $text,  $certPath="cert:\CurrentUser\TrustedPeople", $certName="CN=myCertificate" )
{
   Add-Type -assemblyName "System.Security";
   $cert = Get-ChildItem $certPath | Where-Object { $_.Subject -eq $certName };
   $unicode = new-object System.Text.UnicodeEncoding;
   #this part copied directly from the internal message that BizTalk will accept.
   $pretext = @'
Content-Type: text/plain; charset="utf-16"
Content-Transfer-Encoding: base64
Content-Description: body

'@;
   $pretext+= "`r`n";
    $text = [System.Convert]::ToBase64String( $unicode.GetBytes( $text ));
    $text = $pretext+$text;
    Write-Host $text;
   $encryptData = $unicode.GetBytes( $text );
   $contentInfo = new-object System.Security.Cryptography.Pkcs.ContentInfo (,$encryptData);
   $cmsRecipient = new-object System.Security.Cryptography.Pkcs.CmsRecipient $cert;
   $envelopedCms = new-object System.Security.Cryptography.Pkcs.EnvelopedCms $contentInfo;
   $envelopedCms.Encrypt($cmsRecipient);
   return  [System.Convert]::ToBase64String($envelopedCms.Encode());
}
为了将其转换为S/Mime消息,我做了一些非常类似的事情,从一个heredoc类型的字符串中预加标题,并将加密文本推到末尾,如上面的消息所示。为了可读性,我将它们放入72个字符的块中,这与BizTalk的做法相同,并且在运行PowerShell时不会影响解密


如果您想知道我还需要做些什么才能使这些请求正常工作,我们将非常欢迎。

出现以下问题的原因是因为提供的输入不是MIME格式。请检查提供的输入,它接受Base64格式

Error: There was an authentication failure. "Failed to decode the S/MIME message. The S/MIME message may not be valid.".  

如果MIME消息的格式错误,我们将面临这个问题。

您是否尝试过比较Powershell脚本和等效BizTalk编码消息的输出以检查差异?@NickHeppleston确实有,我就是这样列出编码/加密的每个步骤所包含的内容的。我所能看到的最大区别是,在最内层的编码中,实际的消息数据本身是base64编码的,使用Powershell进行编码的方式与使用Biztalk进行编码的方式不同,尽管从这两种方式进行解码的方式相同。如您所见,文本是由
System.Convert.ToBase64String
创建的,就Powershell(或我使用过的所有其他应用程序)而言,它是base64编码的,但相同的文本在由Biztalk编码时似乎不同。如果你能解释一下这一点,那将是很有帮助的。