一、Python之md5使用
- 功能:MD5签名是一个哈希函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示),
不可逆
- 传入参数:bytes类型
- 导入:from hashlib import md5
- 例子1:
import requests from hashlib import md5 url = "http://p3-tt.bytecdn.cn/list/pgc-image/cd7367fde2454137b2874f62dba5c43f" md5_url = md5(url.encode('utf8')).hexdigest() print(md5_url) # 2f7108ac307fd06f5995948f35a70f2f response = requests.get(url) md5_content = md5(response.content).hexdigest() print(md5_content) # bf93cde0b6edb50b9e95106c11fdd5ad
- 例子2:
from hashlib import md5 chinese = '你好呀' md5_chinese = md5(chinese.encode('utf8')).hexdigest() print(md5_chinese) # 4f65fdb33e0f2bd0de6bd1b41fdea968
- 例子3:
from hashlib import md5 english = 'd8hgeo-2d73k0s2' md5_str = md5(english.encode('utf8')).hexdigest() print(md5_str) # 74335cc0ff464e7006d1530260385a52
二、Python之sha1使用
- 功能:安全哈希算法,SHA1基于MD5,加密后的数据长度更长
- 传入参数:bytes类型
- 导入:from hashlib import sha1
- 例子1:
from hashlib import sha1 def sha1_encrypt_text(decrypt_text: str) -> str: """ SHA1加密 :param decrypt_text: 明文 :return: 密文 """ return sha1(decrypt_text.encode('utf8')).hexdigest() print(sha1_encrypt_text("213432"))
三、Python之base64使用
- 功能:对任意字符串加密,输出常常以=结尾
- 传入参数:bytes类型
- 导入:import base64
- 例子1:
import base64 def base64_encrypt_text(decrypt_text: str) -> str: """ Bse64加密 :param decrypt_text: 明文 :return: 密文 """ return base64.b64encode(decrypt_text.encode("utf-8")).decode() def base64_decrypt_text(encrypt_text: str) -> str: """ Bse64解密 :param encrypt_text: 密文 :return: 明文 """ return base64.b64decode(encrypt_text).decode("utf-8") # base64加密 print(base64_encrypt_text("3243242")) # base64解密 print(base64_decrypt_text("MzI0MzI0Mg=="))
四、Python之rsa使用
- 功能:rsa是非对称加密,即加密用公钥加密,用私钥解密,传出通常是base64或者是16进制字符串形式
- 传入参数:bytes类型
- 导入:import rsa、from binascii import b2a_hex、import base64
- 例子1:公钥格式如
-----BEGIN RSA PUBLIC KEY----- MIGJAoGBANkDMlvSfrZao.... -----END RSA PUBLIC KEY-----
import rsa from binascii import b2a_hex import base64 def rsa_encrypt_text1(public_key, decrypt_text: str, method="base64") -> str: """ RSA加密 :param public_key: 公钥 :param decrypt_text: 明文 :param method: 用base64加密还是16进制字符串 :return: 加密后的数据 """ encrypt_text = rsa.encrypt(decrypt_text.encode('utf-8'), rsa.PublicKey.load_pkcs1(public_key)) if method == "base64": return base64.b64encode(encrypt_text).decode() else: return b2a_hex(encrypt_text).decode() # 公钥格式: publicKey = ''' -----BEGIN RSA PUBLIC KEY----- MIGJAoGBANkDMlvSfrZaowpBQUF0gFRzwt3w0q4rWet33FdWuq39TBdCVicy4Zzw zX5ziuFExkdwbVrhEHBRkGNBJYVJ/1F1gAwd3KdyaId5NwATFkkLyM3wOtFqttwm o888U9KwIyhGH2bFkRIsk+KQmNQIR1m8Fgpdsy2JbnLd559HpTZZAgMBAAE= -----END RSA PUBLIC KEY----- ''' print(rsa_encrypt_text1(publicKey, "nihao@456")) # 以base64输出 print(rsa_encrypt_text1(publicKey, "nihao@456", 'hex')) # 以十六进制字符串输出
- 例子2:公钥格式如
-----BEGIN PUBLIC KEY----- MIGJAoGBANkDMlvSfrZao.... -----END PUBLIC KEY-----
import rsa from binascii import b2a_hex import base64 def rsa_encrypt_text2(public_key, _text: str, method="base64") -> str: """ RSA加密,针对setPublicKey :param public_key: 公钥 :param _text: 明文 :param method: 用base64加密还是16进制字符串 :return: 加密后的数据 """ encrypt_text = rsa.encrypt(_text.encode('utf-8'), rsa.PublicKey.load_pkcs1_openssl_pem(public_key)) if method == "base64": return base64.b64encode(encrypt_text).decode() else: return b2a_hex(encrypt_text).decode() # 公钥格式: public_key1 = """ -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDnJUXorWKGZEpLjgP9Aado78y8LwNiAqJNXkxLC0I5/rtnLmz8DuMgjxRVL+5iBeZ5a/Qm0zOOWd5/IJNLwZ6iAqX3NTxMuioAzaxXQWuhrgVJ+cxhWKuJGe1bsaPIUS+Py79a20FolQN+xT8Lf9aCTk9HdhjOd27LbX5DqwmO/wIDAQAB -----END PUBLIC KEY----- """ print(rsa_encrypt_text2(public_key1, "nihao@456")) # 以base64输出 print(rsa_encrypt_text2(public_key1, "nihao@456", 'hex')) # 以十六进制字符串输出
- 例子3:公钥格式如
978C0A92D2173439707498F0944...
import rsa from binascii import b2a_hex import base64 def rsa_encrypt_text3(key, _text: str, method="base64"): """ RSA加密,针对new RSAKeyPair import rsa from binascii import b2a_hex :param key: 公钥的参数 :param _text: 待加密的明文 :param method: 用base64加密还是16进制字符串 :return: 加密后的数据 """ e = int('010001', 16) n = int(key, 16) pub_key = rsa.PublicKey(e=e, n=n) encrypt_text = rsa.encrypt(_text.encode(), pub_key) if method == "base64": return base64.b64encode(encrypt_text).decode() else: return b2a_hex(encrypt_text).decode() # 公钥格式: public_key1 = "978C0A92D2173439707498F0944AA476B1B62595877DD6FA87F6E2AC6DCB3D0BF0B82857439C99B5091192BC134889DFF60C562EC54EFBA4FF2F9D55ADBCCEA4A2FBA80CB398ED501280A007C83AF30C3D1A142D6133C63012B90AB26AC60C898FB66EDC3192C3EC4FF66925A64003B72496099F4F09A9FB72A2CF9E4D770C41" print(rsa_encrypt_text3(public_key1, "nihao@456")) # 以base64输出 print(rsa_encrypt_text3(public_key1, "nihao@456", 'hex')) # 以十六进制字符串输出