Last active
July 4, 2017 08:01
-
-
Save itsecurityco/e013e4f499d05c0cf53d912474194d99 to your computer and use it in GitHub Desktop.
RSA
This file contains hidden or 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
| #!/bin/python | |
| # @itseco | |
| # https://es.wikipedia.org/wiki/RSA | |
| import sys | |
| p1 = 53 | |
| p2 = 59 | |
| n = p1 * p2 | |
| fn = (p1-1) * (p2-1) | |
| e = 3 | |
| message = sys.argv[1] | |
| print "Generate private key for Alice" | |
| d = (2*fn+1) / e | |
| print "d = %d" % d | |
| print "Generate public key for Alice..." | |
| print "n = %d" % n | |
| print "e = %d" % e | |
| def encrypt(message, n, e): | |
| ciphertext = [] | |
| for char in message: | |
| ciphertext.append((ord(char)**e) % n) | |
| return ciphertext | |
| print "Bob sent encrypted message to Alice using Alice's public key..." | |
| ciphertext = encrypt(message, n, e) | |
| print ciphertext | |
| def decrypt(ciphertext, d, n): | |
| message = [] | |
| for char in ciphertext: | |
| message.append(chr((char**d)%n)) | |
| return message | |
| print "Alice decrypt message with his private key..." | |
| print decrypt(ciphertext, d, n) | |
| """ | |
| root@kali:~# python rsa.py "Hello world" | |
| Generate private key for Alice | |
| d = 2011 | |
| Generate public key for Alice... | |
| n = 3127 | |
| e = 3 | |
| Bob sent encrypted message to Alice using Alice's public key... | |
| [1135, 1518, 2658, 2658, 1132, 1498, 2833, 1132, 2473, 2658, 2487] | |
| Alice decrypt message with his private key... | |
| ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment