{"id":276,"date":"2022-12-24T20:29:31","date_gmt":"2022-12-24T12:29:31","guid":{"rendered":"http:\/\/139.224.63.49\/?p=276"},"modified":"2022-12-24T20:30:16","modified_gmt":"2022-12-24T12:30:16","slug":"rsa%e7%ae%97%e6%b3%95%e5%a4%8d%e4%b9%a0","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=276","title":{"rendered":"RSA\u7b97\u6cd5\u590d\u4e60"},"content":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\u7b14\u8bd5\uff0c\u9047\u5230\u4e00\u9053\u9898\u3002\u7ed9\u5b9a\u4e86RSA\u7684\u516c\u94a5<code class=\"katex-inline\">(N,E)<\/code>,\u95ee\u7ed9\u5bc6\u6587<code class=\"katex-inline\">x=3<\/code>\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\u4e3a\uff1f<\/p>\n<p>\u7531\u4e8e\u592a\u4e45\u6ca1\u6709\u505aCTF\u5bc6\u7801\u9898\u4e86\uff0c\u770b\u5230\u8fd9\u9053\u9898\uff0c\u6211\u611f\u89c9\u964c\u751f\u6709\u719f\u6089\u3002<code class=\"katex-inline\">N<\/code>\u548c<code class=\"katex-inline\">E<\/code>\u4ee3\u8868\u5565\u6211\u65e9\u5df2\u5fd8\u8bb0\uff0c\u6700\u540e\u6211\u53ea\u80fd\u6839\u636e\u4ee5\u524d\u7684\u5370\u8c61\u81ea\u5df1\u731c\u6d4b\u4e86\u4e00\u4e2a\u7b54\u6848\u5199\u4e86\u4e0a\u53bb\u3002<\/p>\n<p>\u56e0\u6b64\u524d\u51e0\u5929\u5c31\u628aRSA\u7b97\u6cd5\u53c8\u91cd\u65b0\u590d\u4e60\u4e86\u4e00\u8fb9\uff0c\u5e76\u4e14\u8fd9\u6b21\u81ea\u5df1\u7528python\u628a\u7b97\u6cd5\u7b80\u5355\u6a21\u62df\u4e86\u4e00\u4e0b\uff0c\u52a0\u6df1\u5370\u8c61\u3002<\/p>\n<p>\u672c\u6587\u53ea\u662f\u81ea\u5df1\u590d\u4e60RSA\u52a0\u89e3\u5bc6\u7684\u5927\u81f4\u8fc7\u7a0b\uff0c\u4e3b\u8981\u53c2\u8003\u7f51\u4e0a\u7684\u4e00\u4e9b\u6587\u7ae0\uff0c\u4e0d\u4fdd\u8bc1\u6587\u7ae0\u6b63\u786e\uff0c\u5f88\u53ef\u80fd\u6709\u9519\u8bef\u3002\u8bf7\u4e0d\u8981\u968f\u4fbf\u53c2\u8003\u6211\u7684\u7406\u89e3\u54c8\u3002\u672c\u6587\u4e0d\u6d89\u53ca\u4efb\u4f55\u6570\u5b66\u8bc1\u660e\uff08\u56e0\u4e3a\u6211\u6570\u8bba\u57fa\u672c\u5565\u90fd\u4e0d\u4f1a\uff09<\/p>\n<h4>\u7b97\u6cd5\u7406\u89e3<\/h4>\n<h5>\u53c2\u6570\u751f\u6210<\/h5>\n<p>RSA\u7684\u5b89\u5168\u6027\u57fa\u4e8e\u5927\u6570\u8d28\u56e0\u6570\u5206\u89e3\u7684\u56f0\u96be\u6027\u3002<\/p>\n<p>\u8981\u6267\u884cRSA\u7b97\u6cd5\uff0c\u9996\u5148\u8981\u968f\u673a\u751f\u6210\u4e24\u4e2a\u5927\u8d28\u6570\uff0c\u8bb0\u4e3a<code class=\"katex-inline\">p<\/code>\u548c<code class=\"katex-inline\">q<\/code><\/p>\n<p>\uff08\u5177\u4f53\u600e\u4e48\u751f\u6210\u7684\u53ef\u80fd\u6709\u4e00\u4e9b\u7b97\u6cd5\u5427\uff0c\u6211\u5bf9\u6b64\u4e0d\u4e86\u89e3\uff0c\u53ea\u77e5\u9053\u53ef\u4ee5\u7528\u968f\u5373\u5927\u6570+\u7d20\u6027\u5224\u5b9a\u6765\u5b9e\u73b0\uff0c\u4f46\u8fd9\u6837\u5b50\u6548\u7387\u592a\u4f4e\uff0c\u5b9e\u9645\u73af\u5883\u4e2d\u5e94\u8be5\u4e0d\u4f1a\u8fd9\u4e48\u5f04\uff09<\/p>\n<pre><code class=\"language-katex\">n=pq\n\\\\\nm = (p-1)(q-1) = \\phi(n)<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684<code class=\"katex-inline\">\\phi<\/code>\u8868\u793a\u7684\u662f\u6b27\u62c9\u51fd\u6570<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u8981\u5bfb\u627e\u4e00\u7ec4\u4e92\u4e3a\u4e58\u6cd5\u9006\u5143\u7684\u6570<code class=\"katex-inline\">e<\/code>\u548c<code class=\"katex-inline\">d<\/code>\uff0c\u6240\u8c13\u4e58\u6cd5\u9006\u5143\u5c31\u662f\u6307\u8981\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6<\/p>\n<pre><code class=\"language-katex\">ed=1(mod \\ m)<\/code><\/pre>\n<p>\u5f53\u7136\u8fd9\u91cc\u6570<code class=\"katex-inline\">e<\/code>\u548c<code class=\"katex-inline\">d<\/code>\u90fd\u8981\u548c<code class=\"katex-inline\">m<\/code>\u4e92\u8d28\uff0c\u4e0d\u7136\u4e0d\u5b58\u5728\u4e58\u6cd5\u9006\u5143<\/p>\n<p>\u5f53\u7136\u7684\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u6bd4\u5982\u4e3a1\u7b49\u80af\u5b9a\u4e0d\u884c<\/p>\n<p>\u8fd9\u91cc\u7684<code class=\"katex-inline\">e<\/code>\u5c31\u662f\u6211\u4eec\u63a5\u4e0b\u6765\u52a0\u5bc6\u8981\u7528\u5230\u7684\u5bc6\u94a5\uff0c<code class=\"katex-inline\">d<\/code>\u5c31\u662f\u89e3\u5bc6\u8981\u7528\u7684\u516c\u94a5<\/p>\n<p>\u5f53\u7136\u56e0\u4e3a<code class=\"katex-inline\">e<\/code>\u548c<code class=\"katex-inline\">d<\/code>\u4e92\u4e3a\u4e58\u6cd5\u9006\u5143\uff0c\u6240\u4ee5\u516c\u94a5\u548c\u5bc6\u94a5\u5176\u5b9e\u4ea4\u6362\u4e86\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u5728\u6570\u5b66\u4e0a\u662f\u5bf9\u79f0\u7684<\/p>\n<p>\u8fd9\u91cc\u8981\u83b7\u5f97\u4e58\u6cd5\u9006\u5143\uff0c\u6211\u9009\u62e9\u7684\u662f\u91c7\u7528\u62d3\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\uff0c\u4e0d\u77e5\u9053\u5b9e\u9645\u5bc6\u7801\u5b66\u5e93\u91cc\u5b9e\u73b0\u662f\u7528\u7684\u4ec0\u4e48\u7b97\u6cd5<\/p>\n<h5>\u52a0\u89e3\u5bc6\u8ba1\u7b97<\/h5>\n<p>\u8981\u52a0\u5bc6\u7684\u4fe1\u606f\u4e3a<code class=\"katex-inline\">x<\/code><\/p>\n<p>\u516c\u94a5\u4e3a<code class=\"katex-inline\">(n,e)<\/code><\/p>\n<p><code class=\"katex-inline\">enc<\/code>\u4e3a\u52a0\u5bc6\u540e\u7684\u5bc6\u6587<\/p>\n<p><code class=\"katex-inline\">dec<\/code>\u4e3a\u89e3\u5bc6\u540e\u5f97\u5230\u7684\u4fe1\u606f<\/p>\n<h6>\u52a0\u5bc6\u8fc7\u7a0b<\/h6>\n<pre><code class=\"language-katex\">enc = x^e \\ mod \\ n<\/code><\/pre>\n<h6>\u89e3\u5bc6\u8fc7\u7a0b<\/h6>\n<pre><code class=\"language-katex\">dec = enc^d \\ mod \\ n<\/code><\/pre>\n<p>\u7b97\u6cd5\u4fdd\u8bc1<code class=\"katex-inline\">x<\/code>\u548c<code class=\"katex-inline\">dec<\/code>\u4e00\u5b9a\u662f\u76f8\u540c\u7684<\/p>\n<p>\u8bc1\u660e\u8fc7\u7a0b\u6211\u4e5f\u4e0d\u592a\u61c2\uff0c\u9700\u8981\u57fa\u7840\u6570\u8bba\u77e5\u8bc6<\/p>\n<h4>\u7b97\u6cd5Python\u5b9e\u73b0<\/h4>\n<pre><code class=\"language-python\">import random\n\n#\u62d3\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\n#\u8f93\u5165a,b\n#\u8fd4\u56der,rx,ry\n#r\u4e3aa\u548cb\u7684\u6700\u5927\u516c\u56e0\u6570\n#\u82e5r==1,\u6709rx*a+ry*b=1\ndef extgcd(a,b):\n    if b==0:\n        return a,1,0\n    r,x,y = extgcd(b,a%b)\n    rx = y \n    ry = x-a\/\/b*y\n    return r,rx,ry\n\ndef gete(m):\n    while True:\n        x = random.randint(2,m-1)\n        a,_,_ = extgcd(x,m)\n        if a==1:\n            return x\n\ndef getd(m,e):\n    a,x,y = extgcd(e,m)\n    assert a==1\n    if x&lt;0:\n        x += (((-x)\/\/m)+1)*m \n    return x\n\np = 31\nq = 29\nn = p*q\nm = (p-1)*(q-1)\ne = gete(m)\nd = getd(m,e)\nprint(&quot;m={}&quot;.format(m))\nprint(&#039;public key is (n={},e={})&#039;.format(n,e))\nprint(&#039;secret key is (n={},d={})&#039;.format(n,d))\nassert (e*d)%m==1\n\ndef encrept(s):\n    s = [ord(c) for c in s]\n    # print(&#039;step 1:&#039;,s)\n    s = [(item**e)%n for item in s]\n    # print(&#039;step 2:&#039;,s)\n    return s\n\ndef decrept(s):\n    # print(&#039;step 3:&#039;,s)\n    s = [(item**d)%n for item in s]\n    # print(&#039;step 4:&#039;,s)\n    s = [chr(item) for item in s]\n    return &#039;&#039;.join(s)\n\ntest_str = &#039;hello world&#039;\nencrept_str = encrept(test_str)\ndecrept_str = decrept(encrept_str)\nprint(&#039;test str:&#039;,test_str)\nprint(&#039;encrepted str:&#039;,encrept_str)\nprint(&#039;decrepted str:&#039;,decrept_str)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\u7b14\u8bd5\uff0c\u9047\u5230\u4e00\u9053\u9898\u3002\u7ed9\u5b9a\u4e86RSA\u7684\u516c\u94a5(N,E),\u95ee\u7ed9\u5bc6\u6587x=3\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\u4e3a\uff1f \u7531\u4e8e\u592a\u4e45\u6ca1\u6709&hellip; <a href=\"http:\/\/iamnear.top\/?p=276\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">RSA\u7b97\u6cd5\u590d\u4e60<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-276","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-uncategorized","7":"h-entry","9":"h-as-article"},"_links":{"self":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=276"}],"version-history":[{"count":2,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":278,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions\/278"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}