首页 > 编程语言 > 详细

Python AES - base64 加解密

时间:2016-11-27 07:24:05      阅读:335      评论:0      收藏:0      [点我收藏+]

 

首先python引用AES加密 

from Crypto.Cipher  import AES

  需要先安装  Crypto  模块, 可以使用 easy_install 进行安装   会自动去官网进行搜索安装

  其中代码示例:

    aes 加密 需要进行加密数据的处理,要求数据长度必须是16的倍数,不足时,在后边补0

class MyCrypt():
    def __init__(self, key):
        self.key = key
        self.mode = AES.MODE_CBC

    def myencrypt(self, text):
        length = 16
        count = len(text)
        print count
        if count < length:
            add = length - count
            text= text + (\0 * add)

        elif count > length:
            add = (length -(count % length))
            text= text + (\0 * add)

        cryptor = AES.new(self.key, self.mode, self.key)
        self.ciphertext = cryptor.encrypt(text)
        return b2a_hex(self.ciphertext)

    def mydecrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.key)
        plain_text = cryptor.decrypt(text)
        return plain_text.rstrip(\0)

 

关于base64 加密 比较简单

  import base64 即可

>>> dir(base64)
[EMPTYSTRING, MAXBINSIZE, MAXLINESIZE, __all__, __builtins__, __doc__, __file__, __name__, __package__, _b32alphabet, 
_b32rev, _b32tab, _translate, _translation, _urlsafe_decode_translation, _urlsafe_encode_translation, _x, b16decode,
b16encode, b32decode, b32encode, b64decode, b64encode, binascii, decode, decodestring, encode, encodestring, k,
re, standard_b64decode, standard_b64encode, string, struct, test, test1, urlsafe_b64decode, urlsafe_b64encode, v]

   其中有 urlsafe 加密解密 , 可以不用在base64基础上进行再次处理。

 

Python AES - base64 加解密

原文:http://www.cnblogs.com/vagabond/p/6105675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!