我也看得云里雾里,

但是ECC和RSA并列为非对称加密双雄,

还是很有必要了解一下的。

RSA是用质数分解,ECC是用离散的椭圆方程解,安全度更高。

而且,这个ECC的加法乘法规则,和普通都不一样,

其解是属于一个什么阿贝尔群(一听就知道高级啦)。

百度的文章,下面这个比较详细。

https://www.sohu.com/a/216057858_465483

  1. from hashlib import sha256
  2.  
  3. def sha256d(string):
  4. if not isinstance(string, bytes):
  5. string = string.encode()
  6.  
  7. return sha256(sha256(string).digest()).hexdigest()
  8.  
  9. def inv_mod(b, p):
  10. if b < 0 or p <= b:
  11. b = b % p
  12. c , d = b, p
  13. uc, vc, ud, vd, temp = 1, 0, 0, 1, 0
  14. while c != 0:
  15. temp = c
  16. q, c, d = d // c, d % c, temp
  17. uc, vc, ud, vd = ud - q * uc, vd - q * vc, uc, vc
  18.  
  19. assert d == 1
  20. if ud > 0:
  21. return ud
  22. else:
  23. return ud + p
  24.  
  25. def leftmost_bit(x):
  26. assert x > 0
  27. result = 1
  28. while result <= x:
  29. result = 2 * result
  30. return result // 2
  31.  
  32. print(inv_mod(2, 23))
  33. print(3*inv_mod(1, 23) % 23)
  34.  
  35. def show_points(p, a, b):
  36. return [(x, y) for x in range(p) for y in range(p) if (y*y - (x*x*x + a*x + b)) % p == 0]
  37.  
  38. print(show_points(p=29, a=4, b=20))
  39.  
  40. def double(x, y, p, a, b):
  41. l = ((3 * x * x + a) * inv_mod(2 * y, p)) % p
  42. x3 = (l * l - 2 * x) % p
  43. y3 = (l * (x - x3) - y) % p
  44. return x3, y3
  45.  
  46. print(double(1, 4, p=5, a=2, b=3))
  47.  
  48. def add(x1, y1, x2, y2, p, a, b):
  49. if x1 == x2 and y1 == y2:
  50. return double(x1, y1, p, a, b)
  51. l = ((y2 - y1) * inv_mod(x2 - x1, p)) % p
  52. x3 = (l * l - x1 -x2) % p
  53. y3 = (l * (x1 - x3) - y1) % p
  54. return x3, y3
  55.  
  56. print(add(1, 4, 3, 1, p=5, a=2, b=3))
  57.  
  58. def get_bits(n):
  59. bits = []
  60. while n != 0:
  61. bits.append(n & 1)
  62. n >> 1
  63. return bits
  64.  
  65. class CurveFp(object):
  66.  
  67. def __init__(self, p, a, b):
  68. """ y^2 = x^3 + a*x + b (mod p)."""
  69. self.p = p
  70. self.a = a
  71. self.b = b
  72.  
  73. def contains_point(self, x, y):
  74. return (y * y - (x * x * x + self.a * x + self.b)) % self.p == 0
  75.  
  76. def show_all_points(self):
  77. return [(x, y) for x in range(self.p) for y in range(self.p) if
  78. (y * y - (x * x * x + self.a * x + self.b)) % self.p == 0]
  79.  
  80. def __repr__(self):
  81. return "Curve(p={0:d}, a={1:d}, b={2:d})".format(self.p, self.a, self.b)
  82.  
  83. class Point(object):
  84.  
  85. def __init__(self, curve, x, y, order=None):
  86.  
  87. self.curve = curve
  88. self.x = x
  89. self.y = y
  90. self.order = order
  91. # self.curve is allowed to be None only for INFINITY:
  92. if self.curve:
  93. assert self.curve.contains_point(x, y)
  94. if order:
  95. assert self * order == INFINITY
  96.  
  97. def __eq__(self, other):
  98. """Is this point equals to another"""
  99. if self.curve == other.curve \
  100. and self.x == other.x \
  101. and self.y == other.y:
  102. return True
  103. else:
  104. return False
  105.  
  106. def __add__(self, other):
  107. """Add one point to another point."""
  108.  
  109. if other == INFINITY:
  110. return self
  111. if self == INFINITY:
  112. return other
  113. assert self.curve == other.curve
  114.  
  115. if self.x == other.x:
  116. if (self.y + other.y) % self.curve.p == 0:
  117. return INFINITY
  118. else:
  119. return self.double()
  120.  
  121. p = self.curve.p
  122. l = ((other.y - self.y) * \
  123. inv_mod(other.x - self.x, p)) % p
  124.  
  125. x3 = (l * l - self.x - other.x) % p
  126. y3 = (l * (self.x - x3) - self.y) % p
  127.  
  128. return Point(self.curve, x3, y3)
  129.  
  130. def __mul__(self, other):
  131. e = other
  132. if self.order:
  133. e = e % self.order
  134. if e == 0:
  135. return INFINITY
  136. if self == INFINITY:
  137. return INFINITY
  138.  
  139. e3 = 3 * e
  140. negative_self = Point(self.curve, self.x, -self.y, self.order)
  141. i = leftmost_bit(e3) // 2
  142. result = self
  143.  
  144. while i > 1:
  145. result = result.double()
  146. if (e3 & i) != 0 and (e & i) == 0:
  147. result = result + self
  148. if (e3 & i) == 0 and (e & i) != 0:
  149. result = result + negative_self
  150. i = i // 2
  151. return result
  152.  
  153. def __rmul__(self, other):
  154. """Multiply a point by an integer."""
  155. return self * other
  156.  
  157. def __repr__(self):
  158. if self == INFINITY:
  159. return "infinity"
  160. return "({0},{1})".format(self.x, self.y)
  161.  
  162. def double(self):
  163. """the double point."""
  164. if self == INFINITY:
  165. return INFINITY
  166.  
  167. p = self.curve.p
  168. a = self.curve.a
  169. l = ((3 * self.x * self.x + a) * \
  170. inv_mod(2 * self.y, p)) % p
  171.  
  172. x3 = (l * l - 2 * self.x) % p
  173. y3 = (l * (self.x - x3) - self.y) % p
  174.  
  175. return Point(self.curve, x3, y3)
  176.  
  177. def invert(self):
  178. return Point(self.curve, self.x, -self.y % self.curve.p)
  179.  
  180. INFINITY = Point(None, None, None)
  181.  
  182. p, a, b = 29, 4, 20
  183. curve = CurveFp(p, a, b)
  184. p0 = Point(curve, 3, 1)
  185. print(p0*2)
  186. print(p0*20)

