使用Python加密您的邮件

我在什么地方读到其他人看过你的邮件。使用Gmail的人报告说,他们似乎收到了与他们的邮件相关的广告,所以可能是谷歌阅读了它!读完这篇文章后,恐慌和偏执突然袭来,你会想,我能做些什么呢?
我们可以用Python语言做什么?首先我想添加一条消息,然后我想加密它,用Gmail发送它,然后我希望以后能够解密它。

选择 | 换行 | 行号
  1. import smtplib,math
  2. from Crypto.Cipher import DES
  3.  
  4. message = """
  5. this is a secret message send from bytes.com
  6. -kudos
  7. """
  8.  
  9. # need to be divisible by 8 so we add extra ' '
  10.  
  11. v = len(message) / 8.0
  12. w = int(math.ceil(v) * 8.0)
  13. for i in range(w-len(message)):
  14.  message = message+" "
  15.  
  16. # here we add a key to encrypt the message, which we choose to be "thebytes"
  17.  
  18. des = DES.new('thebytes', DES.MODE_ECB)
  19. crypted = des.encrypt(message)
  20.  
  21. # create a string which will be easier to decode from mail
  22.  
  23. s=""
  24. for x in crypted:
  25.  s+=str(ord(x))+"#"
  26. s = s[0:len(s)-1] # remove the last '#'
  27.  
  28. # try to decode it, normally you would insert content from a mail
  29.  
  30. s2=""
  31. for b in s.split("#"):
  32.  s2+=chr(int(b))
  33.  
  34. print des.decrypt(s2) 
  35.  
  36. # now, mail it with gmail
  37.  
  38. server = smtplib.SMTP('smtp.gmail.com:587')  
  39. server.starttls()  
  40. server.login("your gmail username","your gmail password")
  41. server.sendmail("to address", "fron address", s)  
  42. server.quit()
  43.  

编码之后,你不禁会想,"为什么一开始就会有人读我的电子邮件?我有什么可隐瞒的?"

# 回答1


为什么会有人想八卦我枯燥的生活,但这是真的。这是一个很好的、简单的/容易理解的Crypto示例。

标签: python

评论已关闭