{"id":204839,"date":"2025-05-29T15:18:04","date_gmt":"2025-05-29T07:18:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204839\/"},"modified":"2025-05-29T15:18:04","modified_gmt":"2025-05-29T07:18:04","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-python-rsa-%e5%8a%a0%e5%af%86%e4%bb%a3%e7%a0%81%e8%bd%ac%e6%8d%a2%e4%b8%ba-c-%e4%bb%a3%e7%a0%81%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204839\/","title":{"rendered":"\u5982\u4f55\u5c06 Python RSA \u52a0\u5bc6\u4ee3\u7801\u8f6c\u6362\u4e3a C# \u4ee3\u7801\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06 Python RSA \u52a0\u5bc6\u4ee3\u7801\u8f6c\u6362\u4e3a C# \u4ee3\u7801\uff1f<\/h1>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/17318338556739afff2d6bc.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python rsa \u52a0\u5bc6\u8f6c\u6362 c# \u4ee3\u7801<\/strong><\/p>\n<p>\u672c\u6587\u65e8\u5728\u63d0\u4f9b\u4e00\u6bb5 python rsa \u52a0\u5bc6\u4ee3\u7801\u5230 c# \u7684\u8f6c\u6362\u793a\u4f8b\u3002<\/p>\n<p>python \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>from rsa import key, common, transform, core, pkcs1\n\ndef get_rsa_encrypt(rsa_key, txt):\n    pub_key = key.publickey(int(rsa_key, 16), 65537)\n    key_length = common.byte_size(pub_key.n)\n    padded = pkcs1._pad_for_encryption(txt, key_length)\n    payload = transform.bytes2int(padded)\n    encrypted = core.encrypt_int(payload, pub_key.e, pub_key.n)\n    encode_str = hex(encrypted)[2:-1]\n    if len(encode_str) % 2 != 0:\n        encode_str = '0{}'.format(encode_str)\n    return encode_str<\/pre>\n<p><strong>c# \u4ee3\u7801\u8f6c\u6362<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4e3a python \u4ee3\u7801\u8f6c\u6362\u4e3a c# \u7684\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/\u516c\u94a5\u5927\u7d20\u6570\n            BigInteger biE = BigInteger.Parse(rsa_key, System.Globalization.NumberStyles.HexNumber);\n            \/\/\u5927\u6574\u6570N\n            BigInteger biN = BigInteger.Parse(\"10001\", System.Globalization.NumberStyles.HexNumber);\n\n\n            byte[] publicKeyByte2 = biE.ToByteArray();\n            byte[] exponentByte2 = biN.ToByteArray();\n\n            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())\n            {\n                RSAParameters RSAKeyInfo = new RSAParameters();\n                UTF8Encoding ByteConverter = new UTF8Encoding();\n                RSAKeyInfo.Modulus = publicKeyByte2.Reverse().ToArray();\n                RSAKeyInfo.Exponent = exponentByte2.Reverse().ToArray();\n                RSA.ImportParameters(RSAKeyInfo);\n                byte[] passwordByte = ByteConverter.GetBytes(txt);\n                var encryptReuslt = RSA.Encrypt(passwordByte, RSAEncryptionPadding.Pkcs1);\n                \/\/var Ciphertext = Convert.ToBase64String(encryptReuslt);\n                encrypted = BitConverter.ToString(encryptReuslt).Replace(\"-\", \"\");\n                \/\/encrypted = GetHexFromChs(Ciphertext);\n            }<\/pre>\n<p>\u6b64 c# \u4ee3\u7801\u5c06\u751f\u6210\u4e0e python \u4ee3\u7801\u76f8\u540c\u683c\u5f0f\u7684\u5341\u516d\u8fdb\u5236\u52a0\u5bc6\u7ed3\u679c\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5c06 Python RSA \u52a0\u5bc6\u4ee3\u7801\u8f6c\u6362\u4e3a C# \u4ee3\u7801\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 Python RSA \u52a0&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204839","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204839"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204839\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}