{"id":2042,"date":"2019-05-22T21:44:31","date_gmt":"2019-05-22T12:44:31","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=2042"},"modified":"2020-02-08T17:44:57","modified_gmt":"2020-02-08T08:44:57","slug":"rsa%e6%9a%97%e5%8f%b7%e3%81%a8%e9%9b%bb%e5%ad%90%e7%bd%b2%e5%90%8d","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/2042","title":{"rendered":"RSA\u6697\u53f7\u3068\u96fb\u5b50\u7f72\u540d"},"content":{"rendered":"<h2>RSA<\/h2>\n<h3>RSA\u3068\u306f<\/h3>\n<p>RSA\u3068\u306f\u6697\u53f7\u5316\u65b9\u5f0f\u306e\u4e00\u3064\u3067\u3059\u3002\u516c\u958b\u9375\u6697\u53f7\u306e\u4e00\u7a2e\u3067\u6587\u66f8\u5185\u5bb9\u3092\u79d8\u533f\u3059\u308b\u305f\u3081\u306e\u6697\u53f7\u5316\u3068\u96fb\u5b50\u7f72\u540d\u3092\u884c\u3046\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u3053\u306e\u30da\u30fc\u30b8\u3067\u306fPython\u4e0a\u3067\u306eRSA\u306e\u9375\u751f\u6210\u304b\u3089\u53d6\u6271\u65b9\u6cd5\u3001\u6697\u53f7\u5316\u3068\u5fa9\u53f7\u5316\u3001\u96fb\u5b50\u7f72\u540d\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<h3>pycryptodome<\/h3>\n<p>\u6697\u53f7\u5316\u7528\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u8272\u3005\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u30da\u30fc\u30b8\u3067\u306fpycryptodome\u3092\u4f7f\u7528\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>Python\u3067\u306fpycrypto\u3068\u3044\u3046\u6697\u53f7\u5316\u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u304c\u4ee5\u524d\u306f\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001Python3.3\u304c\u51fa\u305f\u9803\u4ee5\u964d\u304b\u3089\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3055\u308c\u306a\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u305f\u3081\u3001\u8fd1\u5e74\u306f\u4ee3\u66ff\u3068\u3057\u3066pycryptodome\u3068\u3044\u3046\u3082\u306e\u304c\u3088\u304f\u4f7f\u308f\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u3068\u3044\u3046\u308f\u3051\u3067RSA\u4ee5\u5916\u306bAES\u3001DES\u306a\u3069\u306e\u6697\u53f7\u5316\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3084SHA256\u306e\u3088\u3046\u306a\u30cf\u30c3\u30b7\u30e5\u3092\u6271\u3046\u3082\u306e\u304c\u63c3\u3063\u3066\u3044\u307e\u3059\u3002pycrypto\u306e\u4ee3\u66ff\u3092\u8b33\u3063\u3066\u3044\u308b\u305f\u3081pycrypto\u3092\u5229\u7528\u3057\u305f\u30b3\u30fc\u30c9\u306f\u6982\u306d\u305d\u306e\u307e\u307e\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u53ef\u80fd\u306a\u306f\u305a\u3060\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30db\u30fc\u30eb\u3068\u306a\u308a\u3046\u308b\u53e4\u3044\u6a5f\u80fd\u306f\u4e00\u90e8\u5ec3\u6b62\u3055\u308c\u305f\u305f\u3081\u3001\u6697\u53f7\u5316\u3084\u7f72\u540d\u306e\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u3044\u65b9\u304c\u5927\u304d\u304f\u5909\u308f\u3063\u3066\u3044\u307e\u3059\u3002\u8a73\u3057\u304f\u306f\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a href=\"https:\/\/www.pycryptodome.org\/en\/latest\/\">https:\/\/www.pycryptodome.org\/en\/latest\/<\/a><\/p>\n<p>pypi\u306b\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001pip\u3067\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install pycryptodome\r\n<\/pre>\n<h2>\u9375\u306e\u751f\u6210<\/h2>\n<p>\u307e\u305a\u3001\u6697\u53f7\u5316\u306e\u305f\u3081\u306e\u9375\u306e\u30da\u30a2\u3092\u751f\u6210\u3057\u3066pem\u5f62\u5f0f\u3067\u4fdd\u5b58\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002Unix\u7cfb\u306eOS\u3092\u5229\u7528\u3055\u308c\u3066\u3044\u308b\u65b9\u306f\u901a\u5e38\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u4e0a\u3067\u884c\u3046\u3053\u3068\u304c\u591a\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u3053\u308c\u304b\u3089\u7d39\u4ecb\u3059\u308bPython\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3068\u540c\u7b49\u3067\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# \u79d8\u5bc6\u9375\u306e\u751f\u6210\r\nopenssl genrsa 1024 &gt; private-key.pem\r\n# \u79d8\u5bc6\u9375\u306b\u57fa\u3065\u3044\u3066\u516c\u958b\u9375\u3092\u751f\u6210\r\nopenssl rsa -in private-key.pem -pubout -out public-key.pem\r\n<\/pre>\n<p>\u305d\u308c\u3067\u306fPython\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom Crypto.PublicKey import RSA\r\n\r\n# \u79d8\u5bc6\u9375\u306e\u751f\u6210\r\nprivate_key = RSA.generate(1024)\r\nwith open('private.pem', 'w') as f:\r\n    f.write(private_key.export_key().decode('utf-8'))\r\n\r\n# \u79d8\u5bc6\u9375\u306b\u57fa\u3065\u3044\u3066\u516c\u958b\u9375\u3092\u751f\u6210\r\npublic_key = private_key.publickey()\r\nwith open('public.pem', 'w') as f:\r\n    f.write(public_key.export_key().decode('utf-8'))\r\n<\/pre>\n<p>\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306fRSA.generate\u3067\u79d8\u5bc6\u9375\u3092\u751f\u6210\u3001private_key.publickey()\u3067\u516c\u958b\u9375\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001pem\u5f62\u5f0f\u3067\u4fdd\u5b58\u3059\u308b\u5834\u5408\u306fexport_key()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>\u4e00\u65b9\u3067pem\u5f62\u5f0f\u3092\u8aad\u307f\u8fbc\u3093\u3067\u5143\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u623b\u3059\u306b\u306fRSA.import_key\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# pem\u30ed\u30fc\u30c9\r\nwith open('private.pem', 'br') as f:\r\n    private_pem = f.read()\r\n    private_key = RSA.import_key(private_pem)\r\n\r\nwith open('public.pem', 'br') as f:\r\n    public_pem = f.read()\r\n    public_key = RSA.import_key(public_pem)\r\n\r\n<\/pre>\n<p>&nbsp;<br \/>\n\u203b \u4ee5\u964d\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u9375\u306e\u751f\u6210\u3082\u3057\u304f\u306f\u8aad\u307f\u8fbc\u307f\u306e\u8a18\u8ff0\u306f\u7701\u7565\u3057\u3001\u3044\u304d\u306a\u308apublic_key\u3001private_key\u3068\u8868\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u6697\u53f7\u5316\u3068\u5fa9\u53f7\u5316<\/h2>\n<p>\u6b21\u306b\u751f\u6210\u3057\u305f\u516c\u958b\u9375\u3067\u30c6\u30ad\u30b9\u30c8\u3092\u6697\u53f7\u5316\u3057\u3001\u79d8\u5bc6\u9375\u3067\u5fa9\u53f7\u5316\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom Crypto.PublicKey import RSA\r\nfrom Crypto.Cipher import PKCS1_OAEP\r\n\r\n# \u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u6697\u53f7\u5316\r\nmessage = &quot;\u30c6\u30b9\u30c8\u30e1\u30c3\u30bb\u30fc\u30b8&quot;\r\npublic_cipher = PKCS1_OAEP.new(public_key)\r\nciphertext = public_cipher.encrypt(message.encode())\r\n\r\n# \u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u5fa9\u53f7\r\nprivate_cipher = PKCS1_OAEP.new(private_key)\r\nmessage2 = private_cipher.decrypt(ciphertext).decode(&quot;utf-8&quot;)\r\n\r\n<\/pre>\n<p>\u6697\u53f7\u5316\u3067\u306fencrypt\u3001\u5fa9\u53f7\u3059\u308b\u5834\u5408\u306fdecrypt\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<h2>\u96fb\u5b50\u7f72\u540d<\/h2>\n<p>\u6b21\u306b\u96fb\u5b50\u7f72\u540d\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u30c7\u30fc\u30bf\u9001\u4fe1\u5074\u306e\u672c\u4eba\u78ba\u8a8d\u3084\u9001\u4fe1\u30c7\u30fc\u30bf\u304c\u6539\u3056\u3093\u3055\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u51e6\u7406\u3067\u4f7f\u7528\u3055\u308c\u308b\u6280\u8853\u3067\u3001\u6709\u540d\u6240\u3067\u306fSSL\u3067\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001\u6c7a\u6e08\u306a\u3069\u304c\u95a2\u308f\u308bAPI\u3067\u547c\u3073\u51fa\u3057\u5143\u306e\u59a5\u5f53\u6027\u691c\u8a3c\u306a\u3069\u306b\u5229\u7528\u3055\u308c\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u79d8\u5bc6\u9375\u3067\u7f72\u540d\u3092\u4f5c\u6210\u3057\u3001\u516c\u958b\u9375\u3067\u7f72\u540d\u306e\u59a5\u5f53\u6027\u3092\u691c\u8a3c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom Crypto.PublicKey import RSA\r\nfrom Crypto.Hash import SHA256\r\nfrom Crypto.Signature import pkcs1_15\r\n\r\n# \u30e1\u30c3\u30bb\u30fc\u30b8\u3068\u79d8\u5bc6\u9375\u304b\u3089\u7f72\u540d\u3092\u751f\u6210\r\nmessage = '\u30c6\u30b9\u30c8\u30e1\u30c3\u30bb\u30fc\u30b8'\r\nh1 = SHA256.new(message.encode())\r\nsignature = pkcs1_15.new(private_key).sign(h1)\r\n\r\n# \u516c\u958b\u9375\u304b\u3089\u59a5\u5f53\u6027\u3092\u691c\u8a3c\r\nh2 = SHA256.new(message.encode())\r\ntry:\r\n    pkcs1_15.new(public_key).verify(h2, signature)\r\n    verified = True\r\nexcept ValueError:\r\n    verified = False\r\n\r\nprint(verified)\r\n\r\n<\/pre>\n<p>verify\u30e1\u30bd\u30c3\u30c9\u3067\u4e0d\u5099\u304c\u3042\u308b\u5834\u5408\u306fValueError\u304c\u767a\u751f\u3057\u307e\u3059\u3002\u3053\u308c\u3092\u5229\u7528\u3057\u3066\u4e0a\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306fValueError\u304c\u767a\u751f\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u306f\u8a8d\u8a3c\u6210\u529f\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RSA RSA\u3068\u306f RSA\u3068\u306f\u6697\u53f7\u5316\u65b9\u5f0f\u306e\u4e00\u3064\u3067\u3059\u3002\u516c\u958b\u9375\u6697\u53f7\u306e\u4e00\u7a2e\u3067\u6587\u66f8\u5185\u5bb9\u3092\u79d8\u533f\u3059\u308b\u305f\u3081\u306e\u6697\u53f7\u5316\u3068\u96fb\u5b50\u7f72\u540d\u3092\u884c\u3046\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u3053\u306e\u30da\u30fc\u30b8\u3067\u306fPython\u4e0a\u3067\u306eRSA\u306e\u9375\u751f\u6210\u304b\u3089\u53d6\u6271\u65b9\u6cd5\u3001\u6697\u53f7\u5316\u3068\u5fa9\u53f7\u5316\u3001\u96fb\u5b50\u7f72\u540d\u306b\u3064...<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[181,182,183],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/2042"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=2042"}],"version-history":[{"count":14,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/2042\/revisions"}],"predecessor-version":[{"id":2065,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/2042\/revisions\/2065"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=2042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=2042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=2042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}