0

我正在尝试向 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 中创建的工作消息的确切文字,它似乎可以正常工作,这意味着我在其他地方的编码存在问题,但是因为我已经从工作消息中复制了所有标题以在非工作消息中使用那些(除了我添加了一个新的 Content-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==

我想知道Powershell是否有可能以某种方式对文本进行了错误的编码-使用ASCII而不是Unicode或其他东西-但这似乎有点脆弱,并且使BizTalk看起来难以置信的脆弱。

我用来加密消息的代码是这样的:

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 运行时不会影响解密。

欢迎任何关于我需要做什么才能使这些请求正常工作的想法。

4

1 回答 1

0

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

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

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

于 2013-08-23T12:27:42.823 回答