输出:

  1. 12
  2. 3
  3. [(0, 7), (0, 22), (1, 5), (1, 24), (2, 6), (2, 23), (3, 1), (3, 28), (4, 10), (4, 19), (5, 7), (5, 22), (6, 12), (6, 17), (8, 10), (8, 19), (10, 4), (10, 25), (13, 6), (13, 23), (14, 6), (14, 23), (15, 2), (15, 27), (16, 2), (16, 27), (17, 10), (17, 19), (19, 13), (19, 16), (20, 3), (20, 26), (24, 7), (24, 22), (27, 2), (27, 27)]
  4. (3, 1)
  5. (2, 0)
  6. (24,7)
  7. (15,27)

python实现的椭圆曲线加密的更多相关文章

  1. python AES 双向对称加密解密

    高级加密标准(Advanced Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分 ...

  2. python文件的md5加密方法

    本文实例讲述了python文件的md5加密方法.分享给大家供大家参考,具体如下: 一.简单模式: from hashlib import md5 def md5_file(name): m = md5 ...

  3. python实现base64算法加密

    python本身有base64加密的模块,不过是用C写的,封装成了.so文件,无法查看源码,本着学习的心态,自己实现了一遍,算法 原理参考 浅谈Base64编码算法. 代码如下: # coding:u ...

  4. Python爬虫—破解JS加密的Cookie

    前言 在GitHub上维护了一个代理池的项目,代理来源是抓取一些免费的代理发布网站.上午有个小哥告诉我说有个代理抓取接口不能用了,返回状态521.抱着帮人解决问题的心态去跑了一遍代码.发现果真是这样. ...

  5. 小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理)

    小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理) 异常处理(处理) 1.产生异常.raise 异常类(),抛出异常2. 处理异常: try: xxxxx # 尝试执行的代码. ...

  6. python爬虫:了解JS加密爬取网易云音乐

    python爬虫:了解JS加密爬取网易云音乐 前言 大家好,我是"持之以恒_liu",之所以起这个名字,就是希望我自己无论做什么事,只要一开始选择了,那么就要坚持到底,不管结果如何 ...

  7. JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致

    终于有机会生产JAVA的东东了. 有点兴奋. 花了一天搞完.. java(关键key及算法有缩减): package com.security; import javax.crypto.Cipher; ...

  8. 【转】Python爬取AES加密的m3u8视频流的小电影并转换成mp4

    最近发现一个视频网站,准备去爬取得时候,前面很顺利利用fiddler抓包获取网站的post数据loads为python字典数据,分析数据就能发现每个视频的连接地址就在其中, 发现这些都是m3u8文件流 ...

  9. python hashlib模块 md5加密 sha256加密 sha1加密 sha512加密 sha384加密 MD5加盐

      python hashlib模块   hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, ...

