Created
November 5, 2016 10:09
-
-
Save uzl/2d634b94efd5c7355cf1b2c3d63b666d to your computer and use it in GitHub Desktop.
Documents/research/cryptography/Untitled.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# RSA algorithm implementation in python\n<hr>" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2016-11-05T16:03:02.965000", | |
"end_time": "2016-11-05T16:03:03.158000" | |
}, | |
"collapsed": false, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "from math import sqrt\nfrom random import *\n\n#check if the number is prime or not\ndef is_miller_rabin_prime(n, k=10):\n if n == 2:\n return True\n if not n & 1:\n return False\n\n def check(a, s, d, n):\n x = pow(a, d, n)\n if x == 1:\n return True\n for i in xrange(s - 1):\n if x == n - 1:\n return True\n x = pow(x, 2, n)\n return x == n - 1\n\n s = 0\n d = n - 1\n\n while d % 2 == 0:\n d >>= 1\n s += 1\n\n for i in xrange(k):\n a = randrange(2, n - 1)\n if not check(a, s, d, n):\n return False\n return True\n\n\n\n# ///////////////////////////////////////////\n#generate private key from e and phi\ndef generate_d(e, phi):\n d = 0\n x1 = 0\n x2 = 1\n y1 = 1\n temp_phi = phi\n\n while e > 0:\n temp1 = temp_phi/e\n temp2 = temp_phi - temp1 * e\n temp_phi = e\n e = temp2\n\n x = x2- temp1* x1\n y = d - temp1 * y1\n\n x2 = x1\n x1 = x\n d = y1\n y1 = y\n\n if temp_phi == 1:\n return d + phi\n\n# ////////// get next prime number ////////////\ndef get_nxt_prm(low_lim = 100000000000000000, up_lim = 100000000000000000000000):\n n = randrange(low_lim, up_lim )\n\n if is_miller_rabin_prime(n) == True:\n return n\n while is_miller_rabin_prime(n) == False:\n n += 1\n\n return n\n\n#///////////////////////////////////////////\n\ndef encrypt(pub_key, n, plaintext):\n #Unpack the key into it's components\n\n cipher = []\n\n for c in plaintext:\n assci_val = ord(c)\n tmp_cipher = pow(assci_val, pub_key, n)\n cipher.append(tmp_cipher)\n\n\n return cipher\n\n\ndef decrypt(priv_key, n, ciphertext):\n text = []\n for t in ciphertext:\n tmp_text = pow(t,priv_key, n)\n tmp_c = chr (tmp_text)\n text.append(tmp_c)\n return text\n#///////////////////////////////////////////\n\ndef my_pow(a, e):\n mul = 1\n for i in range(1,e):\n mul *= a\n\n return mul\n\n##################################################################\n##################################################################\n\n\nPRIME_LOW_LIM = 10**6\nPRIME_UP_LIM = 100**9\n\np = get_nxt_prm(PRIME_LOW_LIM, PRIME_UP_LIM)\nq = get_nxt_prm(PRIME_LOW_LIM, PRIME_UP_LIM )\nprint 'generating two prime number ...\\np = ', p, '\\nq = ', q, '\\n'\n\n\nn = p * q\nprint 'calculating (p*q)...\\n n =>', n\n\nphi = (p-1) * (q-1)\nprint '\\ncalculating (p-1)*(q-1)...\\n phi => ', phi, '\\n'\n\ne = get_nxt_prm(2, phi)\nprint '\\ngenerating public key ...\\n e => ', e , ' and length = ', len(str(e))\n\n\nd = generate_d(e, phi)\n\n\n\nprint '\\ngenerating private key ...\\n d => ', d, '; length = ', len(str(d))\n\n#msg = 123\n#print 'Original message', msg\n\n#encrip_msg = pow(msg,e,n)\n#print 'encrypted message: ', encrip_msg\n\n#ttt = pow(msg, e, n)\n#decryp_msg = pow(encrip_msg, d, n)\n#print 'decrypted message: ', decryp_msg\n\nsecret_msg = 'hello how are you'\nprint '\\n\\nOrginal Message: ',secret_msg\n\nprint '\\nEncrypted message: '\naaaa = encrypt(e, n, secret_msg)\nfor a in aaaa:\n print a,\n\n\nprint '\\n\\nDecrypted message: '\nbbbb = decrypt(d,n, aaaa)\nfor b in bbbb:\n print b,\n", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "generating two prime number ...\np = 786709258744600993 \nq = 491849468931253789 \n\ncalculating (p*q)...\n n => 386942531116832324326612782024412477\n\ncalculating (p-1)*(q-1)...\n phi => 386942531116832323048054054348557696 \n\n\ngenerating public key ...\n e => 121327031582590434470174630011335053 and length = 36\n\ngenerating private key ...\n d => 561698314797619019238914984058775109 ; length = 36\n\n\nOrginal Message: hello how are you\n\nEncrypted message: \n14455771632145545850173578267156838 68895843678156386295559241558762639 192733712786940919027828192705696538 192733712786940919027828192705696538 119970030015389214863981240712568988 85899282463559380982929001086032931 14455771632145545850173578267156838 119970030015389214863981240712568988 192269082242455792120532725711217567 85899282463559380982929001086032931 243954766336442801710375055839269382 45384817009482099843900877556762664 68895843678156386295559241558762639 85899282463559380982929001086032931 102074773659448332761057499210785635 119970030015389214863981240712568988 44644789665370318448880445320218878 \n\nDecrypted message: \nh e l l o h o w a r e y o u\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Reference: \n\nhttps://simple.wikipedia.org/wiki/RSA_(algorithm) \nhttp://stackoverflow.com/questions/17298130/working-with-large-primes-in-python\n https://gist.github.com/JonCooperWorks/5314103#file-rsa-py-L92" | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "Python [Root]", | |
"display_name": "Python [Root]", | |
"language": "python" | |
}, | |
"latex_envs": { | |
"current_citInitial": 1, | |
"eqLabelWithNumbers": true, | |
"cite_by": "apalike", | |
"bibliofile": "biblio.bib", | |
"eqNumInitial": 0 | |
}, | |
"nav_menu": {}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"nbconvert_exporter": "python", | |
"name": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12", | |
"file_extension": ".py", | |
"codemirror_mode": { | |
"version": 2, | |
"name": "ipython" | |
} | |
}, | |
"anaconda-cloud": {}, | |
"toc": { | |
"threshold": 6, | |
"number_sections": true, | |
"toc_cell": false, | |
"toc_window_display": false, | |
"toc_section_display": "block", | |
"sideBar": true, | |
"navigate_menu": true | |
}, | |
"gist": { | |
"id": "0a9b1855b44bc020b25e1fc19bc80dec", | |
"data": { | |
"description": "Documents/research/cryptography/Untitled.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/0a9b1855b44bc020b25e1fc19bc80dec" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment