{"id":707,"date":"2024-07-02T16:23:22","date_gmt":"2024-07-02T08:23:22","guid":{"rendered":"https:\/\/www.madbull.site\/?p=707"},"modified":"2024-07-02T16:24:00","modified_gmt":"2024-07-02T08:24:00","slug":"python%e5%ae%9e%e7%8e%b0%e5%af%b9%e7%a7%b0%e7%a7%98%e9%92%a5%e5%8a%a0%e8%a7%a3%e5%af%86%e5%92%8c%e9%9d%9e%e5%af%b9%e7%a7%b0%e7%a7%98%e9%92%a5%e5%8a%a0%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=707","title":{"rendered":"python\u5b9e\u73b0\u5bf9\u79f0\u79d8\u94a5\u52a0\u89e3\u5bc6\u548c\u975e\u5bf9\u79f0\u79d8\u94a5\u52a0\u89e3\u5bc6"},"content":{"rendered":"\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n\nimport os\nimport sys\n\nimport base64\nimport traceback\nfrom Crypto.PublicKey import RSA\nfrom Crypto.Cipher import PKCS1_v1_5\nfrom Crypto.Cipher import ARC4\n\npriv_file = \"priv.pem\"\npub_file = \"pub.pem\"\n\n# \u6839\u636e\u6587\u4ef6\u83b7\u53d6RSA\u5bf9\u8c61\ndef get_rsa_key_obj(key_file) :\n    key_obj = None \n    with open(key_file, \"r\") as f:\n        key_str = f.read()\n        key_obj = PKCS1_v1_5.new(RSA.importKey(key_str))\n    return key_obj\n\n# \u4f7f\u7528RSA\u516c\u94a5\u52a0\u5bc6\ndef rsa_encrypt(key_obj, plaintext, length=100) :\n    try:\n        data_lst = &#91;]\n        for i in range(0, len(plaintext), length):\n            data_tmp = key_obj.encrypt(plaintext&#91;i:i+length])\n            data_lst.append(data_tmp)\n        ciphertext = b\"\".join(data_lst)\n        msg_b64 = base64.b64encode(ciphertext)\n        return msg_b64\n    except Exception as e:\n        traceback.print_exc()\n        return None\n    \n# \u4f7f\u7528RSA\u79c1\u94a5\u89e3\u5bc6\ndef rsa_decrypt3(key_obj, msg_b64, length=128) :\n    msg = base64.b64decode(msg_b64)\n    try:\n        data_lst = &#91;]\n        for i in range(0, len(msg), length):\n            data_tmp = key_obj.decrypt(msg&#91;i:i+length],\"\")\n            data_lst.append(data_tmp) \n        plaintext = b''.join(data_lst)\n        return plaintext \n    except Exception :\n        traceback.print_exc()\n        return None\n\n# rc4\u89e3\u5bc6\ndef rc4_encrypt(msg,key):\n    cipher = ARC4.new(key)\n    ciphertext=cipher.encrypt(msg)\n    return base64.b64encode(ciphertext)\n\n# rc4\u52a0\u5bc6\ndef rc4_decrypt(msg,key):\n    cipher = ARC4.new(key)\n    ciphertext = base64.b64decode(msg)\n    return cipher.decrypt(ciphertext)\n\n\nif __name__ == \"__main__\":\n    RSA_pub_key_obj = get_rsa_key_obj(pub_file) \n    RSA_priv_key_obj = get_rsa_key_obj(priv_file) \n\n    ret = rsa_encrypt(RSA_pub_key_obj, b\"12345678\")\n    print(ret)\n    print(\"length : \"+str(len(ret)))\n    ret = rsa_decrypt3(RSA_priv_key_obj, ret) \n    print(ret)\n    print(\"length : \"+str(len(ret)))\n\n    ret = rc4_encrypt(b\"1234567890\", b\"abcde\")\n    print(ret)\n    print(\"length : \"+str(len(ret)))\n    ret = rc4_decrypt(ret, b\"abcde\")\n    print(ret)\n    print(\"length : \"+str(len(ret)))<\/code><\/pre>\n<\/div>\n\n\n\n<p>\u751f\u6210\u4e00\u4e2a\u5bc6\u94a5\u5bf9 priv.pem\uff0c\u5e76\u628a\u516c\u94a5 pub.pem \u4ece\u5bc6\u94a5\u5bf9\u91cc\u63d0\u53d6\u51fa\u6765\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"876\" height=\"174\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111.png\" alt=\"\" class=\"wp-image-708\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111.png 876w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111-300x60.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111-768x153.png 768w\" sizes=\"auto, (max-width: 876px) 100vw, 876px\" \/><\/figure>\n\n\n\n<p>\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"120\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-1024x120.png\" alt=\"\" class=\"wp-image-709\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-1024x120.png 1024w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-300x35.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-768x90.png 768w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222.png 1421w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u751f\u6210\u4e00\u4e2a\u5bc6\u94a5\u5bf9priv.pem\uff0c\u5e76\u628a\u516c\u94a5\u4ece\u5bc6\u94a5\u5bf9\u91cc\u63d0\u53d6\u51fa\u6765<\/p>\n","protected":false},"author":1,"featured_media":548,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[155,154],"tags":[142,282,280,277,278,281,279],"class_list":["post-707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-154","tag-python","tag-rsa","tag-280","tag-277","tag-278","tag-281","tag-279"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=707"}],"version-history":[{"count":2,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":711,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions\/711"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/548"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}