通过解码登录发送电子邮件

时间:2018-07-17 17:38:52

标签: python python-2.7 email gmail

我有两个脚本,第一个脚本旨在在两个单独的文本文件中写下电子邮件地址和密码(01.txt和02.txt)。使用简单的Caesar算法隐藏它们。 在第二个脚本中,我想对邮件地址和密码进行解密,然后发送邮件。 这很奇怪,因为当我这样做时,我会打印正确的登录名和密码,但不会发送任何内容,从而引发错误:SMTPAuthenticationError,这是不正确的,因为在打印时,我看到键入的信息正确无误解码。你有铅吗?

mail_begin.py:

def user():
    log = open("01.txt", "w")
    from_add = raw_input("Your mail address is : ")

    from_add = from_add.decode("utf-8")

    key = 3


    lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    lettres = lettres.decode("utf-8")

    crypted = ""


    for car in from_add:

        if car in lettres:

            num = lettres.find(car)

            num += key

            if num >= len(lettres):
                num = num - len(lettres)

            crypted += lettres[num]

        else:
            crypted += car

    log.write(crypted)
    log.close()

def password():

    log2 = open("02.txt", "w")
    passw2 = raw_input("Enter your password to log-in:")

    passw2 = passw2.decode("utf-8")

    key = 3


    lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    lettres = lettres.decode("utf-8")

    crypted = ""


    for car in passw2:

        if car in lettres:

            num = lettres.find(car)

            num += key

            if num >= len(lettres):
                num = num - len(lettres)

            crypted += lettres[num]

        else:
            crypted += car

    log2.write(crypted)
    log2.close()

user()
password()

mail_finished.py

import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

msg = MIMEMultipart()
name = open("01.txt",'r')
passw = open("02.txt",'r')
f_l1 = str(name.readlines())
f_l2 = str(passw.readlines())

print (f_l1)
print (f_l2)

f_l1 = f_l1.decode("utf-8")
f_l2 = f_l2.decode("utf-8")
key = 3


lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
lettres = lettres.decode("utf-8")


decrypted = ""
decrypted2 =""

for car in f_l1:
    if car in lettres:
        num = lettres.find(car)

        num -= key

        if num < 0 :
            num = num + len(lettres)

        decrypted += lettres[num]
    else:
        decrypted += car

for car in f_l2:
    if car in lettres:
        num = lettres.find(car)

        num -= key

        if num < 0 :
            num = num + len(lettres)

        decrypted2 += lettres[num]
    else:
        decrypted2 += car

msg['From'] = str(decrypted)
msg['To'] = str(decrypted)

body = "Time to go back to the lab ! Your scan is over"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
print decrypted # e-mail clear
print decrypted2 # password clear
server.login(decrypted, decrypted2) # not working, bad authentification...
text = msg.as_string()
server.sendmail(str(decrypted),str(decrypted), text)
server.quit()

name.close()
passw.close()

os.remove('01.txt')
os.remove('02.txt')

完成所有操作后,它会删除txt文件,因此没有人可以对其进行解码。 您可以在家尝试一下,看看问题出在哪里吗? 谢谢

2 个答案:

答案 0 :(得分:0)

readlines()通过在列表上调用str()来返回列表,您正在生成列表的字符串表示形式。

str(['mylist']) 

"['mylist']"

相反,您应该只使用readline而不是readlines来读取所需的行-它已经是一个字符串,不需要一直调用str()

with open("01.txt") as f:
    name = f.readline().strip()

with open("02.txt") as f:
    password = f.readline().strip()

答案 1 :(得分:0)

实际上,如果我只是修改:

f_l1 = (name.readlines())
f_l2 = (passw.readlines())

通过

f_l1 = (name.readline())
f_l2 = (passw.readline())

现在正在工作。谢谢您的回答nosklo