本文整理汇总了Python中cryptography.hazmat.primitives.asymmetric.ec.SECP256K1属性的典型用法代码示例。如果您正苦于以下问题:Python ec.SECP256K1属性的具体用法?Python ec.SECP256K1怎么用?Python ec.SECP256K1使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cryptography.hazmat.primitives.asymmetric.ec
的用法示例。
在下文中一共展示了ec.SECP256K1属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def __init__(self):
super(Encryption_module, self).__init__()
self.cmh_struct_encryption = {
# num : [string to look for, function, server(1) or client(0), return on success, return on failure]
# return value meanings: True - module continues
# False - module thread terminates
# in case of Stateless modules, the whole module terminates if the return value is False
0 : ["XFLT>ECDHd1", self.encryption_step_1, 1, True, False, True],
1 : ["XFLT>ECDHd2", self.encryption_step_2, 0, True, False, False],
2 : ["XFLT>ECDHd3", self.encryption_step_3, 1, True, False, True],
3 : ["XFLT>ECDHd4", self.encryption_step_4, 0, True, False, False],
4 : ["XFLT>ECDHd5", self.encryption_step_5, 1, True, False, True],
}
self.client_step_count = 2
self.server_step_count = 3
self.server_public_key_file = "misc/public_key.pem"
self.server_private_key_file = "misc/private_key.pem"
self.curve = ec.SECP256K1()
return
示例2: hex_to_priv_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def hex_to_priv_key(priv_key_hex, public_key_hex):
priv_key_value = long(priv_key_hex, 16)
public_key = hex_to_key(public_key_hex)
public_numbers = public_key.public_numbers()
private_numbers = ec.EllipticCurvePrivateNumbers(priv_key_value, public_numbers)
priv_key = private_numbers.private_key(backend)
return priv_key
# Hybrid Encryption Scheme:
# - We perform a Elliptic Curves Diffie-Hellman Key Exchange using:
# - SECP256K1 as curve for key generation
# - ANSI X9.63 KDF as Key Derivation Function to derive the shared secret
# - The symmetric cipher is an AES256.MODE_GCM with authentication tag 16-byte
# of length. Since the key is used only once, we can pick the known nonce/iv
# '000000000000' (96 bits of length). We return concatenation of the encoded
# point, the tag and the ciphertext
示例3: encrypt
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def encrypt(message, receiver_public_key):
sender_private_key = ec.generate_private_key(ec.SECP256K1(), backend)
shared_key = sender_private_key.exchange(ec.ECDH(), receiver_public_key)
sender_public_key = sender_private_key.public_key()
point = sender_public_key.public_numbers().encode_point()
iv = '000000000000'
xkdf = x963kdf.X963KDF(
algorithm = hashes.SHA256(),
length = 32,
sharedinfo = '',
backend = backend
)
key = xkdf.derive(shared_key)
encryptor = Cipher(
algorithms.AES(key),
modes.GCM(iv),
backend = backend
).encryptor()
ciphertext = encryptor.update(message) + encryptor.finalize()
return point + encryptor.tag + ciphertext
示例4: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def decrypt(message, receiver_private_key):
point = message[0:65]
tag = message[65:81]
ciphertext = message[81:]
sender_public_numbers = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256K1(), point)
sender_public_key = sender_public_numbers.public_key(backend)
shared_key = receiver_private_key.exchange(ec.ECDH(), sender_public_key)
iv = '000000000000'
xkdf = x963kdf.X963KDF(
algorithm = hashes.SHA256(),
length = 32,
sharedinfo = '',
backend = backend
)
key = xkdf.derive(shared_key)
decryptor = Cipher(
algorithms.AES(key),
modes.GCM(iv,tag),
backend = backend
).decryptor()
message = decryptor.update(ciphertext) + decryptor.finalize()
return message
示例5: sign_digest
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def sign_digest(hash_hex, privkey_hex, hashfunc=hashlib.sha256):
"""
Given a digest and a private key, sign it.
Return the base64-encoded signature
"""
if not isinstance(hash_hex, (str, unicode)):
raise ValueError("hash hex is not a string")
hash_hex = str(hash_hex)
pk_i = decode_privkey_hex(privkey_hex)
privk = ec.derive_private_key(pk_i, ec.SECP256K1(), default_backend())
sig = privk.sign(hash_hex.decode('hex'), ec.ECDSA(utils.Prehashed(hashes.SHA256())))
sig_r, sig_s = decode_dss_signature(sig)
sigb64 = encode_signature(sig_r, sig_s)
return sigb64
示例6: verify_digest
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def verify_digest(hash_hex, pubkey_hex, sigb64, hashfunc=hashlib.sha256):
"""
Given a digest, public key (as hex), and a base64 signature,
verify that the public key signed the digest.
Return True if so
Return False if not
"""
if not isinstance(hash_hex, (str, unicode)):
raise ValueError("hash hex is not a string")
hash_hex = str(hash_hex)
pubk_uncompressed_hex = keylib.key_formatting.decompress(pubkey_hex)
sig_r, sig_s = decode_signature(sigb64)
pubk = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256K1(), pubk_uncompressed_hex.decode('hex')).public_key(default_backend())
signature = encode_dss_signature(sig_r, sig_s)
try:
pubk.verify(signature, hash_hex.decode('hex'), ec.ECDSA(utils.Prehashed(hashes.SHA256())))
return True
except InvalidSignature:
return False
示例7: hex_to_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def hex_to_key(pub_key_hex):
pub_key_hex = pub_key_hex.strip()
pub_key_point = pub_key_hex.decode('hex')
public_numbers = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256K1(), pub_key_point)
public_key = public_numbers.public_key(backend)
return public_key
示例8: _get_curve_by_name
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def _get_curve_by_name(self, name):
if name == 'P-256':
return ec.SECP256R1()
elif name == 'P-384':
return ec.SECP384R1()
elif name == 'P-521':
return ec.SECP521R1()
elif name == 'secp256k1':
return ec.SECP256K1()
elif name in _OKP_CURVES_TABLE:
return name
else:
raise InvalidJWKValue('Unknown Elliptic Curve Type')
示例9: generate_privkey
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def generate_privkey() -> datatypes.PrivateKey:
"""Generate a new SECP256K1 private key and return it"""
privkey = ec.generate_private_key(CURVE, default_backend())
return keys.PrivateKey(
pad32(int_to_big_endian(privkey.private_numbers().private_value))
)
示例10: serialize_private_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def serialize_private_key(cls, private_key: bytes, password=Optional[bytes]) -> bytes:
pri_key = ec.derive_private_key(int.from_bytes(private_key, byteorder="big"),
ec.SECP256K1,
default_backend())
algorithm = \
serialization.BestAvailableEncryption(password) if password is not None else serialization.NoEncryption()
return pri_key.private_bytes(encoding=cls.encoding,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=algorithm)
示例11: serialize_public_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def serialize_public_key(cls, public_key: bytes) -> bytes:
public_numbers = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256K1, public_key)
pub_key = public_numbers.public_key(default_backend())
return pub_key.public_bytes(encoding=cls.encoding,
format=serialization.PublicFormat.SubjectPublicKeyInfo)
示例12: __init__
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def __init__(self, privkey_hex):
"""
Instantiate a signer with a hex-encoded ECDSA private key
"""
pk_i = decode_privkey_hex(privkey_hex)
privk = ec.derive_private_key(pk_i, ec.SECP256K1(), default_backend())
self.signer = privk.signer(ec.ECDSA(hashes.SHA256()))
示例13: public_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def public_key(self):
# lazily calculate and set the public key
if not hasattr(self, '_public_key'):
privk = ec.derive_private_key(int(self._ecdsa_private_key_string.encode('hex'), 16), ec.SECP256K1(), default_backend())
pubk = privk.public_key()
ecdsa_public_key_str = pubk.public_numbers().encode_point().encode('hex')
if self._compressed:
ecdsa_public_key_str = keylib.key_formatting.compress(ecdsa_public_key_str)
self._public_key = _ECPublicKey(ecdsa_public_key_str, version_byte=self._pubkeyhash_version_byte)
# return the public key object
return self._public_key
示例14: generate_privkey
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def generate_privkey() -> datatypes.PrivateKey:
"""Generate a new SECP256K1 private key and return it"""
privkey = ec.generate_private_key(CURVE, default_backend())
return keys.PrivateKey(pad32(int_to_big_endian(privkey.private_numbers().private_value)))
示例15: main
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import ec [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.ec import SECP256K1 [as 别名]
def main():
parser = argparse.ArgumentParser(description='Encrypt messages to Oraclize using Elliptic Curve Integrated Encryption Scheme.')
parser.add_argument('-e', '--encrypt', dest='mode', action='store_const', const='encrypt', help='Encrypt a string. Requires -p')
parser.add_argument('-p', '--with-public-key', dest='public_key', action='store', help='Use the provided hex-encoded public key to encrypt')
parser.add_argument('-d', '--decrypt', dest='mode', action='store_const', const='decrypt', help='Decrypt a string. Provide private key in Wallet Import Format in standard input, or first line of standard input if encrypted text is also provided on standard input. DO NOT PUT YOUR PRIVATE KEY ON THE COMMAND LINE.')
parser.add_argument('-g', '--generate', dest='mode', action='store_const', const='generate', help='Generates a public and a private key')
parser.add_argument('text', nargs='?', action='store', help='String to encrypt, decrypt. If not specified, standard input will be used.')
args = parser.parse_args()
if args.mode != 'encrypt' and args.mode != 'decrypt' and args.mode != 'generate':
parser.print_help()
return
if args.mode == 'encrypt' and not args.public_key:
print "Please, provide a valid public key"
return
if args.mode == 'encrypt':
if args.public_key:
pub_key = hex_to_key(args.public_key)
if args.text:
print base64.b64encode(encrypt(args.text, pub_key))
return
else:
print base64.b64encode(encrypt(sys.stdin.read(), pub_key))
return
elif args.mode == 'decrypt':
if args.text:
print "Insert your public key"
public_key = sys.stdin.read()
print "Insert your private key:"
private_key = sys.stdin.read()
private_key = hex_to_priv_key(private_key, public_key)
text = base64.b64decode(args.text)
else:
print "Insert your public key"
public_key = sys.stdin.read()
print "\nInsert your private key:"
private_key = sys.stdin.read()
private_key = hex_to_priv_key(private_key, public_key)
print "\nInsert encrypted text"
text = base64.b64decode(sys.stdin.read())
print decrypt(text, private_key)
if args.mode == 'generate':
receiver_private_key = ec.generate_private_key(ec.SECP256K1(), backend)
receiver_public_key = receiver_private_key.public_key()
number = receiver_private_key.private_numbers()
print "Public Key:", receiver_public_key.public_numbers().encode_point().encode('hex')
print "Private Key:", hex(number.private_value)