{"id":4399,"date":"2021-08-03T10:53:07","date_gmt":"2021-08-03T10:53:07","guid":{"rendered":"https:\/\/www.bearloga.space\/?page_id=4399"},"modified":"2021-08-03T10:53:07","modified_gmt":"2021-08-03T10:53:07","slug":"sozdanie-klyuchej-rsa","status":"publish","type":"page","link":"https:\/\/www.bearloga.space\/en\/sozdanie-klyuchej-rsa\/","title":{"rendered":"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0435\u0439 RSA"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u0412 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u043d\u0430 \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 RSA \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u043b\u044e\u0447\u0435\u0439 RSA<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0448\u0430\u0433\u0438 \u0443\u0447\u0430\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0439 RSA \u2014<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0434\u0432\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0447\u0438\u0441\u043b\u0430, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e&nbsp;<strong>p<\/strong>&nbsp;\u0438&nbsp;<strong>q<\/strong>&nbsp;.&nbsp;\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0447\u0438\u0441\u0435\u043b \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f&nbsp;<strong>n<\/strong>&nbsp;, \u0433\u0434\u0435&nbsp;<strong>n = p * q<\/strong><\/li><li>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0441&nbsp;<strong>(p-1)<\/strong>&nbsp;\u0438&nbsp;<strong>(q-1).<\/strong>&nbsp;\u041f\u0443\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f&nbsp;<strong>e<\/strong>&nbsp;.<\/li><li>\u0420\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 e.&nbsp;\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f&nbsp;<strong>d<\/strong>&nbsp;.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0434\u0432\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0447\u0438\u0441\u043b\u0430, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e&nbsp;<strong>p<\/strong>&nbsp;\u0438&nbsp;<strong>q<\/strong>&nbsp;.&nbsp;\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0447\u0438\u0441\u0435\u043b \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f&nbsp;<strong>n<\/strong>&nbsp;, \u0433\u0434\u0435&nbsp;<strong>n = p * q<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0441&nbsp;<strong>(p-1)<\/strong>&nbsp;\u0438&nbsp;<strong>(q-1).<\/strong>&nbsp;\u041f\u0443\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f&nbsp;<strong>e<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0420\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 e.\u00a0\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f\u00a0<strong>d<\/strong>\u00a0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0439 RSA<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u044b \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0439 RSA \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c&nbsp;<strong>\u043c\u043e\u0434\u0443\u043b\u044f<\/strong>&nbsp;Python \u2014&nbsp;<strong>\u043c\u043e\u0434\u0443\u043b\u044c Cryptomath \u0438 \u043c\u043e\u0434\u0443\u043b\u044c<\/strong>&nbsp;<strong>\u0420\u0430\u0431\u0438\u043d\u0430 \u041c\u0438\u043b\u043b\u0435\u0440\u0430<\/strong>&nbsp;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u041c\u043e\u0434\u0443\u043b\u044c \u041a\u0440\u0438\u043f\u0442\u043e\u043c\u0430\u0442\u0430<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043c\u043e\u0434\u0443\u043b\u044f cryptomath, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u0441\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 RSA, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def gcd(a, b):\n   while a != 0:\n      a, b = b % a, a\n   return b\n\ndef findModInverse(a, m):\n   if gcd(a, m) != 1:\n      return None\n   u1, u2, u3 = 1, 0, a\n   v1, v2, v3 = 0, 1, m\n   \n   while v3 != 0:\n      q = u3 \/\/ v3\n         v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3\n   return u1 % m<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u041c\u043e\u0434\u0443\u043b\u044c \u0420\u0430\u0431\u0438\u043d\u041c\u0438\u043b\u043b\u0435\u0440<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043c\u043e\u0434\u0443\u043b\u044f RabinMiller, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u0441\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 RSA, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import random\ndef rabinMiller(num):\n   s = num - 1\n   t = 0\n   \n   while s % 2 == 0:\n      s = s \/\/ 2\n      t += 1\n   for trials in range(5):\n      a = random.randrange(2, num - 1)\n      v = pow(a, s, num)\n      if v != 1:\n         i = 0\n         while v != (num - 1):\n            if i == t - 1:\n               return False\n            else:\n               i = i + 1\n               v = (v ** 2) % num\n      return True\ndef isPrime(num):\n   if (num 7&lt; 2):\n      return False\n   lowPrimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, \n   67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, \n   157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, \n   251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,317, 331, 337, 347, 349, \n   353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, \n   457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, \n   571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, \n   673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, \n   797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, \n   911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n\t\n   if num in lowPrimes:\n      return True\n   for prime in lowPrimes:\n      if (num % prime == 0):\n         return False\n   return rabinMiller(num)\ndef generateLargePrime(keysize = 1024):\n   while True:\n      num = random.randrange(2**(keysize-1), 2**(keysize))\n      if isPrime(num):\n         return num<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0439 RSA \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import random, sys, os, rabinMiller, cryptomath\n\ndef main():\n   makeKeyFiles('RSA_demo', 1024)\n\ndef generateKey(keySize):\n   # Step 1: Create two prime numbers, p and q. Calculate n = p * q.\n   print('Generating p prime...')\n   p = rabinMiller.generateLargePrime(keySize)\n   print('Generating q prime...')\n   q = rabinMiller.generateLargePrime(keySize)\n   n = p * q\n\t\n   # Step 2: Create a number e that is relatively prime to (p-1)*(q-1).\n   print('Generating e that is relatively prime to (p-1)*(q-1)...')\n   while True:\n      e = random.randrange(2 ** (keySize - 1), 2 ** (keySize))\n      if cryptomath.gcd(e, (p - 1) * (q - 1)) == 1:\n         break\n   \n   # Step 3: Calculate d, the mod inverse of e.\n   print('Calculating d that is mod inverse of e...')\n   d = cryptomath.findModInverse(e, (p - 1) * (q - 1))\n   publicKey = (n, e)\n   privateKey = (n, d)\n   print('Public key:', publicKey)\n   print('Private key:', privateKey)\n   return (publicKey, privateKey)\n\ndef makeKeyFiles(name, keySize):\n   # Creates two files 'x_pubkey.txt' and 'x_privkey.txt' \n      (where x is the value in name) with the the n,e and d,e integers written in them,\n   # delimited by a comma.\n   if os.path.exists('%s_pubkey.txt' % (name)) or os.path.exists('%s_privkey.txt' % (name)):\n      sys.exit('WARNING: The file %s_pubkey.txt or %s_privkey.txt already exists! Use a different name or delete these files and re-run this program.' % (name, name))\n   publicKey, privateKey = generateKey(keySize)\n   print()\n   print('The public key is a %s and a %s digit number.' % (len(str(publicKey[0])), len(str(publicKey[1])))) \n   print('Writing public key to file %s_pubkey.txt...' % (name))\n   \n   fo = open('%s_pubkey.txt' % (name), 'w')\n\tfo.write('%s,%s,%s' % (keySize, publicKey[0], publicKey[1]))\n   fo.close()\n   print()\n   print('The private key is a %s and a %s digit number.' % (len(str(publicKey[0])), len(str(publicKey[1]))))\n   print('Writing private key to file %s_privkey.txt...' % (name))\n   \n   fo = open('%s_privkey.txt' % (name), 'w')\n   fo.write('%s,%s,%s' % (keySize, privateKey[0], privateKey[1]))\n   fo.close()\n# If makeRsaKeys.py is run (instead of imported as a module) call\n# the main() function.\nif __name__ == '__main__':\n   main()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u043d\u0430 \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 RSA \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Python. \u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u043b\u044e\u0447\u0435\u0439 RSA \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0448\u0430\u0433\u0438 \u0443\u0447\u0430\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[140],"tags":[],"class_list":["post-4399","page","type-page","status-publish","hentry","category-kriptografiya-2"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages\/4399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/comments?post=4399"}],"version-history":[{"count":0,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages\/4399\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/media?parent=4399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/categories?post=4399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/tags?post=4399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}