Chinaunix首页 | 论坛 | 博客
  • 博客访问: 77255
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 156
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-23 09:58
个人简介

Dev Ops, 我刚起步啊

文章分类

全部博文(14)

文章存档

2017年(2)

2016年(5)

2015年(7)

我的朋友

分类: Python/Ruby

2015-03-10 17:52:57

env: python 6.6
install pip install PyCrypto==2.3


点击(此处)折叠或打开

  1. from Crypto.Cipher import AES

  2. import base64
  3. import urllib
  4. import binascii


  5. """
  6.   reference : https://www.dlitz.net/software/pycrypto/api/current/
  7. """
  8. padding = "ThisisPadding"
  9. CHARSET_UTF8="UTF8"
  10. key=b"abcdefghijklmnop"

  11. originalUrl = ""
  12. originalUrl = ""*3+"123"

  13. def encrypt(msg):

  14.     #base64_encoded = base64.encodestring(encoded_url) generate multiple lines

  15.     EncodeAES = lambda c,e:c.encrypt(e)

  16.     #AES key must be either 16, 24, or 32 bytes long
  17.     cipher = AES.new(key)

  18.     #The string here must be multiple 16 length
  19.     encrypted_msg = EncodeAES(cipher,msg+padding)

  20.     base64_encoded = base64.b64encode(encrypted_msg)

  21.     encoded = urllib.quote(base64_encoded).encode(CHARSET_UTF8)
  22.     return encoded



  23. def decrypt(msg):

  24.     print "msg:%d" % len(msg)
  25.     decoded_url = urllib.unquote(msg).decode(CHARSET_UTF8)

  26.     print "durl:%d" %len(decoded_url)

  27.     base64_decoded = base64.decodestring(decoded_url)

  28.     print "b64:%d" %len(base64_decoded)

  29.     DecodeAES = lambda c, e: c.decrypt(e).rstrip(padding)

  30.     cipher = AES.new(key)

  31.     decoded = DecodeAES(cipher, base64_decoded)

  32.     print decoded

  33. enmsg=encrypt(originalUrl)

  34. print enmsg

  35. decrypt(enmsg)

阅读(2536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~