import itertools
from Cryptodome.Cipher import AES
# noinspection SpellCheckingInspection
def egcd(a, b):
x, y, u, v = 0, 1, 1, 0
gcd = 0
while a != 0:
q, r = b // a, b % a
m, n = x - u * q, y - v * q
b, a, x, y, u, v = a, r, u, v, m, n
gcd = b
return gcd, x, y
def gen_rsa_param(p, q, e):
n = p * q
# phi(n)
phi = (p - 1) * (q - 1)
# modular inverse of e
_, d, _ = egcd(e, phi)
return n, e, d
def long_to_bin(z):
s = hex(z)
s = s.replace('0x', '')
s = s.replace('L', '')
if (len(s) % 2) != 0:
s = '0' + s
return s.decode('hex')
def get_part1():
buf_part1 = ''
for i in itertools.count():
if i >= 1000:
break
key = str(i).zfill(3) + '1314000000000'
buf_enc = '912CA2036A9A0656D17B6B552F157F8E'.decode('hex')
cipher = AES.new(key, AES.MODE_ECB)
buf_plain = cipher.decrypt(buf_enc)
if buf_plain[0:5] == 'pediy':
print('aes_key: %s' % key)
buf_part1 = str(i - 1).zfill(3)
return buf_part1
def get_part2():
p = 208096057845685678782766058500526476379
q = 273086345401562743300402731618892888991
e = 0x3e9
n, e, d = gen_rsa_param(p, q, e)
print('rsa_n: %s' % hex(n))
print('rsa_e: %s' % hex(e))
print('rsa_d: %s' % hex(d))
c = 0x208CBB7CD6ECC64516D07D978F5F0681F534EAD235D5C49ADD72D2DB840D5304
m = long_to_bin(pow(c, d, n))
return m
print('sn: %s%s' % (get_part1(), get_part2()))