current position:Home>Base64 encryption and decryption in Python

Base64 encryption and decryption in Python

2022-01-30 06:59:44 Random code 3000

Little knowledge , Great challenge ! This article is participating in “   A programmer must have a little knowledge   ” Creative activities


Base64 It's the most common transport on the network 8Bit Bytecode One of the ways to code ,Base64 It's based on 64 Three printable characters Binary system Method of data . You can see RFC2045~RFC2049, It has MIME Detailed specification of .

Base64 Encoding is the process from binary to character , Can be used in HTTP In the environment, the longer identification information . use Base64 The code is unreadable , It needs to be decoded before reading .

Base64 Because of the above advantages are widely used in various fields of computer , However, since the output includes more than two “ Symbol class ” character (+, /, =), Different application scenarios are developed Base64 Various “ variant ”. For unification and standardization Base64 Output ,Base62x It is regarded as an improved version of symbolization .

python Medium base64

take url Code as base64

#  Want to encode the string into base64, First convert the string into binary data 
url = ""
bytes_url = url.encode("utf-8")
str_url = base64.b64encode(bytes_url)  #  The encoded parameter must be binary data 

 Copy code 

decode base64

#  take base64 Decode it into a string 
import base64
url = "aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vc29uZ3poaXh1ZS8="
str_url = base64.b64decode(url).decode("utf-8")

 Copy code 


  • python in base64 Whether encoding or decoding , The return values are byte arrays
  • Decode the string , The encoding is a byte array

copyright notice
author[Random code 3000],Please bring the original link to reprint, thank you.

Random recommended