zoukankan      html  css  js  c++  java
  • AEE加密解密

     from Crypto.Cipher import AES
    from binascii import b2a_hex, a2b_hex


    class AesHandler(object):
        def __init__(self, key, iv, mode):
            self.key = key
            self.iv = iv
            self.mode = mode
            self.BS = AES.block_size  # Size of a data block (in bytes) 16
            self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(
                self.BS - len(s) % self.BS)
            self.unpad = lambda s: s[0:-ord(s[-1])]

        def encrypt(self, text):
            text = self.pad(text)
            cipher = AES.new(self.key, self.mode, self.iv)
            cipher_text = cipher.encrypt(text)
            return b2a_hex(cipher_text)

        def decrypt(self, text):
            cipher = AES.new(self.key, self.mode, self.iv)
            plain_text = cipher.decrypt(a2b_hex(text))
            return self.unpad(plain_text.rstrip(''))


    if __name__ == '__main__':
        key = 'zhyh37MmA67Ato%Z'
        iv = 'Q@p%TLoLCEMollMq'
        aes = AesHandler(key, iv, AES.MODE_CBC)
        e = aes.encrypt("in the wifi.com")
        print "加密:", e
        d = aes.decrypt(e)
        print "解密:", d
  • 相关阅读:
    ExtJs控件属性配置详细
    static void和void区别(转)
    OpenSSL 内存管理分析笔记
    Openssl 之大数运算函数 BN
    python学习:字符串
    python学习:字典
    python学习:购物车程序
    python学习:元组和嵌套
    python学习:列表
    python学习:continue及break使用
  • 原文地址:https://www.cnblogs.com/xiaoyaowuming/p/6600352.html
Copyright © 2011-2022 走看看