前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【愚公系列】2021年12月 攻防世界-简单题-CRYPTO-012(easy_ECC)

【愚公系列】2021年12月 攻防世界-简单题-CRYPTO-012(easy_ECC)

作者头像
愚公搬代码
发布2021-12-27 08:16:04
3660
发布2021-12-27 08:16:04
举报
文章被收录于专栏:历史专栏

文章目录


前言

题目描述:转眼两个人又走到了下一个谜题的地方,这又是一种经典的密码学加密方式 而你刚好没有这个的工具,你对小鱼说“小鱼我知道数学真的很重要了,有了工具只是方便我们使用 懂了原理才能做到,小鱼你教我一下这个缇努怎么做吧!”在小鱼的一步步带领下,你终于明白了ECC 的基本原理,成功的解开了这个题目,两个人相视一笑,快步走向了下一个题目所在的位置。flag格式为cyberpeace{x+y的值}


提示:以下是本篇文章正文内容,下面案例可供参考

一、easy_ECC

题目链接:https://adworld.xctf.org.cn/task/answer?type=crypto&number=5&grade=0&id=5109&page=1

二、使用步骤

1.下载附件

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
已知椭圆曲线加密Ep(a,b)参数为

p = 15424654874903

a = 16546484

b = 4548674875

G(6478678675,5636379357093)

私钥为

k = 546768

求公钥K(x,y)

2.解密脚本

代码语言:javascript
复制
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))
在这里插入图片描述
在这里插入图片描述

得到:(0xcb19fe553fa, 0x50545408eb4)

再16进制转10进制可得 进制转换网址:http://www.hiencode.com/jinzhi.html

在这里插入图片描述
在这里插入图片描述

得到:13957031351290

在这里插入图片描述
在这里插入图片描述

得到:5520194834100

flag为:cyberpeace{19477226185390}


总结

  • 椭圆曲线加密
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/12/26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
  • 一、easy_ECC
  • 二、使用步骤
    • 1.下载附件
      • 2.解密脚本
      • 总结
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档