Python加密解密大全(md5、sha1、base64、url编码、DES、AES、HmacSHA256、RSA)
文章目录
一、MD5加解密
密文形式:
499e457fe872851c5169f3935b123708
import hashlib# 1、加密txt = hashlib.md5('密文字符串'.encode(encoding='UTF-8')).hexdigest()print(txt)# 2、解密# 无解
二、sha1加解密
密文形式:
bcb304ffc292603a0865e9ffcf082f6350df67cb
import hashlib# 1、加密txt = "你好,世界"sha1 = hashlib.sha1()sha1.update(txt.encode('utf-8'))encrypt_txt = sha1.hexdigest()print("加密的结果:",encrypt_txt)# 2、解密# 无解
三、base64加解密
密文形式:
5L2g5aW9LOS4lueVjOOAguS4lueVjOi/meS5iOWkp++8jOaIkeaDs+WOu+eci+eci+OAgg==
import base64# 1、加密txt = "你好,世界"encrypt_txt = base64.b64encode(txt.encode('utf-8'))print("加密的结果:",encrypt_txt.decode())# 2、解密txt = base64.b64decode(encrypt_txt).decode("utf-8")print("解密的结果:",txt)
四、url编解码
密文形式:
%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C
from urllib import parsetxt = "你好世界"url_encode = parse.quote(txt)print("url编码结果:",url_encode)url_decode = parse.unquote(url_encode)print("url解码结果:",url_decode)
五、DES加解密
密文形式1:
9f07ebab7634f3f985be789f817e19a5
密文形式2:DHS3RQRCu2IhJCMh0pf8kQ==
(一)CBC模式
import binasciifrom pyDes import des, CBC, PAD_PKCS5import base64def des_encrypt_1(secret_key, s): # 加密方式一 iv = secret_key k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5) en = k.encrypt(s, padmode=PAD_PKCS5) return binascii.b2a_hex(en).decode()def des_encrypt_2(secret_key, s): # 加密方式二 iv = b"\x01\x02\x03\x04\x05\x06\x07\x08" k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5) en = k.encrypt(s, padmode=PAD_PKCS5) return base64.b64encode(en).decode()def des_decrypt_1(secret_key, s): # 解密方式一 iv = secret_key k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5) de = k.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5) return de.decode()def des_decrypt_2(secret_key, s): # 解密方式二 iv = b"\x01\x02\x03\x04\x05\x06\x07\x08" k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5) de = k.decrypt(base64.b64decode(s), padmode=PAD_PKCS5) return de.decode()key = 'b3L11XNL' # 必须为8位txt = "hello world!"encrypt_txt_1 = des_encrypt_1(key, txt)print('DES加密方式一结果:', encrypt_txt_1)clear_str_1 = des_decrypt_1(key, encrypt_txt_1)print('DES解密方式一结果:', clear_str_1)encrypt_txt_2 = des_encrypt_2(key, txt)print('DES加密方式二结果:', encrypt_txt_2)clear_str_2 = des_decrypt_2(key, encrypt_txt_2)print('DES解密方式二结果:', clear_str_2)
(二)ECB/NoPadding模式(Zeros填充)
from Cryptodome.Cipher import DESimport base64def pad(text_byte): while len(text_byte) % 8 != 0: text_byte += b"\x00" # 进行 Zeros 填充 return text_bytedef des_encrypt(text,key): des = DES.new(key.encode('utf-8'), DES.MODE_ECB) # 创建一个DES实例 padded_text_byte = pad(text.encode('utf-8')) encrypted_text = des.encrypt(padded_text_byte) # 加密 return base64.b64encode(encrypted_text).decode()def des_decrypt(encrypt_txt,key): des = DES.new(key.encode('utf-8'), DES.MODE_ECB) # 创建一个DES实例 base64_decode = base64.b64decode(encrypt_txt) decrypt_txt = des.decrypt(base64_decode).decode() return decrypt_txtkey = '12345678' # 密钥为8位text = "hello world!" # 加密文本encrypt_txt = des_encrypt(text,key)print(encrypt_txt)decrypt_txt = des_decrypt(encrypt_txt,key)print(decrypt_txt)
六、AES加解密
密文形式:
7wn9R9Ph1gnxItBI4cT74w==
(一)CBC模式
import base64from Crypto.Cipher import AESimport redef AES_Encrypt(data, key): data = data + (16 - len(data.encode()) % 16) * chr(16 - len(data.encode()) % 16) iv = b"\x01\x02\x03\x04\x05\x06\x07\x08\t\x01\x02\x03\x04\x05\x06\x07" # key = bytes.fromhex('0a010b05040f070917030106080c0d5b') # 秘钥Hex形式 # iv = bytes.fromhex('0a010b05040f070917030106080c0d5b') # 向量iv的Hex形式 cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC, iv=iv) # 加密 ciphertext = cipher.encrypt(data.encode("utf-8")) return base64.b64encode(ciphertext).decode()def AES_Dncrypt(text, key): iv = b"\x01\x02\x03\x04\x05\x06\x07\x08\t\x01\x02\x03\x04\x05\x06\x07" aes = AES.new(key.encode("utf-8"), AES.MODE_CBC, iv=iv) base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8')) # 优先逆向解密base64成bytes decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8') # 执行解密密并转码返回str decrypted_text = re.sub('[^!-~]+',' ',decrypted_text).strip() # 去除特殊不可见字符 return decrypted_textkey = 'at1Q722ZZu8tLhJ1' # 必须为16位txt = "hello world!"encrypt_txt = AES_Encrypt(txt,key)print("AES加密结果:",encrypt_txt)print("AES解密结果:",AES_Dncrypt(encrypt_txt,key))
(二)ECB模式
import base64from Crypto.Cipher import AESimport redef AES_Encrypt(data, key): # AES的ECB模式加密 data = data + (16 - len(data.encode()) % 16) * chr(16 - len(data.encode()) % 16) cipher = AES.new(key.encode("utf-8"), AES.MODE_ECB) ciphertext = cipher.encrypt(data.encode("utf-8")) return base64.b64encode(ciphertext).decode()def AES_Dncrypt(text, key): # AES的ECB模式解密 aes = AES.new(key.encode("utf-8"), AES.MODE_ECB) base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8')) # 优先逆向解密base64成bytes decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8') # 执行解密密并转码返回str decrypted_text = re.sub('[^!-~]+',' ',decrypted_text).strip() # 去除特殊不可见字符 return decrypted_textkey = 'UmcFNAQcJNU4DD4W' # 钥匙必须为16位data = "hello world" # 加密内容encrypt_txt =AES_Encrypt(data,key)print("AES加密结果:",encrypt_txt)print("AES解密结果:",AES_Dncrypt(encrypt_txt,key))
七、HmacSHA256加解密
密文形式1:
740cf79fd5e831ed4b24376e5c68715ac8b3aba3a7f863762af20139ec3158ee
密文形式2:dAz3n9XoMe1LJDduXGhxWsizq6On+GN2KvIBOewxWO4=
HmacSHA1加解密,则用from hashlib import sha1
from hashlib import sha256import hmacimport base64import binasciidef encrypt_1(data, key): key = key.encode('utf-8') message = data.encode('utf-8') sign = binascii.b2a_hex(hmac.new(key, message, digestmod=sha256).digest()).decode() return signdef encrypt_2(data, key): key = key.encode('utf-8') message = data.encode('utf-8') sign = base64.b64encode(hmac.new(key, message, digestmod=sha256).digest()) sign = str(sign, 'utf-8') return signdata ="你好,世界"key = '123456789'print(encrypt_1(data,key))print(encrypt_2(data,key))
八、RSA加解密
密文形式:
U/jVOvdftVnJQWyU3BNVDMQWlgubtBL5GEqi1L7GAdfEt+o8czO3Xfg==
from Crypto.PublicKey import RSAfrom Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5import base64# 公钥public_key = '''-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqcKXN9SC3qEpr5NKCPfzPRUX7nbH9KCQL44TZTxNcP4bsJCDbaP+wlJqr8ZbSXZI6EX6CAwnSVMYD65AKPRfVP+/cK3Ga7xuhfPTmXmMZVfLODpAfEGfjH1+aY308j8RERmgpPCxOfCzq67Yp9sGP7Tgf0IOTBPj3aOtbUEKIflMdVXKWOi6LqR+6LOTgcX9ByJVsuSgyNq62As/33h7pMXYxfSO0rYboloKjgV1pR/yiv6WfYca6MJDtn+kN8JMLtFDK3qgKjiz/f35OQ12IKnckNWLqGsb8sEx4YzycFG+PNY3GvQKWmzlEb+5l939wd7KvOOxCEf3ogoS4m+XOwIDAQAB-----END PUBLIC KEY-----'''def rsa_encrypt(message): """校验RSA加密 使用公钥进行加密""" cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(public_key)) cipher_text = base64.b64encode(cipher.encrypt(message.encode())).decode() return cipher_textif __name__ == '__main__': encrypt_txt = rsa_encrypt('''hello word''') print(encrypt_txt)
九、生成各种随机参数
(一)设备uuid
密文形式:
ad84e998-aeef-4d85-a0a1-ade7c8645389
import uuiduid = str(uuid.uuid4())print(uid)
来源地址:https://blog.csdn.net/weixin_51111267/article/details/124797218
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341