python encryption method

MD5 encryption

This is a very widely used encryption, irreversible, often used in everyday string encryption, the following I will brief you in this way, the main use Python comes with a module hashlib, the following test code, create a md5 object, and then encrypt directly on the line:

import hashlib

def MD5(string):
    '''md5加密'''
    h1 = hashlib.md5()     # 创建md5对象
    # 声明encode
    h1.update(string.encode(encoding = 'utf-8'))
    return h1.hexdigest()

if __name__ == "__main__":
    string = "MD5加密"
    print('MD5加密前为:' + string)
    print('MD5加密后为:' + MD5(string))

operation result:

MD5加密前为:MD5加密
MD5加密后为:e5c7bda34849527574a025bc06867c11

Hashed

Here with SHA1 (Secure Hash), for example, briefly explain how Python is hashed, hashlib also use this module, the following test code, very simple, first create sha1 object, and then encrypt directly on the line, also supported here SHA224, SHA256, etc. encryption:

import hashlib

def hash(string):
    '''哈希加密'''
    h1 = hashlib.sha1()     # 创建hash对象
    # 声明encode
    h1.update(string.encode(encoding = 'utf-8'))
    return h1.hexdigest()

if __name__ == "__main__":
    string = "MD5加密"
    print('MD5加密前为:' + string)
    print('MD5加密后为:' + hash(string))

operation result:

hash加密前为:MD5加密
hash加密后为:983bb73e82bd715e65584ea6580bad4ec321c0f4

DES encryption

This is a block encryption algorithm, encryption and decryption algorithm is the same, here I will brief you in this way, mainly used pycryptodome this module, enter the installation command pip install pycryptodomex, the following test code, you need to define a single key:

from Cryptodome.Cipher import DES
import binascii

# 设置一个密钥
key = b'abcdefgh'
# 需要去生成一个DES对象
des = DES.new(key,DES.MODE_ECB)
# 需要加密的数据
text = "This is a key!"
text = text + (8 - (len(text) % 8)) * '='
print("text:%s" % text)

# DES加密过程
encrypt_text = des.encrypt(text.encode())
encrypt_text = binascii.b2a_hex(encrypt_text)
print("DES加密后:" + encrypt_text.decode())

# DES解密过程
decrypt_text = binascii.a2b_hex(encrypt_text)
decrypt_text = des.decrypt(decrypt_text)
print("DES解密后:" + decrypt_text.decode())

operation result:

text:This is a key!==
DES加密后:a5ee554e36736a5d9b6db2cd9442ff38
DES解密后:This is a key!==

AES encryption

This encryption method is a widely used, is to improve and replace DES, here I will brief you in this way, also used pycryptodome this module, test code as follows, somewhat complicated

from Cryptodome.Cipher import AES
from Cryptodome import Random
from binascii import b2a_hex

# 密钥
key = b'This is a 16 key'
# 要加密的明文
data = "AES加密"
# 生成长度等于AES块大小的不可重复的密钥向量
iv = Random.new().read(AES.block_size)
# 使用key和iv初始化AES对象,使用MODE_CFB模式
mycipher = AES.new(key,AES.MODE_CFB,iv)
# 加密的明文长度必须为16的倍数
# 将iv(密钥向量)加到加密的密文开头,一起传输
ciphertext = iv + mycipher.encrypt(data.encode())
# 解密的话要使用key和iv生成新的AES对象
mydecrypt = AES.new(key,AES.MODE_CFB,ciphertext[:16])
# 使用新生成的AES对象,将加密的密文解密
decrypttext = mydecrypt.decrypt(ciphertext[16:])

print("ASD加密后:",b2a_hex(ciphertext)[16:])
print("ASD解密后:",decrypttext.decode())

operation result:

ASD加密后: b'18541e53ebfe5eef40d3d4631dc3c313d4'
ASD解密后: AES加密

RAS Encryption

This is an asymmetric encryption algorithm, often used in public key encryption and electronic commerce, the following I will brief you in this way, mainly used rsa this module, install command pip install rsa, the following test code, you need to define a single public and private keys

import rsa

key = rsa.newkeys(3000)   # 生成随机密钥
privateKey = key[1]   # 私钥
publicKey = key[0] # 公钥
message = "RSA加密"   # 要加密的明文
message = message.encode()

# RSA加密过程
cryptedMessage = rsa.encrypt(message,publicKey)
print("RSA加密后:",cryptedMessage)

# RSA解密过程
message = rsa.decrypt(cryptedMessage,privateKey)
message = message.decode()
print("RSA解密后:",message)

operation result:

RSA加密后: b"\x04\xf2\xaf\xbb\xe1\xffW\xd5\xabT`.\xd8\x02\xa7\x85\xdawF\x15Q\x88\xd9\xaf\x9b\xb7@gb\x8a\x81\xce\x8c2GCV;\r\x8a\x91NF\xd8\xb5\x99\x0b\xc5\xf9\xba\x94\xd4l\x8b\xa9a\xd5\x80\x98\xdeb\xcb\xf9N\x8c'Y\x9cX\xca\xf5$\x07A\x1a@\xb6-\xc7\xb22iNu0\xf6\x800\x9d\tB\x93\xe2\xa5P\xe7'+1*\x8aLV\xcac\xd6\x81\xfb\x0b\xee\xe9\x8b\xc4K\\\x0c/\x80\x184T\x80x\x87\xdc\xb3=\x12\x97\x8c\xa6\xecXF\x92\x87m$\x8e\x1c\xe7\x93;1\x9a;n`\xd8\xdb|\xbfW\x04\xfc\xbb\xec&\x8b\x91%\xd6\x12Qe\xa9\x84\xd1\x0c\xac\xd7\xabG\x945W7\x12\xae\xf3#\xd4\xf9\xaed^C\xae'w\x8aM\x01\r\xf9\x80\x1e\tl4\x1c)\\\x98\x01]e+[c\xf34\x99{7\xa5.\xefS\xd6\x99\xfa\x9f\x7f\xe0\x9b\xa2\xf9<p0%\xa1iG\x02\xf9\x0co\xde\xda\xab\x96`y\xb8\xb0\xe8\xd0 \xa9\xa1\x97@\xe7\x06\xa8\xac\x19-[W\xef\x0fR\xb7\\H\x0b\x15^\x0e@\x86g\xb8\xc8U=\xe7\xff\x06\n\x93<P\xc3\xa4\xa5\r\x8e5\xef+D\x9cM?\x1b\x11T\x8f\xa2\xb9\xa4,\x03n\x1e\xd0\xbc\x1ft\xab\x879\x11\xe9\x01\xffJ\xf9\x87\xb4/j\xf1O\x14\x19\xac\x8a\xb8C\xe5~o\xa9]\xca,\xe4\xe6\x18r\xd9\x8f]\xa1\xf8G\xed\x8c\xb6F\x9f\xabu\x12\xc3\x93\xf2] XO\xb0*\xadH0g\x88\x0c\xb9"
RSA解密后: RSA加密

Guess you like

Origin www.cnblogs.com/kadycui/p/11027107.html