In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to use Python to realize the public and private key relationship of block chain. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
1. The relationship between private key, public key and address
Private key, public key: the elliptic curve encryption algorithm is generated, but the private key cannot be obtained by backstepping the public key. The function of the public key is to encrypt the information with his own private key when trading with the other party, and then the other party uses his own public key to decrypt the original information, a process commonly known as signature.
Address: because the public key is too long to be used in the transaction, the public key hash is encrypted by SHA256, RIPEMD160 and Base58 algorithms to generate the address.
2. Public and private key encryption process
Private key signature process: signature is the process of using the private key to encrypt the message, and then send the original information and the encrypted information out.
Public key verification process: receive the information sent by the other party and the information signed by the private key, use the secret signed information of the other party's public key, and compare it with the original information, if it is consistent, it is not tampered with, on the contrary.
3. Python implementation (Etay Square)
Generate public and private keys
Tai Fong can generate public and private keys based on passwords.
From eth_account import Account
From eth_utils.hexadecimal import encode_hex
From eth_account.messages import encode_defunct
Import json
Def get_key (key):
"
Get public key and private key
: return: returns public key and private key
"
Ac1 = Account.create (key)
Return encode_hex (ac1.key), ac1.address
The resulting result:
The key is 123
The private key is 0xbd26862c106b7985319b72a08b34ffe2827affb1a7c8f17962456a6f7c5a8246
The public key is: 0x1761Ae9C3F60124338aEF74C5C322fB23C1AF8b2
Private key signature
When there is a transaction, the private key can be used to sign the transaction, so the private key should be saved.
Def message_sign (text, prv_key):
"
Obtain signature based on private key
: param text: text to be signed
: param prv_key: private key
: return: signature
"
Try:
Message = encode_defunct (text=text)
Result = Account.sign_message (message, prv_key)
Result = str (result)
Result = result [result.find ("'signature':"): result.find (')}')]
Return result [result.find ("('") + 2:] .replace ("'",'')
Except:
Return 'Private key format is incorrect'
The result is:
The original text is: qwe
The private key is 0xbd26862c106b7985319b72a08b34ffe2827affb1a7c8f17962456a6f7c5a8246
Signature is: 0x86b90940723e1667df873cfdcfc9ca52f045c29bb5ca700ad85f889a99c5bca43c3a5adc1d25f1b10b3314647424918426439178c0f17034cd8302d8305070131b
Public key signature verification
When the miner is packing, the public key can be used to verify the signature.
Def verifity (text, signature, address):
"
Verify signature
: param text: original text
: param signature: signature
: param address: public key
: return: verify the result
"
Try:
Message = encode_defunct (text=text)
Address_new = Account.recover_message (message, signature=signature)
If address = = address_new:
Return 'verify consistency'
Else:
Return 'validation failed'
Except:
Return 'format error'
The original text is: qwe
The public key is: 0x1761Ae9C3F60124338aEF74C5C322fB23C1AF8b2
Signature is: 0x86b90940723e1667df873cfdcfc9ca52f045c29bb5ca700ad85f889a99c5bca43c3a5adc1d25f1b10b3314647424918426439178c0f17034cd8302d8305070131b
The result is: verify consistency
Private key derives public key
According to the relational private key in 1, the public key can be derived.
Def recover_address (prv_key):
"
Push address based on private key
: param prv_key: private key
: return: address
"
Try:
Acct = Account.from_key (prv_key)
Return acct.address
Except:
Return 'format error'
The result is:
The private key is 0xbd26862c106b7985319b72a08b34ffe2827affb1a7c8f17962456a6f7c5a8246
The result is: 0x1761Ae9C3F60124338aEF74C5C322fB23C1AF8b2
After reading the above, do you have any further understanding of how to implement the block chain public and private key relationship with Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.