随机推荐

  1. Study 4 —— 表单标签

    表单:用于采集浏览者的相关数据.表单标记<form></form>表单的基本语法格式如下: <form action="url" method=&qu ...

  2. C# 与 SQL Server 的数据类型对应关系

    (一)C#与SQL Server 2005(或以下版本): C# C#取值 SQL Server SQL Server取值 System.DateTime samlltime System.Objec ...

  3. Bleve代码阅读(一)——新建索引

    引言 Bleve是Golang实现的一个全文检索库,类似Lucene之于Java.在这里通过阅读其代码,来学习如何使用及定制检索功能.也是为了通过阅读代码,学习在具体环境下Golang的一些使用方式. ...

  4. mysql 原理 ~ binlog

    一 简介:我们会持续对binlog进行分析,但是不深入代码二 版本 5.6    格式    GTID和传统格式    传统格式     一 binlog针对具体事务注意点-1         1 u ...

  5. python - 类的内置 attr 方法

    类的内置 attr 方法 #类的内置 attr 方法: # __getattr__ # __setattr__ # __delattr__ # __getattr__ #到调用一个类不存在数参数时,将 ...

  6. Activity生命周期详解

    http://blog.csdn.net/liuhe688/article/details/6733407 onPause 回到 onResume 的过程“在一般的开发中用不上”,但是作为开发者还是有 ...

  7. 发送http请求的方法

    在http/1.1 协议中,定义了8种发送http请求的方法 get post options head put delete trace connect patch. 根据http协议的设计初衷,不 ...

  8. 对HUAWEI-ManagedProvisioning的一次不完整分析

    分析思路 关注点1:AndroidManifest.xml是Android应用的入口文件,包含有APP服务的权限.广播和启动位置. 关注点2:涉及到修改系统的函数,setWifiEnabled().I ...

  9. GCC的符号可见性——解决多个库同名符号冲突问题

    引用自:https://github.com/wwbmmm/blog/wiki/gcc_visibility 问题 最近项目遇到一些问题,场景如下 主程序依赖了两个库libA的funcA函数和libB ...

  10. 通过全备+binlog_server同步恢复被drop的库或表

    MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步 :伪master+Binlog+同步(本文 ...