Fortune Telling Collection - Comprehensive fortune-telling - 12: string encryption and decryption

12: string encryption and decryption

Coding: the process of representing character strings in a computer into binary data in a certain order.

Character encoding varies from country to country:

1, computer->; Stands for [English letters, numbers and some special symbols]-> Ascii coding [0~256]

2, universal code, unified character coding [claims to be able to unify the expression of any language in the world]

Any data in any language can use one character to represent unicode encoding.

3. China has GB 2312->; GBK->; GB 18030

4. Data transmission coding: Unicode transmission format 8 bits [UTF-8].

Coding and decoding in python;

Fatal rule: the encoding and decoding of strings is a difficult and important point in any language.

Any string-> Are all made up of bytes!

In python 3: character: (str); Bytes (bytes)

Character-> Byte: encode: encode a string into binary data that the computer can operate.

Bytes-> Character: decode: Decode a binary data into natural data according to the specified encoding.

What is encryption? One plaintext data is calculated according to a specified algorithm to get another ciphertext data that can hide real information. This process is called encryption; The processing algorithm is called encryption algorithm; The key data used is called the key.

What is decryption? According to the specified algorithm and key data, the process of obtaining correct plaintext data from ciphertext data is called decryption operation.

(1), one-way encryption algorithm: an algorithm that can only be encrypted but not decrypted.

For example, the user account password is stored (one-way encryption), and no one can view the user's plaintext password at this time.

Process->; User input plaintext password-> Encryption->; And compare it with the stored ciphertext password->; Equality-success

One-way hash encryption algorithm->; Message digest algorithm 5

Single hash encryption algorithm->; SHAX encryption

(2) Two-way encryption algorithm: it can be encrypted, and the encrypted data can be decrypted to get plaintext data.

Used in more scenes; Encrypted data transmission->; Target address-> Decrypt to obtain plaintext data for processing.

Symmetric encryption: encryption and decryption use the same key;

Asymmetric encryption: encryption and decryption use different keys; E.g. HTTPS transmission data.

Hashlib mainly provides character encryption function, integrates md5 and sha modules together, and supports MD5, SHA 1, SHA224, SHA256, SHA384, SHA5 12 and other algorithms.

Note: hashlib encrypted string type is binary code, and directly encrypting the string will give an error:

There are two ways to convert a string into binary data.