0

I'm hoping this is just something simple. I'm trying to determine whether or not an email is already encrypted.

# Read e-mail from stdin
raw = sys.stdin.read()
raw_message = email.message_from_string( raw )

I took the example from http://docs.python.org/2/howto/regex.html on doing a simple test for match.

p = re.compile('-----BEGIN\sPGP\sMESSAGE-----')
m = p.match(raw)
if m:
    log = open(cfg['logging']['file'], 'a')
    log.write("THIS IS ENCRYPTED")
    log.close()
else:
    log = open(cfg['logging']['file'], 'a')
    log.write("NOT ENCRYPTED:")
    log.close()

The email is read. The log file is written to but it always comes back no match. I've written raw to a logfile and that string is present.

Not sure where to go next.

UPDATE: Here is the output from a raw ( a simple test message )

Sending email to: <bruce@packetaddiction.com>
Received: from localhost (localhost [127.0.0.1])
    by mail2.packetaddiction.com (Postfix) with ESMTP id 5FE5D22A65
    for <bruce@packetaddiction.com>; Tue, 10 Sep 2013 16:19:12 +0000 (UTC)
X-Virus-Scanned: Debian amavisd-new at mail2.packetaddiction.com
Received: from mail2.packetaddiction.com ([127.0.0.1])
    by localhost (mail2.packetaddiction.com [127.0.0.1]) (amavisd-new, port 10024)
    with ESMTP id cc3zZ_izEb1j for <bruce@packetaddiction.com>;
    Tue, 10 Sep 2013 16:19:06 +0000 (UTC)
Received: from mail.secryption.com (mail.secryption.com [178.18.24.223])
    by mail2.packetaddiction.com (Postfix) with ESMTPS id 9CA3C22A5B
    for <bruce@packetaddiction.com>; Tue, 10 Sep 2013 16:19:06 +0000 (UTC)
Received: from localhost (localhost.localdomain [127.0.0.1])
    by mail.secryption.com (Postfix) with ESMTP id 9994E1421F81
    for <bruce@packetaddiction.com>; Tue, 10 Sep 2013 12:19:19 -0400 (EDT)
X-Virus-Scanned: Debian amavisd-new at mail.secryption.com
Received: from mail.secryption.com ([127.0.0.1])
    by localhost (mail.secryption.com [127.0.0.1]) (amavisd-new, port 10024)
    with ESMTP id WbkVn_cowG6q for <bruce@packetaddiction.com>;
    Tue, 10 Sep 2013 12:19:18 -0400 (EDT)
Received: from dennis.cng.int (mail.compassnetworkgroup.com [173.163.129.21])
    (using TLSv1 with cipher RC4-MD5 (128/128 bits))
    (No client certificate requested)
    by mail.secryption.com (Postfix) with ESMTPSA id 5B4191421F80
    for <bruce@packetaddiction.com>; Tue, 10 Sep 2013 12:19:18 -0400 (EDT)
User-Agent: K-9 Mail for Android
MIME-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 8bit
Subject: Message
From: Bruce Markey <bruce@secryption.com>
Date: Tue, 10 Sep 2013 12:19:00 -0400
To: "bruce@packetaddiction.com" <bruce@packetaddiction.com>
Message-ID: <36615ed6-a1a9-49ac-ac85-31905916d478@email.android.com>

-----BEGIN PGP MESSAGE-----
Version: APG v1.0.8

hQEMAwPNxvNWsisWAQgAuOTLkiitYzhGJydOzN4sBoGjhRm9JeJMfmxKxKTKcV2W
ZBuN0z+nS1KxnXrIlahhwLtpiFvp5apI8wAyAiLC2BhFieFttOl1/xLVJbd1nI1o
KQE1RUXhPURejJ3eH9g/LmkhtFQcnsuHGTGnLi6dugBNhWLqgnLUBX+VLt6moz2C
84lDuQ1y7B/JFOctKRScUqmxDd8b2peZJOnVT/p0tSYNfN9QGH3W02FZShE4KKBl
HpezK8KC6cZdf34Eao+ep+fP5DuKx/4j3ksCbFKyQ3gd+yxK/xnhkijDsYCfFRiF
ElAGDvXu4RXqrKRpBxq1bRhU8YqS7j5593MTUViWitLAGgH1DV0UeA/B5LMUDRyz
4ZfDqd0kDYsPUy2Cg20HdXHaobkzdvHLzfqQq0Owc1nTcvu4nzCbIMhTAlZjn8ZA
aODTlKcvnFBWEtNERPm0x6nkbhMo3GeysejaJSRod3aGqhuhga4iIrrew1W03297
aalwY8RKeNoV15VItsyrbbT+HvDNSaFFCPUAs+KcLHCOez5/woozjlqKdBI6yHCe
gqpYJPP07qFsVviltfDO63xS48f2HCPe4iyXCy6Usp0+jM7zAzH7KH1O854GH46Q
r0A01DLo9REmDr4U
=pBQZ
-----END PGP MESSAGE-----
4

2 回答 2

5

re.match will only find a match at the beginning of the string, as noted here. You want to use re.search

raw = """Sending email to: <bruce@packetaddiction.com>...
...
-----BEGIN PGP MESSAGE-----
...
"""
>>> p = re.compile('-----BEGIN\sPGP\sMESSAGE-----')
>>> m = p.search(raw)
>>> m
<_sre.SRE_Match object at 0x0000000002E02510>
>>> m.group()
'-----BEGIN PGP MESSAGE-----'

>>> m = p.match(raw)
>>> print m
None

Although, as noted, regex is likely overkill for this problem as the matching text is static.

于 2013-09-10T16:32:57.203 回答
4

Regular expressions are used when you want a "fuzzy" match - that is, you aren't sure if the string you are looking for will be identical every time.

In this case, the string you are looking for appears to be exactly -----BEGIN PGP MESSAGE----. In this case, the string.find function will be simpler to use and faster to boot.

>>> a = "This is a PGP encrypted email. -----BEGIN PGP MESSAGE----- !@#$%^..."
>>> b = "This is not encrypted. My hovercraft is full of eels." #example strings
>>> a.find("-----BEGIN PGP MESSAGE-----")
30    # Return value '30' means that the search string was found at index 30 of source string
>>> b.find("-----BEGIN PGP MESSAGE-----")
-1    # -1 means 'not found in the source string'
>>> 
于 2013-09-10T16:18:24.533 回答