Home  >  Article  >  Backend Development  >  Encryption and decryption skills in Python web development

Encryption and decryption skills in Python web development

WBOY
WBOYOriginal
2023-06-17 12:53:081671browse

Python has become one of the important languages ​​​​in Web development, and encryption and decryption technology are an indispensable part of Web development. In this article, I will introduce encryption and decryption techniques in Python.

  1. Introduction to Encryption and Decryption

In web development, data security is always crucial, especially when some confidential data needs to be transmitted. Therefore, encryption and decryption technology came into being, which can protect data and ensure that only legitimate users can access or process the data.

To put it simply, encryption is to convert the original data into unreadable ciphertext through a certain encryption algorithm, while decryption is to restore the ciphertext to the original data. Encryption and decryption require the use of a specific key, and only the person who has mastered this key can perform encryption or decryption operations.

  1. Encryption and decryption algorithms

There are many encryption and decryption algorithms in Python, including AES, DES, RSA, etc. The following is a brief introduction to several commonly used algorithms.

(1)AES

AES (Advanced Encryption Standard) is an advanced encryption standard algorithm that can protect the security of data transmission. AES is a symmetric encryption algorithm, which means the same key is used during encryption and decryption. The AES encryption algorithm adopts a block cipher design. For each key length, there are standard block lengths, commonly used ones are 128 bits, 192 bits and 256 bits.

When using Python for AES encryption and decryption operations, you can use the AES module in the pycryptodome library or the fernet module in the cryptography library.

(2) RSA

RSA is an asymmetric encryption algorithm that uses two keys, a public key for encryption and a private key for decryption. The security of the RSA algorithm depends on the difficulty of prime factorization, usually with a key length of 1024 bits or 2048 bits.

When using Python for RSA encryption and decryption operations, you can use the RSA module in the pycryptodome library or the rsa module in the cryptography library.

(3)DES

DES (Data Encryption Standard) is a symmetric encryption algorithm that divides data into 64-bit blocks and uses a 56-bit key for encryption. DES has been deemed unsafe and is generally no longer used.

You can also use the DES module in the pycryptodome library to perform DES encryption and decryption operations in Python.

  1. Encryption and decryption implementation in Python

In Python, you can use various libraries and modules to perform encryption and decryption operations. The following uses examples to introduce the use of common libraries.

(1) Use pycryptodome to implement AES encryption and decryption

pycryptodome is a Python package that can provide various modules required for encryption and decryption operations. The following example demonstrates how to use pycryptodome for AES encryption and decryption.

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64

def encrypt_aes(data, key):
    cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC)
    cipher_text = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
    iv = base64.b64encode(cipher.iv).decode('utf-8')
    cipher_text = base64.b64encode(cipher_text).decode('utf-8')
    return iv, cipher_text

def decrypt_aes(iv, cipher_text, key):
    cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, base64.b64decode(iv.encode('utf-8')))
    plain_text = unpad(cipher.decrypt(base64.b64decode(cipher_text.encode('utf-8'))), AES.block_size)
    return plain_text.decode('utf-8')

In the above code, we use the AES module and Padding module of pycryptodome to perform encryption and decryption operations. The AES module receives a key and an initialization vector (for CBC mode), then uses the pad function to pad the data to an integer multiple of the AES block size before encrypting it. When decrypting, the AES module is also used to receive the key, initial vector and ciphertext, and the unpad function is used to remove the padding from the decrypted data.

(2) Use cryptography to implement RSA encryption and decryption

cryptography is a powerful encryption library in Python, which includes various encryption algorithms. The following example demonstrates how to use cryptography for RSA encryption and decryption.

from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes

def generate_rsa_key():
    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048
    )

    return private_key, private_key.public_key()

def encrypt_rsa(data, public_key):
    data = data.encode('utf-8')
    cipher_text = public_key.encrypt(
        data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    return cipher_text

def decrypt_rsa(cipher_text, private_key):
    plain_text = private_key.decrypt(
        cipher_text,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    return plain_text.decode('utf-8')

In the above code, we use the asymmetric module of cryptography, which provides operations such as RSA key generation, encryption and decryption. When generating private and public keys, we use the generate_private_key function and specify the public exponent (generally 65537) and key length (generally 2048 bits).

When encrypting, we use the encrypt function of the public key and specify parameters such as padding mode and hash algorithm. When decrypting, we use the decrypt function of the private key and also specify parameters such as padding mode and hash algorithm. It should be noted that when using cryptography for encryption and decryption operations, both the key and data need to use the bytes type.

  1. Summary

This article introduces common encryption and decryption algorithms in Python, and how to use various libraries and modules for encryption and decryption operations. In web development, encryption and decryption are one of the important means to ensure data security. I hope this article will be helpful to you.

The above is the detailed content of Encryption and decryption skills in Python web development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn