python椭圆曲线加密算法_椭圆曲线算法(ECC)学习(一)

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import sys

import ctypes

import ctypes.util

OpenSSL = None

class CipherName:

def __init__(self, name, pointer, blocksize):

self._name = name

self._pointer = pointer

self._blocksize = blocksize

def __str__(self):

return "Cipher : " + self._name + " | Blocksize : " + str(self._blocksize) + " | Fonction pointer : " + str(self._pointer)

def get_pointer(self):

return self._pointer()

def get_name(self):

return self._name

def get_blocksize(self):

return self._blocksize

class _OpenSSL:

"""

Wrapper for OpenSSL using ctypes

"""

def __init__(self, library):

"""

Build the wrapper

"""

self._lib = ctypes.CDLL(library)

self.pointer = ctypes.pointer

self.c_int = ctypes.c_int

self.byref = ctypes.byref

self.create_string_buffer = ctypes.create_string_buffer

self.BN_new = self._lib.BN_new

self.BN_new.restype = ctypes.c_void_p

self.BN_new.argtypes = []

self.BN_free = self._lib.BN_free

self.BN_free.restype = None

self.BN_free.argtypes = [ctypes.c_void_p]

self.BN_num_bits = self._lib.BN_num_bits

self.BN_num_bits.restype = ctypes.c_int

self.BN_num_bits.argtypes = [ctypes.c_void_p]

self.BN_bn2bin = self._lib.BN_bn2bin

self.BN_bn2bin.restype = ctypes.c_int

self.BN_bn2bin.argtypes = [ctypes.c_void_p, ctypes.c_void_p]

self.BN_bin2bn = self._lib.BN_bin2bn

self.BN_bin2bn.restype = ctypes.c_void_p

self.BN_bin2bn.argtypes = [ctypes.c_void_p, ctypes.c_int,

ctypes.c_void_p]

self.EC_KEY_free = self._lib.EC_KEY_free

self.EC_KEY_free.restype = None

self.EC_KEY_free.argtypes = [ctypes.c_void_p]

self.EC_KEY_new_by_curve_name = self._lib.EC_KEY_new_by_curve_name

self.EC_KEY_new_by_curve_name.restype = ctypes.c_void_p

self.EC_KEY_new_by_curve_name.argtypes = [ctypes.c_int]

self.EC_KEY_generate_key = self._lib.EC_KEY_generate_key

self.EC_KEY_generate_key.restype = ctypes.c_int

self.EC_KEY_generate_key.argtypes = [ctypes.c_void_p]

self.EC_KEY_check_key = self._lib.EC_KEY_check_key

self.EC_KEY_check_key.restype = ctypes.c_int

self.EC_KEY_check_key.argtypes = [ctypes.c_void_p]

self.EC_KEY_get0_private_key = self._lib.EC_KEY_get0_private_key

self.EC_KEY_get0_private_key.restype = ctypes.c_void_p

self.EC_KEY_get0_private_key.argtypes = [ctypes.c_void_p]

self.EC_KEY_get0_public_key = self._lib.EC_KEY_get0_public_key

self.EC_KEY_get0_public_key.restype = ctypes.c_void_p

self.EC_KEY_get0_public_key.argtypes = [ctypes.c_void_p]

self.EC_KEY_get0_group = self._lib.EC_KEY_get0_group

self.EC_

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECC(Elliptic Curve Cryptography)椭圆加密算法是一种基于椭圆曲线数学问题的公钥加密算法。下面是一个简单的代码实现示例: ```python # 导入所需的库 from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives import serialization # 生成密钥对 private_key = ec.generate_private_key(ec.SECP256R1()) public_key = private_key.public_key() # 将密钥对序列化为字节串 private_key_bytes = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) public_key_bytes = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) # 加密和解密示例 message = b"Hello, ECC!" ciphertext = public_key.encrypt(message, ec.ECIES()) plaintext = private_key.decrypt(ciphertext, ec.ECIES()) # 打印结果 print("原始消息:", message) print("加密后的消息:", ciphertext) print("解密后的消息:", plaintext) ``` 上述代码使用了Python中的`cryptography`库来实现ECC椭圆加密算法。首先,通过`ec.generate_private_key()`生成一个私钥对象,然后通过私钥对象获取对应的公钥对象。接下来,使用`private_key.private_bytes()`和`public_key.public_bytes()`将密钥对序列化为字节串。最后,使用公钥对象的`encrypt()`方法对消息进行加密,使用私钥对象的`decrypt()`方法对密文进行解密。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值