zoukankan      html  css  js  c++  java
  • Python AES

    首先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 + ('' * add)
    
            elif count > length:
                add = (length -(count % length))
                text= text + ('' * 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('')

    关于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基础上进行再次处理。

  • 相关阅读:
    php八种常用函数
    已知二叉树的前序中序遍历,如何得到它的后序遍历?
    PTA_Have fun with numbers(C++)
    PTA_输入符号及符号个数打印沙漏(C++)
    Web安全之SQL注入
    南京邮电大学//bugkuCTF部分writeup
    修改或添加HTTP请求头
    第二次作业
    博客作业1
    linux python 串口
  • 原文地址:https://www.cnblogs.com/vagabond/p/6105675.html
Copyright © 2011-2022 走看看