Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/RSA-encryption/Intro-Challenges/Challenge-1/encrypt.py
1402 views
1
from Crypto.Util.number import *
2
import gmpy2
3
from secret import flag
4
5
p = getPrime(512)
6
q = getPrime(512)
7
n = p*q
8
e = 65537
9
10
# Euler's Totient function
11
phin = (p-1)*(q-1)
12
assert GCD(e, phin)
13
# Private key exponent
14
d = inverse(e, phin)
15
16
# m is the message/flag
17
m = bytes_to_long(flag)
18
ciphertext = pow(m, e, n)
19
ciphertext = long_to_bytes(ciphertext).encode("hex")
20
print "d: ", d
21
print "ciphertext: ", ciphertext
22
23
24
# Writing private key to privatekey.txt
25
obj1 = open("privatekey.txt",'w')
26
append = "d: " + str(d) + "\n" + "n: " + str(n)
27
obj1.write(append)
28
obj1.close()
29
30
obj1 = open("ciphertext.txt",'w')
31
obj1.write(ciphertext)
32
obj1.close()
33