defmodular_exponentiation(base, exp, mod): """ 快速模幂运算,计算 (base^exp) % mod 使用指数的二进制展开方法以加速大整数运算。 """ result = 1 base = base % mod # 确保 base 小于 mod while exp > 0: if exp % 2 == 1: # 如果 exp 是奇数 result = (result * base) % mod exp = exp >> 1# exp //= 2 base = (base * base) % mod # base 的平方 return result
defrsa_keygen(p, q, e): """ 给定质数 p 和 q 以及公钥指数 e,生成 RSA 密钥对。 :param p: 质数 p :param q: 质数 q :param e: 公钥指数 e :return: (n, e, d) 三元组,分别是模数 n,公钥指数 e 和私钥指数 d """ # 计算模数 n n = p * q # 计算欧拉函数 φ(n) phi = (p - 1) * (q - 1) # 计算私钥指数 d d = mod_inverse(e, phi) return n, e, d
defextended_gcd(a, b): """ 扩展欧几里得算法,计算 a 和 b 的最大公约数,并找到 a 的模逆元。 返回 gcd, x, y,其中 gcd 是 a 和 b 的最大公约数,x 和 y 满足 gcd = a * x + b * y。 """ if a == 0: return b, 0, 1 gcd, x1, y1 = extended_gcd(b % a, a) x = y1 - (b // a) * x1 y = x1 return gcd, x, y
defmod_inverse(e, phi): """ 计算 e 关于 φ(n) 的模逆元 d,使得 (d * e) % phi == 1。 """ gcd, x, y = extended_gcd(e, phi) if gcd != 1: raise ValueError("e 和 φ(n) 不是互质的,无法计算模逆元") else: return x % phi
if __name__ == "__main__": # 给定的质数 p 和 q p = 106697219132480173106064317148705638676529121742557567770857687729397446898790451577487723991083173010242416863238099716044775658681981821407922722052778958942891831033512463262741053961681512908218003840408526915629689432111480588966800949428079015682624591636010678691927285321708935076221951173426894836169 q = 144819424465842307806353672547344125290716753535239658417883828941232509622838692761917211806963011168822281666033695157426515864265527046213326145174398018859056439431422867957079149967592078894410082695714160599647180947207504108618794637872261572262805565517756922288320779308895819726074229154002310375209 e = 65537
classMyMath: @staticmethod defswap(a, i, j): a[i], a[j] = a[j], a[i]
classGoodCard: @staticmethod defanything(s): card = list(s) to = len(card) - 1 GoodCard.perfect(card, 0, to, (to + 1) // 2) return''.join(card)
@staticmethod defcircle(a, start, i, n2): k = (i * 2) % n2 while k != i: temp = a[i + start] a[i + start] = a[k + start] a[k + start] = temp k = (k * 2) % n2
@staticmethod defperfect(a, start, end, n): if start >= end: return if start == end - 1: MyMath.swap(a, start, end) return
k = 0 p = n * 2 + 1 k_3 = 1 while k_3 <= p // 3: k += 1 k_3 *= 3
m = (k_3 - 1) // 2 GoodCard.right_circle(a, start + m, start + n + m - 1, m) i = 0 t = 1 while i < k: GoodCard.circle(a, start - 1, t, m * 2 + 1) t *= 3 i += 1
@staticmethod defreverse(a, start, end): while start < end: MyMath.swap(a, start, end) start += 1 end -= 1
@staticmethod defright_circle(a, start, end, n): m = n % (end - start + 1) GoodCard.reverse(a, end - m + 1, end) GoodCard.reverse(a, start, end - m) GoodCard.reverse(a, start, end)
# Example usage result = GoodCard.anything("abcdefghijklm")
table = "abcdefghijklm" flag = "ywts7humj5h6g"#121 119 116 115 55 104 117 109 106 53 104 54 103 for i inrange(len(flag)): print(flag[result.index(table[i])],end="")