主页 > 冷钱包imtoken > 比特币密码学 非对称密码学如何应用于区块链或比特币?
比特币密码学 非对称密码学如何应用于区块链或比特币?
区块链如何应用非对称加密技术?
在百度上搜索这个问题,除了广告比特币钱包使用了非对称加密技术,大部分都是讲解非对称加密原理的文章。 我还没有找到任何关于区块链如何使用非对称密码学的解释。
我在知乎上搜索了一下,发现了一个关于这个问题的问题,但是还没有答案。
嗯,我好像明白了,暧昧,这个问题怎么解决?
看源码!
所以比特币加密技术,我找了一个区块链项目,找到了非对称加密的代码。 下面结合代码来解释一下区块链是如何应用非对称加密的。
以Python代码为例,我们先看main函数。
def client ( a, m, f, t, p ):
if a:
if a == "n":
public_key, private_key = generate_ECDSA_keys()
click.echo("")
click.echo("address:{0}".format(public_key))
click.echo("")
click.echo("private_key:{0}".format(private_key))
click.echo("")
print("ok!")
if a == "s":
if ( m and f and t and p ):
print(send_transaction(f, t, m, p))
这部分是生成公钥和私钥。 公钥和私钥是非对称加密中的一对密钥,但公钥是公开的,私钥是私人持有的。 用公钥加密的密文可以用私钥解密。
公钥在广泛的广播中发布。 例如,公钥类似于银行卡号,私钥类似于银行卡密码。
也就是说,公钥也可以理解为一个地址,私钥可以理解为这个地址的密码。
我们先看看公钥和私钥的生成函数。
def generate_ECDSA_keys():
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
private_key = sk.to_string().hex()
vk = sk.get_verifying_key()
public_key = vk.to_string().hex()
public_key = base64.b64encode(bytes.fromhex(public_key))
return public_key.decode(),private_key
可以看出sk是一个签名,私钥是由签名转换而来的。
其中,这段代码表示公钥是从私钥推导出来的。
vk = sk.get_verifying_key()
它是怎么来的? 让我们看看功能。
def get_verifying_key(self):
"""
Return the VerifyingKey associated with this private key.
Equivalent to reading the `verifying_key` field of an instance.
:return: a public key that can be used to verify the signatures made
with this SigningKey
:rtype: VerifyingKey
"""
可以看出,这个函数的返回值指的是:
可用于验证由此生成的公钥
用于验证使用此签名密钥生成的签名的公钥。
因此,您应该能够理解私钥和公钥是如何生成的。
该函数的输出类似于以下内容:
address:Z/qsNWOAAWULpqvtM/OMHmJE+6PG0oPUsOMGk2ySYgrUB5noaZsD6b0NbbPgslr1cdninkqYKcJ+sx74/Mhn2A==
private_key:196f72bf05e307458a0691ca73a2981d859e499ef9fc264183feddde5bd47217
地址(公钥)和私钥。
接下来需要启动节点查看区块链的信息。
if __name__ == '__main__':
make_a_genesis_block()
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=8080, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
app.run(debug=True, host='0.0.0.0', port=port)
然后,根据下面的代码,使用签名将消息发送到链上。 -f是发送消息的节点地址,-t是接收消息的节点地址,-p是私钥。
@click.option('-f', help='from address')
@click.option('-t', help='to address')
@click.option('-p', help='private_key')
那么,我们就来看看如何上链吧。
if a == "s":
if ( m and f and t and p ):
print(send_transaction(f, t, m, p))
可以看出这个函数是上传消息到链上的函数,也就是发送交易信息的函数,然后我们进入这个函数。
def send_transaction(from_address, to_address, memo, private_key, private_key_own):
if private_key == private_key_own:
signature, message = sign_ECDSA_msg(private_key)
url = "http://localhost:8080/post"
d = {"from_address": from_address, "to_address": to_address,"memo":memo,
"signature":signature,"message":message}
r = requests.post(url, data=d)
return r.text
else:
return ("Wrong address or key length! Verify and try again.")
大致分析一下这个功能的设计思路。
if private_key == private_key_own:
这是验证收到的私钥是否与持有的私钥匹配的链接。 如果不匹配,直接跳转到else,不能进行链式操作。
如果它们匹配,则执行该函数。
当私钥对加密信息进行解密时,生成和消息,即签名和消息。 那么私钥解密函数就是这个函数。 更进一步,我们进入这个函数,看看私钥是如何解密的。
def sign_ECDSA_msg(private_key):
message = str(round(time()))
bmessage = message.encode()
sk = ecdsa.SigningKey.from_string(bytes.fromhex(private_key),
curve=ecdsa.SECP256k1)
signature = base64.b64encode(sk.sign(bmessage))
return signature, message
我们看到消息是Get Time Series。 这里的核心函数是函数,也就是导入的第三方库中的函数。 我们来看看这个函数的解释。
def from_string(cls, string, curve=NIST192p, hashfunc=sha1):
"""
Decode the private key from :term:`raw encoding`.
Note: the name of this method is a misnomer coming from days of
Python 2, when binary strings and character strings shared a type.
In Python 3, the expected type is `bytes`.
:param string: the raw encoding of the private key
:type string: bytes like object
:param curve: The curve on which the point needs to reside
:type curve: ecdsa.curves.Curve
:param hashfunc: The default hash function that will be used for
signing, needs to implement the same interface
as hashlib.sha1
:type hashfunc: callable
:raises MalformedPointError: if the length of encoding doesn't match
the provided curve or the encoded values is too large
:raises RuntimeError: if the generation of public key from private
key failed
:return: Initialised SigningKey object
:rtype: SigningKey
"""
可以看出输入是key,输出是. 什么是签名? 这里没有必要深入研究比特币密码学,因为输出也被输入到下一个函数中。 另外,让我们看看这个函数的作用。
def b64encode(s, altchars=None):
"""Encode the bytes-like object s using Base64 and return a bytes object.
Optional altchars should be a byte string of length 2 which specifies an
alternative alphabet for the '+' and '/' characters. This allows an
application to e.g. generate url or filesystem safe Base64 strings.
"""
如您所见,这是一个对字节进行编码的函数。 即通过函数获取存储信息和签名。
然后,在,上传函数得到的信息中,节点为8080。
这样就可以明确公钥、私钥和非对称加密技术在区块链中的工作机制。
当然,这段代码比较简单比特币钱包使用了非对称加密技术,介绍了最基本的区块链原理。 您还可以通过学习过程逐步了解更复杂的区块链技术。
挖矿网Ethos中文网拥有简单易用的挖矿系统,为挖矿行业提供教程软件和矿机评测及交易信息,对比计算各种数字货币在挖矿网的挖矿收益,以及介绍矿网挖矿工具,矿场最新动态等。
矿业网络,版权所有丨如未注明,均为原创丨本站采用BY-NC-SA协议授权
转载请注明原文链接:比特币加密技术 非对称加密技术如何应用于区块链或比特币?