Perl MIME-Tools:更改了正文部分的内容

时间:2018-02-15 06:46:09

标签: perl mime

我有以下示例输入到mime-parser(mytest)。

Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="----=_Part_abc123"

------=_Part_abc123
Content-Type: application/text; name=test.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=test.txt

VUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVU
VUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVU
------=_Part_abc123
Content-Type: application/pkcs7-signature; name=smime.p7s; smime-type=signed-data
Content-Transfer-Encoding: binary
Content-Disposition: attachment; filename="smime.p7s"
Content-Description: S/MIME Cryptographic Signature

blabla
------=_Part_abc123--

代码是这样的(只需读入并再次写出来)。

use strict;
use warnings;
use MIME::Parser;

my $fn = "mytest";
my $parser = new MIME::Parser ();
$parser->output_to_core (1);

open (F, $fn);
my $entity = $parser->parse (\*F);
close (F);

open (F, ">mytestnew");
$entity->print (\*F);
close (F);

exit 1;

产生的输出(mytestnew)就是这个。

Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="----=_Part_abc123"

------=_Part_abc123
Content-Type: application/text; name=test.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=test.txt

VUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVU
VUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVU

------=_Part_abc123
Content-Type: application/pkcs7-signature; name=smime.p7s; smime-type=signed-data
Content-Transfer-Encoding: binary
Content-Disposition: attachment; filename="smime.p7s"
Content-Description: S/MIME Cryptographic Signature

blabla
------=_Part_abc123--

我们可以看到第一部分的内容发生了变化(参见VUVU ......换行)。

如果使用签名(第2部分)来验证内容,则会出现问题。也许是因为base64编码不是“漂亮”。但是签名的验证并不关心这一点。它只是看到一个腐败的内容和failes。 在我的样本中,我剪切了签名 - 问题不在于验证。

那么如何解析内容,再次使用MIME工具写出并保持内容不变?

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

来自docs

  

不打算准确打印您解析的内容!

     

解析是一种(略微)有损操作。因为像   base64-encoding中的歧义,以下不会吐出来   它的输入在所有情况下都保持不变:

$entity = $parser->parse(\*STDIN);
$entity->print(\*STDOUT);
     

如果您使用MIME :: Tools处理电子邮件,请记得 保存   如果要以未更改的方式发送 ,则解析数据。这至关重要   像PGP签名的电子邮件。