\

Author: Boblee, master of artificial intelligence, good at and love python, research artificial intelligence, swarm intelligence, blockchain and other technologies based on python, and use python to develop front-end and back-end, crawler and so on.

1. The relationship between private key, public key, and address

Private key and public key: the elliptic curve encryption algorithm generates the private key, but the private key cannot be obtained through the public key backward. A public key encrypts information with its private key and decrypts the original information with its public key. This process is commonly known as signature.

Address: Because the public key is too long, it is not convenient to use 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 signing process: A private key is used to encrypt message and send the original message and encrypted message.

Public key verification process: After receiving the information sent by the other party and the information signed by the private key, use the information signed by the other party’s public key and compare it with the original information. If the information is consistent with the original information, it is not tampered with.

3. Python Implementation (Ethereum)

Generating public and private Keys

Ethereum 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):
    """Obtaining public and private keys :return: Returns public and private keys"""
    ac1 = Account.create(key)
    return encode_hex(ac1.key),ac1.address
Copy the code

Generated results:

The key is 123

The private key is: 0 xbd26862c106b7985319b72a08b34ffe2827affb1a7c8f17962456a6f7c5a8246

The public key is: 0 x1761ae9c3f60124338aef74c5c322fb23c1af8b2

The private key signature

When there is a transaction, the private key can be used to sign the transaction, so keep the private key well.

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 wrong'
Copy the code

The result is:

The original text is: Qwe

The private key is: 0 xbd26862c106b7985319b72a08b34ffe2827affb1a7c8f17962456a6f7c5a8246

The signature is: 0x86b90940723e1667df873cfdcfc9ca52f045c29bb5ca700ad85f889a99c5bca43c3a5adc1d25f1b10b3314647424918426439178c0f17034cd8302 d8305070131b

Public key attestation

The public key can be used to verify the signature when the miner is packaged.

def verifity(text, signature, address):
    """Verify signature: param text: original text: param signature: signature: param address: public key :return: verification result"""
    try:
        message = encode_defunct(text=text)
        address_new = Account.recover_message(message, signature=signature)
        if address == address_new:
            return 'Verify consistent'
        else:
            return 'Verification failed'
    except:
        return 'Formatting error'
Copy the code

The original text is: Qwe

The public key is: 0 x1761ae9c3f60124338aef74c5c322fb23c1af8b2

The signature is: 0x86b90940723e1667df873cfdcfc9ca52f045c29bb5ca700ad85f889a99c5bca43c3a5adc1d25f1b10b3314647424918426439178c0f17034cd8302 d8305070131b

The result: validation is consistent

The private key is derived from the public key

The public key can be derived from the relational private key in 1.

def recover_address(prv_key):
    """Param prv_key: private key: return: address"""
    try:
        acct = Account.from_key(prv_key)
        return acct.address
    except:
        return 'Formatting error'
Copy the code

The result is:

The private key is: 0 xbd26862c106b7985319b72a08b34ffe2827affb1a7c8f17962456a6f7c5a8246

The result is: 0 x1761ae9c3f60124338aef74c5c322fb23c1af8b2

4, endnotes

Eth_account is the python-based ethereum package used in this article. The installation only needs PIP install eth_Account. Eth_account also has other features such as transaction hash derivation of addresses, public key encryption, etc.

Appreciate the author

Python Chinese community as a decentralized global technology community, to become the world’s 200000 Python tribe as the vision, the spirit of Chinese developers currently covered each big mainstream media and collaboration platform, and ali, tencent, baidu, Microsoft, amazon and open China, CSDN industry well-known companies and established wide-ranging connection of the technical community, Have come from more than 10 countries and regions tens of thousands of registered members, members from the ministry, tsinghua university, Peking University, Beijing university of posts and telecommunications, the People’s Bank of China, the Chinese Academy of Sciences, cicc, huawei, BAT, such as Google, Microsoft, government departments, scientific research institutions, financial institutions, and well-known companies at home and abroad, nearly 200000 developers to focus on the platform.

Recommended reading:

2020Python Recruitment promotion channel now open! \

Old driver teaches you to read Python decorator in 5 minutes \

Using Python to implement particle swarm optimization \

Bargain-hunting us stocks? Using Python to analyze the real return of U.S. stocks \

▼ clickBecome a registered member of the community if you likeIn the see