-
-
Save mj2266/ee7adde29d2a849c0b6aa751e39750e7 to your computer and use it in GitHub Desktop.
''' | |
620031587 | |
Net-Centric Computing Assignment | |
Part A - RSA Encryption | |
''' | |
from __future__ import division | |
import random | |
''' | |
Euclid's algorithm for determining the greatest common divisor | |
Use iteration to make it faster for larger integers | |
''' | |
def gcd(a, b): | |
while b != 0: | |
a, b = b, a % b | |
return a | |
''' | |
d can be calculated using formula : d = (phi*i + 1)/e | |
where i is integer starting from 1 | |
and we find integer value for d iteratively | |
''' | |
def multiplicative_inverse(e, phi): | |
d = None | |
i = 1 | |
exit = False | |
while not exit: | |
temp1 = phi*i +1 | |
d = float(temp1/e) | |
d_int = int(d) | |
i += 1 | |
if(d_int == d): | |
exit=True | |
return int(d) | |
''' | |
Tests to see if a number is prime. | |
''' | |
def is_prime(num): | |
if num == 2: | |
return True | |
if num < 2 or num % 2 == 0: | |
return False | |
for n in xrange(3, int(num**0.5)+2, 2): | |
if num % n == 0: | |
return False | |
return True | |
def generate_keypair(p, q): | |
if not (is_prime(p) and is_prime(q)): | |
raise ValueError('Both numbers must be prime.') | |
elif p == q: | |
raise ValueError('p and q cannot be equal') | |
#n = pq | |
n = p * q | |
#Phi is the totient of n | |
phi = (p-1) * (q-1) | |
#Choose an integer e such that e and phi(n) are coprime | |
e = random.randrange(1, phi) | |
#Use Euclid's Algorithm to verify that e and phi(n) are comprime | |
g = gcd(e, phi) | |
while g != 1: | |
e = random.randrange(1, phi) | |
g = gcd(e, phi) | |
#Use Extended Euclid's Algorithm to generate the private key | |
d = multiplicative_inverse(e, phi) | |
#Return public and private keypair | |
#Public key is (e, n) and private key is (d, n) | |
return ((e, n), (d, n)) | |
def encrypt(pk, plaintext): | |
#Unpack the key into it's components | |
key, n = pk | |
#Convert each letter in the plaintext to numbers based on the character using a^b mod m | |
cipher = [(ord(char) ** key) % n for char in plaintext] | |
#Return the array of bytes | |
return cipher | |
def decrypt(pk, ciphertext): | |
#Unpack the key into its components | |
key, n = pk | |
#Generate the plaintext based on the ciphertext and key using a^b mod m | |
plain = [chr((char ** key) % n) for char in ciphertext] | |
#Return the array of bytes as a string | |
return ''.join(plain) | |
if __name__ == '__main__': | |
''' | |
Detect if the script is being run directly by the user | |
''' | |
print "RSA Encrypter/ Decrypter" | |
p = int(raw_input("Enter a prime number (17, 19, 23, etc): ")) | |
q = int(raw_input("Enter another prime number (Not one you entered above): ")) | |
print "Generating your public/private keypairs now . . ." | |
public, private = generate_keypair(p, q) | |
print "Your public key is ", public ," and your private key is ", private | |
message = raw_input("Enter a message to encrypt with your private key: ") | |
encrypted_msg = encrypt(private, message) | |
print "Your encrypted message is: " | |
print ''.join(map(lambda x: str(x), encrypted_msg)) | |
print "Decrypting message with public key ", public ," . . ." | |
print "Your message is:" | |
print decrypt(public, encrypted_msg) |
Thank you for your code.
This is SEVERELY broken
https://www.0xkasper.com/articles/hacktm-ctf-2020-qualifiers-write-up#rsa-is-easy-1
DO NOT USE FOR ANYTHING!
can you please rewrite the program to generate the prime number automatically without enter by user thank you, and another thing how i can add the value of public key into the encrypts message?
thank you for your code
Do you still need the prime number automatic generator? @balasmeh
This is SEVERELY broken
https://www.0xkasper.com/articles/hacktm-ctf-2020-qualifiers-write-up#rsa-is-easy-1
DO NOT USE FOR ANYTHING!
This wasnt my code, i forked it from someone and fixed few issues. any way let me know what the issues are please @stackola thanks
you are welcome @Bharghav-Baddam
@mj2266 yes please I'm interested of it I would be thankful for you
Also please I'm looking for another help from you I want to make it as client server, where client send hi and server response
@balasmeh you can contact me here https://www.linkedin.com/in/manjyotsingh-nanra-6a4999152/
does this one work on python3. Thanks
can you please rewrite the program to generate the prime number automatically without enter by user thank you, and another thing how i can add the value of public key into the encrypts message?
thank you for your code