6-python library-md5 base64 AES RSA encryption algorithm

The data will be encrypted when doing some network transmission. Here are some common encryption algorithms.

1.md5

1. String md5

import hashlib


if __name__ == '__main__':
    username = "test"
    username_md5 = hashlib.md5(username.encode(encoding='UTF-8')).hexdigest()
    print(username_md5)

There are also various encryption methods of sha in hashlib. The call is the same as md5. Just replace md5 with sha1, you can try it yourself.

2. File md5

import hashlib

if __name__ == '__main__':

    username = "./test.txt"

    m = hashlib.md5()
    n = 1024 * 4
    inp = open(username, 'rb')
    while True:
        buf = inp.read(n)
        if buf:
            m.update(buf)
        else:
            break
    print(m.hexdigest())
    

2.base64

Base64 encryption method

import base64


if __name__ == '__main__':
    username = "test"
    username_encode = base64.b64encode(username.encode(encoding='UTF-8'))
    print(username_encode)
    username_decode = base64.b64decode(username_encode)
    print(username_decode)
    

3.AES

  • Secret key: use the secret key when encrypting, and need the same secret key to decrypt when decrypting
  • Plain text: parameters that need to be encrypted
  • Mode: aes encryption commonly used ECB and CBC mode (I only used these two modes, there are other modes)
  • iv Offset: This parameter is not required in ECB mode, but is required in AES mode
  1. AES CBC encryption:
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex


# 如果text不足16位的倍数就用空格补足为16位
def add_to_16(text):
    if len(text.encode('utf-8')) % 16:
        add = 16 - (len(text.encode('utf-8')) % 16)
    else:
        add = 0

    text = text + ('\0' * add)
    return text.encode('utf-8')


# 加密函数
def encrypt(text):
    key = '9999999999999999'.encode('utf-8')
    mode = AES.MODE_CBC
    iv = b'qqqqqqqqqqqqqqqq'
    text = add_to_16(text)
    cryptos = AES.new(key, mode, iv)
    cipher_text = cryptos.encrypt(text)
    # 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串
    return b2a_hex(cipher_text)


# 解密后,去掉补足的空格用strip() 去掉
def decrypt(text):
    key = '9999999999999999'.encode('utf-8')
    iv = b'qqqqqqqqqqqqqqqq'
    mode = AES.MODE_CBC
    cryptos = AES.new(key, mode, iv)
    plain_text = cryptos.decrypt(a2b_hex(text))
    return bytes.decode(plain_text).rstrip('\0')


if __name__ == '__main__':
    e = encrypt("hello world")  # 加密
    d = decrypt(e)  # 解密
    print("加密:", e)
    print("解密:", d)

2.AES ECB encryption

from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex


def add_to_16(text):
    if len(text.encode('utf-8')) % 16:
        add = 16 - (len(text.encode('utf-8')) % 16)
    else:
        add = 0
    text = text + ('\0' * add)
    return text.encode('utf-8')


# 加密函数
def encrypt(text):
    key = '9999999999999999'.encode('utf-8')
    mode = AES.MODE_ECB
    text = add_to_16(text)
    cryptos = AES.new(key, mode)

    cipher_text = cryptos.encrypt(text)
    return b2a_hex(cipher_text)


# 解密后,去掉补足的空格用strip() 去掉
def decrypt(text):
    key = '9999999999999999'.encode('utf-8')
    mode = AES.MODE_ECB
    cryptor = AES.new(key, mode)
    plain_text = cryptor.decrypt(a2b_hex(text))
    return bytes.decode(plain_text).rstrip('\0')


if __name__ == '__main__':
    e = encrypt("hello world")  # 加密
    d = decrypt(e)  # 解密
    print("加密:", e)
    print("解密:", d)

4. If

The encryption and decryption of rsa requires a public key and a private key. Generally, the client uses the public key for encryption, and the server uses the private key for decryption to verify the legitimacy.

Execute the above two commands on ubuntu to generate two files: private.key and public.key

openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.key

Use the above two files for encryption and decryption tests, as follows:

import rsa


if __name__ == '__main__':
    username = "test"
    with open("./public.key", mode="rb") as f:
        public_key = f.read()

    pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(public_key)
    username_rsa = rsa.encrypt(username.encode(), pubkey)
    print(username_rsa.hex())
    print(len(username_rsa))

    with open("./private.key", mode="rb") as f:
        private_key = f.read()

    prikey = rsa.PrivateKey.load_pkcs1(private_key)
    username = rsa.decrypt(username_rsa, prikey)
    print(username)
Published 106 original articles · praised 76 · 130,000 visits +

Guess you like

Origin blog.csdn.net/Creator_Ly/article/details/104710771