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
  • 相关阅读:
    京东Java面试题(二)
    京东Java面试题(一)
    阿里java面试题
    Java垃圾回收机制
    MyBatis面试题
    Java IO流总结
    Spring中文文档
    Vue.js实战之组件之间的数据传递
    Vue.js实战之Vuex的入门教程
    Vue系列——在vue项目中使用jQuery及其第三方插件
  • 原文地址:https://www.cnblogs.com/xiaoyaowuming/p/6600352.html
Copyright © 2011-2022 走看看