{"id":984,"date":"2022-01-11T21:25:44","date_gmt":"2022-01-11T13:25:44","guid":{"rendered":"http:\/\/www.eait.co\/?p=984"},"modified":"2022-01-12T09:58:58","modified_gmt":"2022-01-12T01:58:58","slug":"python-aes%e5%8a%a0%e5%af%86%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=984","title":{"rendered":"python AES\u52a0\u5bc6\u89e3\u5bc6"},"content":{"rendered":"<p>\u53ef\u7b97\u641e\u6210\u4e00\u534a\u4e86\uff0c\u7ecf\u6838\u5bf9\uff0c\u52a0\u89e3\u5bc6\u540e\u54c8\u5e0c\u503c\u76f8\u7b49\uff0c\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# \u4e8c\u8fdb\u5236\u52a0\u5bc6\u89e3\u5bc6\u6d4b\u8bd5\u5b8c\u6210\r\nimport base64\r\nimport hashlib\r\nimport os\r\n\r\nfrom Crypto.Cipher import AES\r\nfrom Crypto import Random\r\n&quot;&quot;&quot;\r\n\u795e\u5751\uff1a\r\n\u5c06crypto\u7684\u5305\u6587\u4ef6\u5939\u6539\u6210\u5927\u5199\u5373\u53ef, \u6216\u8005\u5b89\u88c5pycryptodome\r\n\u5728 Windows \u4e2d\uff0c\u4e0d\u7ba1\u662f Python2 \u548c Python3 \uff0c\u90fd\u4e0d\u80fd\u7528 crypto \u548c pycrypto \uff0c\u53ef\u4ee5\u7528 pycryptodome \u3002\r\n\u5728 Linux \u4e2d\uff0c\u4e0d\u7ba1\u662f Python2 \u548c Python3 \uff0c\u90fd\u4e0d\u80fd\u7528 crypto \uff0c\u53ef\u4ee5\u7528 pycrypto \u548c pycryptodome \u3002\r\n\u8fd8\u8981\u5b89\u88c5cipher\r\n&quot;&quot;&quot;\r\nwith open(file='C:\/log2.txt',mode='r',encoding='utf-8') as f:\r\n    secret = f.read()\r\n    print(secret)\r\n\r\nclass AESCipher:\r\n    def __init__(self):\r\n        '''\r\n        CBC\u52a0\u5bc6\u9700\u8981\u4e00\u4e2a\u5341\u516d\u4f4d\u7684key(\u5bc6\u94a5)\u548c\u4e00\u4e2a\u5341\u516d\u4f4div(\u504f\u79fb\u91cf)\r\n        '''\r\n        self.key = self.check_key(secret)\r\n        # \u6570\u636e\u5757\u7684\u5927\u5c0f  16\u4f4d\r\n        self.BS = 16\r\n        # CBC\u6a21\u5f0f \u76f8\u5bf9\u5b89\u5168 \u56e0\u4e3a\u6709\u504f\u79fb\u5411\u91cf iv \u4e5f\u662f16\u4f4d\u5b57\u8282\u7684\r\n        self.mode = AES.MODE_CBC\r\n        # \u586b\u5145\u51fd\u6570 \u56e0\u4e3aAES\u52a0\u5bc6\u662f\u4e00\u6bb5\u4e00\u6bb5\u52a0\u5bc6\u7684  \u6bcf\u6bb5\u90fd\u662fBS\u4f4d\u5b57\u8282\uff0c\u4e0d\u591f\u7684\u8bdd\u662f\u9700\u8981\u81ea\u5df1\u586b\u5145\u7684\r\n        self.pad = lambda s: s + ((self.BS - len(s) % self.BS)*chr(self.BS - len(s) % self.BS)).encode()\r\n        # \u5c06\u586b\u5145\u7684\u6570\u636e\u5254\u9664\r\n        self.unpad = lambda s: s&#x5B;:-ord(s&#x5B;len(s) - 1:])]\r\n\r\n    def check_key(self, key):\r\n        '''\r\n        \u68c0\u6d4bkey\u7684\u957f\u5ea6\u662f\u5426\u4e3a16,24\u6216\u800532bytes\u7684\u957f\u5ea6\r\n        '''\r\n        try:\r\n            if isinstance(key, bytes):\r\n                assert len(key) in &#x5B;16, 24, 32]\r\n                return key\r\n            elif isinstance(key, str):\r\n                assert len(key.encode()) in &#x5B;16, 24, 32]\r\n                return key.encode()\r\n            else:\r\n                pass\r\n                # raise Exception(f'\u5bc6\u94a5\u5fc5\u987b\u4e3astr\u6216bytes,\u4e0d\u80fd\u4e3a{type(key)}')\r\n        except AssertionError:\r\n            print('\u8f93\u5165\u7684\u957f\u5ea6\u4e0d\u6b63\u786e')\r\n\r\n    def check_data(self, data):\r\n        '''\r\n        \u68c0\u6d4b\u52a0\u5bc6\u7684\u6570\u636e\u7c7b\u578b\r\n        '''\r\n        if isinstance(data, int):\r\n            data = str(data)\r\n        elif isinstance(data, bytes):\r\n            # data = data.decode()\r\n            data = data # \u6539\u5b57\u8282\u52a0\u89e3\u5bc6\u4e0d\u9700\u8981\u8fd9\u4e2a\r\n        elif isinstance(data, str):\r\n            pass\r\n        else:\r\n            pass\r\n            # raise Exception(f'\u52a0\u5bc6\u7684\u6570\u636e\u5fc5\u987b\u4e3astr\u6216bytes,\u4e0d\u80fd\u4e3a{type(data)}')\r\n        return data\r\n\r\n    def encrypt(self, raw):\r\n        raw = self.check_data(raw)\r\n        print('lenchecked raw',len(raw))\r\n        raw = self.pad(raw)   #\u6539\u5b57\u8282\u52a0\u5bc6\uff0c\u6ce8\u91ca\u6b64\u53e5\r\n        print('len pad rawlen',len(raw))\r\n        # \u968f\u673a\u83b7\u53d6iv\r\n        iv = Random.new().read(AES.block_size)\r\n        # \u5b9a\u4e49\u521d\u59cb\u5316\r\n        cipher = AES.new(self.key, self.mode, iv)\r\n        # \u6b64\u5904\u662f\u5c06\u5bc6\u6587\u548civ\u4e00\u8d77 base64 \u89e3\u5bc6\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u6839\u636e\u8fd9\u4e2aiv\u6765\u89e3\u5bc6\r\n        encdata = iv + cipher.encrypt(raw)\r\n        print('len enc data',len(encdata))\r\n        return encdata\r\n\r\n    def decrypt(self, enc):\r\n        # \u5148\u5c06\u5bc6\u6587\u8fdb\u884cbase64\u89e3\u7801\r\n        # enc = base64.b64decode(enc)   #\u52a0\u5bc6\u4e0d\u5b58base64\u4e86\uff0c \u540e\u9762\u76f4\u63a5\u7528\u4e8c\u8fdb\u5236 \u53ef\u8282\u7701\u7a7a\u95f430%\r\n        # \u53d6\u51faiv\u503c\r\n        iv = enc&#x5B;:self.BS]\r\n        # \u521d\u59cb\u5316\u81ea\u5b9a\u4e49\r\n        cipher = AES.new(self.key, self.mode, iv)\r\n        # \u8fd4\u56deutf8\u683c\u5f0f\u7684\u6570\u636e\r\n        return self.unpad(cipher.decrypt(enc&#x5B;self.BS:]))  #\u5220\u9664decode()\u4fdd\u7559\u4e8c\u8fdb\u5236\u5199\u5165\r\n\r\n\r\n\r\n# from random import randint\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    encpython = r'C:\\Users\\Administrator\\Desktop\\\u52a0\u5bc6'\r\n    files = os.walk(encpython)\r\n    for path,dirlist,filelist in files:\r\n        for file in filelist:\r\n            if not file.startswith('enc777'):\r\n                file_path = os.path.join(path, file)\r\n                with open(file=os.path.join(path,file),mode='rb') as f:\r\n                    file_content = f.read()\r\n                content_hash = hashlib.md5(file_content).hexdigest()\r\n                aes = AESCipher()\r\n                enc_content = aes.encrypt(file_content)\r\n                dec_contnet = aes.decrypt(enc_content)\r\n                content_hashdec = hashlib.md5(dec_contnet).hexdigest()\r\n                print(content_hash==content_hashdec,'ddddd')\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u7b97\u641e\u6210\u4e00\u534a\u4e86\uff0c\u7ecf\u6838\u5bf9\uff0c\u52a0\u89e3\u5bc6\u540e\u54c8\u5e0c\u503c\u76f8\u7b49\uff0c\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528 # \u4e8c\u8fdb\u5236\u52a0\u5bc6\u89e3\u5bc6\u6d4b\u8bd5\u5b8c\u6210 import base6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,25,29],"tags":[20],"class_list":["post-984","post","type-post","status-publish","format-standard","hentry","category-python","category-py","category-29","tag-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=984"}],"version-history":[{"count":2,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions\/986"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}