# 密文转化为指定格式 s = 'AAAAABAABBBAABBAAAAAAAABAABABAAAAAAABBABAAABBAAABBAABAAAABABAABAAABBABAAABAAABAABABBAABBBABAAABABABBAAABBABAAABAABAABAAAABBABBAABBAABAABAAABAABAABAABABAABBABAAAABBABAABBA' a = s.lower()
# base64解密一下 b = base64.b64decode(s).decode('ascii') # 对解密后的字符串进行处理 b = b.strip('&#;') c = [] c = b.split(';&#') # unicode解密 d = '' for i in c: d += chr(int(i)) # base64再次解密 e = base64.b64decode(d).decode('ascii') # 对字符进行处理 e = e.strip('/') f = [] f = e.split('/') # 转化为ascii码 flag ='' for i in f: flag += chr(int(i)) print('flag is ',flag)
def encode1(ans): s = '' for i in ans: x = ord(i) ^ 36 x = x + 25 s += chr(x) return s def encode2(ans): s = '' for i in ans: x = ord(i) + 36 x = x ^ 36 s += chr(x) return s def encode3(ans): return base64.b32encode(ans) flag = ' ' print 'Please Input your flag:' flag = raw_input() final = 'UC7KOWVXWVNKNIC2XCXKHKK2W5NLBKNOUOSK3LNNVWW3E===' if encode3(encode2(encode1(flag))) == final: print 'correct' else: print 'wrong'
分析代码,输入的flag需要进行三次加密(??),想求答案,写段代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import base64
key = 'UC7KOWVXWVNKNIC2XCXKHKK2W5NLBKNOUOSK3LNNVWW3E===' temp = base64.b32decode(key) #可得temp为'\xa0\xbe\xa7Z\xb7\xb5Z\xa6\xa0Z\xb8\xae\xa3\xa9Z\xb7Z\xb0\xa9\xae\xa3\xa4\xad\xad\xad\xad\xad\xb2' #手动赋值进行接下来的解密 b = "\xa0\xbe\xa7Z\xb7\xb5Z\xa6\xa0Z\xb8\xae\xa3\xa9Z\xb7Z\xb0\xa9\xae\xa3\xa4\xad\xad\xad\xad\xad\xb2" s = '' for i in b: s += chr((ord(i) ^ 36) - 36) l = '' for i in s: l += chr((ord(i) - 25) ^ 36) print ('flag is ',l)
得到flag :cyberpeace{interestinghhhhh}
0x11 幂数加密
下载下来分析后,式云影密码
1 2 3 4
8842101220480224404014224202480122 88421 122 48 2244 4 142242 248 122 23 5 12 12 4 15 14 5 w e l l d o n e
就这样,以0为分界,每组加起来,去对应顺序字母
贴一段大佬的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/user/bin/env python # -*-coding:utf-8 -*-
a = open(r'crypto11.txt','r') ciphertext = a.read()
s = ciphertext.split('0')
flag = '' for i in range(len(s)): list = [] for j in s[i]: list.append(j) b = 0 for k in list: b += int(k) # 字母ascii值与字母顺序相差为96 flag += chr(b+96) print('flag is ',flag)
def get_inverse(mu, p): """ 获取y的负元 """ for i in range(1, p): if (i*mu)%p == 1: return i return -1
def get_gcd(zi, mu): """ 获取最大公约数 """ if mu: return get_gcd(mu, zi%mu) else: return zi
def get_np(x1, y1, x2, y2, a, p): """ 获取n*p,每次+p,直到求解阶数np=-p """ flag = 1 # 定义符号位(+/-)
# 如果 p=q k=(3x2+a)/2y1mod p if x1 == x2 and y1 == y2: zi = 3 * (x1 ** 2) + a # 计算分子 【求导】 mu = 2 * y1 # 计算分母
# 若P≠Q,则k=(y2-y1)/(x2-x1) mod p else: zi = y2 - y1 mu = x2 - x1 if zi* mu < 0: flag = 0 # 符号0为-(负数) zi = abs(zi) mu = abs(mu)
# 将分子和分母化为最简 gcd_value = get_gcd(zi, mu) # 最大公約數 zi = zi // gcd_value # 整除 mu = mu // gcd_value # 求分母的逆元 逆元: ∀a ∈G ,ョb∈G 使得 ab = ba = e # P(x,y)的负元是 (x,-y mod p)= (x,p-y) ,有P+(-P)= O∞ inverse_value = get_inverse(mu, p) k = (zi * inverse_value)
if flag == 0: # 斜率负数 flag==0 k = -k k = k % p # 计算x3,y3 P+Q x3 = (k ** 2 - x1 - x2) % p y3 = (k * (x1 - x3) - y1) % p return x3,y3
def get_rank(x0, y0, a, b, p): """ 获取椭圆曲线的阶 """ x1 = x0 #-p的x坐标 y1 = (-1*y0)%p #-p的y坐标 tempX = x0 tempY = y0 n = 1 while True: n += 1 # 求p+q的和,得到n*p,直到求出阶 p_x,p_y = get_np(tempX, tempY, x0, y0, a, p) # 如果 == -p,那么阶数+1,返回 if p_x == x1 and p_y == y1: return n+1 tempX = p_x tempY = p_y
def get_param(x0, a, b, p): """ 计算p与-p """ y0 = -1 for i in range(p): # 满足取模约束条件,椭圆曲线Ep(a,b),p为质数,x,y∈[0,p-1] if i**2%p == (x0**3 + a*x0 + b)%p: y0 = i break
import collections import random EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h') curve = EllipticCurve( 'secp256k1', # Field characteristic. p=int(input('p=')), # Curve coefficients. a=int(input('a=')), b=int(input('b=')), # Base point. g=(int(input('Gx=')), int(input('Gy='))), # Subgroup order. n=int(input('k=')), # Subgroup cofactor. h=1, ) # Modular arithmetic ########################################################## def inverse_mod(k, p): """Returns the inverse of k modulo p. This function returns the only integer x such that (x * k) % p == 1. k must be non-zero and p must be a prime. """ if k == 0: raise ZeroDivisionError('division by zero') if k < 0: # k ** -1 = p - (-k) ** -1 (mod p) return p - inverse_mod(-k, p) # Extended Euclidean algorithm. s, old_s = 0, 1 t, old_t = 1, 0 r, old_r = p, k while r != 0: quotient = old_r // r old_r, r = r, old_r - quotient * r old_s, s = s, old_s - quotient * s old_t, t = t, old_t - quotient * t gcd, x, y = old_r, old_s, old_t assert gcd == 1 assert (k * x) % p == 1 return x % p # Functions that work on curve points ######################################### def is_on_curve(point): """Returns True if the given point lies on the elliptic curve.""" if point is None: # None represents the point at infinity. return True x, y = point return (y * y - x * x * x - curve.a * x - curve.b) % curve.p == 0 def point_neg(point): """Returns -point.""" assert is_on_curve(point) if point is None: # -0 = 0 return None x, y = point result = (x, -y % curve.p) assert is_on_curve(result) return result def point_add(point1, point2): """Returns the result of point1 + point2 according to the group law.""" assert is_on_curve(point1) assert is_on_curve(point2) if point1 is None: # 0 + point2 = point2 return point2 if point2 is None: # point1 + 0 = point1 return point1 x1, y1 = point1 x2, y2 = point2 if x1 == x2 and y1 != y2: # point1 + (-point1) = 0 return None if x1 == x2: # This is the case point1 == point2. m = (3 * x1 * x1 + curve.a) * inverse_mod(2 * y1, curve.p) else: # This is the case point1 != point2. m = (y1 - y2) * inverse_mod(x1 - x2, curve.p) x3 = m * m - x1 - x2 y3 = y1 + m * (x3 - x1) result = (x3 % curve.p, -y3 % curve.p) assert is_on_curve(result) return result def scalar_mult(k, point): """Returns k * point computed using the double and point_add algorithm.""" assert is_on_curve(point) if k < 0: # k * point = -k * (-point) return scalar_mult(-k, point_neg(point)) result = None addend = point while k: if k & 1: # Add. result = point_add(result, addend) # Double. addend = point_add(addend, addend) k >>= 1 assert is_on_curve(result) return result # Keypair generation and ECDHE ################################################ def make_keypair(): """Generates a random private-public key pair.""" private_key = curve.n public_key = scalar_mult(private_key, curve.g) return private_key, public_key private_key, public_key = make_keypair() print("private key:", hex(private_key)) print("public key: (0x{:x}, 0x{:x})".format(*public_key))