Make e-mail text more secure (Python)

vegaseat 4 Tallied Votes 451 Views Share

In today's environment where just about anyone can snoop around your e-mail, it is prudent to securely encrypt your more private writings. This little Python program will make it easier to do so.

''' encrypt109_py23.py
using the Python module base64 to encode and decode text
also added list based XOR crypting against an expanded password
modified to work with Python2 and Python3
tested with Python27, Python33 and IronPython273  by  vegaseat  02aug2013
'''

import base64
import operator

def password_expand(pw):
    '''
    make the password longer and more secure
    '''
    s1 = pw
    s2 = "".join(chr(ord(c) + 3) for c in pw)
    s3 = "".join(chr(ord(c) - 5) for c in pw)
    return s1 + s2 + s3

def encrypt109(text, password):
    '''
    use base64 encoding
    modified to work with Python2 and Python3
    '''
    x_text = XOR_crypt2(text, password)
    try:
        # Python27
        return base64.encodestring(x_text)
    except TypeError:
        # Python3
        return base64.encodestring(x_text.encode("utf8")).decode("utf8")

def decrypt109(e_text, password):
    '''
    use base64 decoding
    modified to work with Python2 and Python3
    '''
    try:
        # Python27
        b64_text = base64.decodestring(e_text)
    except TypeError:
        # Python3
        b64_text = base64.decodestring(e_text.encode("utf8")).decode("utf8")
    return XOR_crypt2(b64_text, password)

def XOR_crypt2(text, password):
    '''
    xor crypt using list container
    '''
    xlist = []
    n = 0
    for c in text:
        # loop through password start to end and repeat
        if n >= len(password) - 1:
            n = 0
        pw = ord(password[n])
        n += 1
        bt = ord(c)
        # xor byte with password byte
        xbt = operator.xor(bt, pw)
        # convert to character and append to xlist
        xlist.append(chr(xbt))
        # convert xlist to string and return
    return ''.join(xlist)

text = '''\
It has been a rough day. I got up this morning and put on my
shirt and a button fell off. I picked up my briefcase and
the handle came off. Now I am afraid to go to the bathroom.'''

# pick a password you like (don't forget it!)
original_password = 'ford75'
# program uses the expanded password to make it longer and more secure
password = password_expand(original_password)

encrypted = encrypt109(text, password)
# this is the text you would send via e-mail (paste it)
print("encrypted:\n{}".format(encrypted))

print('-'*50)

# the recipient of the e-mail would paste the encrypted text
# also needs the original password (this program will expand it)
decrypted = decrypt109(encrypted, password)
print("decrypted:\n{}".format(decrypted))

''' my result ...
encrypted:
LxtSDFZGSRAQAlQYAEofMEcBB1IAVkxHUjxHXVcVShgvEhIHGxcXWAYAGw5UX0ELAzsSFhoGRFhb
SR8MbUlQCBgZf1MIC1IFF1ccBgEIVBgHDwEzEgkJFEoXfEkCHARRXQVKGC8SCxZSBkVcDBQWBkld
QQsDOzgSBxdEX1QHFhkCGlsABwh/XQAJXER5Wh5SPEdbVUELCy1TDwtSEFgVDh1VE1UYFQIIf1AH
GxoWWFoEXA==

--------------------------------------------------
decrypted:
It has been a rough day. I got up this morning and put on my
shirt and a button fell off. I picked up my briefcase and
the handle came off. Now I am afraid to go to the bathroom.

'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If you use IronPython, you can compile this program and send it to any Windows user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.