python实现的椭圆曲线加密
我也看得云里雾里,
但是ECC和RSA并列为非对称加密双雄,
还是很有必要了解一下的。
RSA是用质数分解,ECC是用离散的椭圆方程解,安全度更高。
而且,这个ECC的加法乘法规则,和普通都不一样,
其解是属于一个什么阿贝尔群(一听就知道高级啦)。
百度的文章,下面这个比较详细。
https://www.sohu.com/a/216057858_465483
from hashlib import sha256
def sha256d(string):
if not isinstance(string, bytes):
string = string.encode()
return sha256(sha256(string).digest()).hexdigest()
def inv_mod(b, p):
if b < 0 or p <= b:
b = b % p
c , d = b, p
uc, vc, ud, vd, temp = 1, 0, 0, 1, 0
while c != 0:
temp = c
q, c, d = d // c, d % c, temp
uc, vc, ud, vd = ud - q * uc, vd - q * vc, uc, vc
assert d == 1
if ud > 0:
return ud
else:
return ud + p
def leftmost_bit(x):
assert x > 0
result = 1
while result <= x:
result = 2 * result
return result // 2
print(inv_mod(2, 23))
print(3*inv_mod(1, 23) % 23)
def show_points(p, a, b):
return [(x, y) for x in range(p) for y in range(p) if (y*y - (x*x*x + a*x + b)) % p == 0]
print(show_points(p=29, a=4, b=20))
def double(x, y, p, a, b):
l = ((3 * x * x + a) * inv_mod(2 * y, p)) % p
x3 = (l * l - 2 * x) % p
y3 = (l * (x - x3) - y) % p
return x3, y3
print(double(1, 4, p=5, a=2, b=3))
def add(x1, y1, x2, y2, p, a, b):
if x1 == x2 and y1 == y2:
return double(x1, y1, p, a, b)
l = ((y2 - y1) * inv_mod(x2 - x1, p)) % p
x3 = (l * l - x1 -x2) % p
y3 = (l * (x1 - x3) - y1) % p
return x3, y3
print(add(1, 4, 3, 1, p=5, a=2, b=3))
def get_bits(n):
bits = []
while n != 0:
bits.append(n & 1)
n >> 1
return bits
class CurveFp(object):
def __init__(self, p, a, b):
""" y^2 = x^3 + a*x + b (mod p)."""
self.p = p
self.a = a
self.b = b
def contains_point(self, x, y):
return (y * y - (x * x * x + self.a * x + self.b)) % self.p == 0
def show_all_points(self):
return [(x, y) for x in range(self.p) for y in range(self.p) if
(y * y - (x * x * x + self.a * x + self.b)) % self.p == 0]
def __repr__(self):
return "Curve(p={0:d}, a={1:d}, b={2:d})".format(self.p, self.a, self.b)
class Point(object):
def __init__(self, curve, x, y, order=None):
self.curve = curve
self.x = x
self.y = y
self.order = order
# self.curve is allowed to be None only for INFINITY:
if self.curve:
assert self.curve.contains_point(x, y)
if order:
assert self * order == INFINITY
def __eq__(self, other):
"""Is this point equals to another"""
if self.curve == other.curve \
and self.x == other.x \
and self.y == other.y:
return True
else:
return False
def __add__(self, other):
"""Add one point to another point."""
if other == INFINITY:
return self
if self == INFINITY:
return other
assert self.curve == other.curve
if self.x == other.x:
if (self.y + other.y) % self.curve.p == 0:
return INFINITY
else:
return self.double()
p = self.curve.p
l = ((other.y - self.y) * \
inv_mod(other.x - self.x, p)) % p
x3 = (l * l - self.x - other.x) % p
y3 = (l * (self.x - x3) - self.y) % p
return Point(self.curve, x3, y3)
def __mul__(self, other):
e = other
if self.order:
e = e % self.order
if e == 0:
return INFINITY
if self == INFINITY:
return INFINITY
e3 = 3 * e
negative_self = Point(self.curve, self.x, -self.y, self.order)
i = leftmost_bit(e3) // 2
result = self
while i > 1:
result = result.double()
if (e3 & i) != 0 and (e & i) == 0:
result = result + self
if (e3 & i) == 0 and (e & i) != 0:
result = result + negative_self
i = i // 2
return result
def __rmul__(self, other):
"""Multiply a point by an integer."""
return self * other
def __repr__(self):
if self == INFINITY:
return "infinity"
return "({0},{1})".format(self.x, self.y)
def double(self):
"""the double point."""
if self == INFINITY:
return INFINITY
p = self.curve.p
a = self.curve.a
l = ((3 * self.x * self.x + a) * \
inv_mod(2 * self.y, p)) % p
x3 = (l * l - 2 * self.x) % p
y3 = (l * (self.x - x3) - self.y) % p
return Point(self.curve, x3, y3)
def invert(self):
return Point(self.curve, self.x, -self.y % self.curve.p)
INFINITY = Point(None, None, None)
p, a, b = 29, 4, 20
curve = CurveFp(p, a, b)
p0 = Point(curve, 3, 1)
print(p0*2)
print(p0*20)
输出:
12 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)] (3, 1) (2, 0) (24,7) (15,27)
python实现的椭圆曲线加密的更多相关文章
- python AES 双向对称加密解密
高级加密标准(Advanced Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分 ...
- python文件的md5加密方法
本文实例讲述了python文件的md5加密方法.分享给大家供大家参考,具体如下: 一.简单模式: from hashlib import md5 def md5_file(name): m = md5 ...
- python实现base64算法加密
python本身有base64加密的模块,不过是用C写的,封装成了.so文件,无法查看源码,本着学习的心态,自己实现了一遍,算法 原理参考 浅谈Base64编码算法. 代码如下: # coding:u ...
- Python爬虫—破解JS加密的Cookie
前言 在GitHub上维护了一个代理池的项目,代理来源是抓取一些免费的代理发布网站.上午有个小哥告诉我说有个代理抓取接口不能用了,返回状态521.抱着帮人解决问题的心态去跑了一遍代码.发现果真是这样. ...
- 小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理)
小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理) 异常处理(处理) 1.产生异常.raise 异常类(),抛出异常2. 处理异常: try: xxxxx # 尝试执行的代码. ...
- python爬虫:了解JS加密爬取网易云音乐
python爬虫:了解JS加密爬取网易云音乐 前言 大家好,我是"持之以恒_liu",之所以起这个名字,就是希望我自己无论做什么事,只要一开始选择了,那么就要坚持到底,不管结果如何 ...
- JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致
终于有机会生产JAVA的东东了. 有点兴奋. 花了一天搞完.. java(关键key及算法有缩减): package com.security; import javax.crypto.Cipher; ...
- 【转】Python爬取AES加密的m3u8视频流的小电影并转换成mp4
最近发现一个视频网站,准备去爬取得时候,前面很顺利利用fiddler抓包获取网站的post数据loads为python字典数据,分析数据就能发现每个视频的连接地址就在其中, 发现这些都是m3u8文件流 ...
- python hashlib模块 md5加密 sha256加密 sha1加密 sha512加密 sha384加密 MD5加盐
python hashlib模块 hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, ...
随机推荐
- 51Nod-1436 方程的解数
题目链接 题解链接 版权属于以上链接 #include <iostream> #define mod(a, m) ((a) % (m) + (m)) % (m) using namesp ...
- 枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)
//Uva725 #include <iostream> #include <cstring> #include <cstdlib> #include <cs ...
- python -- 题目不看别人的自己写然后比较
题目一 ''' 编写Python脚本,分析xx.log文件,按域名统计访问次数倒序输出 xx.log文件内容如下: https://www.sogo.com/ale.html https://www. ...
- JavaScript 数字转汉字+element时间选择器快速选择
window.CN = { : '一', : '二', : '三', : '四', : '五', : '六', : '七', : '八', : '九', : '零' } window.LEVEL = ...
- CopyFromScreen在屏幕缩放情况下需要做处理
using System; using System.Drawing; using System.Runtime.InteropServices; //这段代码转自网上 namespace Syste ...
- springboot项目使用idea开启远程调试
远程调试是调试服务器的有效手段,远程服务器运行的应用可以在本地代码中打断点调试,能让开发人员准确定位服务器上的问题. 一.开启远程调试前提:本地代码与服务器代码一致, 二.开启远程调试步骤 1.开发工 ...
- ROS学习笔记(二) # ROS NodeHandles
1. 自动启动和关闭 ros::NodeHandle nh: 这段代码执行之后,如果内部节点还没有启动,ros::NodeHandle 会启动这个节点:一旦所有的 ros::NodeHandle 实例 ...
- 【vim】保存文件没有权限 :w !sudo tee %
每当你打开一个你没有写入权限的文件(比如系统配置文件)并做了一些修改,Vim 无法通过普通的 ":w" 命令来保存. 你不需要重新以 root 方式打开文件再进行修改,只需要运行: ...
- java并发编程系列四、AQS-AbstractQueuedSynchronizer
什么是AbstractQueuedSynchronizer?为什么我们要分析它? AQS:抽象队列同步器,原理是:当多个线程去获取锁的时候,如果获取锁失败了,当前线程就会被打包成一个node节点放入 ...
- HTML学习笔记03-HTML基础
<!DOCTYPE HTML> <html> <head> <title> </title> </head> <body& ...