都是作者累积的,且看其珍惜,大家可以尽量可以保存一下,如果转载请写好出处https://www.cnblogs.com/pythonywy
一.md5加密
1.简介
这是一种使用非常广泛的加密方式,不可逆的,常见16位和32位一般都是md5
import hashlib
data = '你好'
print(hashlib.md5(data.encode(encoding="UTF-8")).hexdigest()) #32位
print(hashlib.md5(data.encode(encoding="UTF-8")).hexdigest()[8:-8]) #16位
二.RSA加密
1.简介
非对称加密算法,也就是比较常见的公钥私钥加密,可逆的
2.指数和模加密无填充-模板一
import rsa
#模
m = "ae068c2039bd2d82a529883f273cf20a48e0b6faa564e740402375a9cb332a029b8492ae342893d9c9d53d94d3ab8ae95de9607c2e03dd46cebe211532810b73cc764995ee61ef435437bcddb3f4a52fca66246dbdf2566dd85fbc4930c548e7033c2bcc825b038e8dd4b3553690e0c438bbd5ade6f5a476b1cbc1612f5d501f"
#指数
e = '10001'
#加密参数
message = '123456'
class Encrypt(object):
def __init__(self, e, m):
self.e = e
self.m = m
def encrypt(self, message):
mm = int(self.m, 16)
ee = int(self.e, 16)
rsa_pubkey = rsa.PublicKey(mm, ee)
crypto = self._encrypt(message.encode(), rsa_pubkey)
return crypto.hex()
def _pad_for_encryption(self, message, target_length):
message = message[::-1]
max_msglength = target_length - 11
msglength = len(message)
padding = b''
padding_length = target_length - msglength - 3
for i in range(padding_length):
padding += b'\x00'
return b''.join([b'\x00\x00', padding, b'\x00', message])
def _encrypt(self, message, pub_key):
keylength = rsa.common.byte_size(pub_key.n)
padded = self._pad_for_encryption(message, keylength)
payload = rsa.transform.bytes2int(padded)
encrypted = rsa.core.encrypt_int(payload, pub_key.e, pub_key.n)
block = rsa.transform.int2bytes(encrypted, keylength)
return block
if __name__ == '__main__':
en = Encrypt(e, m)
print(en.encrypt(message))
3.指数和模加密无填充-模板二
import codecs
def rsa_encrypt(content):
public_exponent = '010001'
public_modulus = 'ae068c2039bd2d82a529883f273cf20a48e0b6faa564e740402375a9cb332a029b8492ae342893d9c9d53d94d3ab8ae95de9607c2e03dd46cebe211532810b73cc764995ee61ef435437bcddb3f4a52fca66246dbdf2566dd85fbc4930c548e7033c2bcc825b038e8dd4b3553690e0c438bbd5ade6f5a476b1cbc1612f5d501f'
content = content[::-1]
rsa = int(codecs.encode(content.encode('utf-8'), 'hex_codec'),
16) ** int(public_exponent, 16) % int(public_modulus, 16)
# 把10进制数rsa转为16进制('x'表示16进制),再取前256位,不够的在最前面补0
return format(rsa, 'x').zfill(256)
4.指数和模加密无填充-模板三
import math
if __name__ == '__main__':
# 实为16进制串,前补0
e = ''
# m也需要补00
m = '008eb933413be3234dddd2730fbb1d05c8848a43d5dc3bdd997f2a9935fba6beb9ffb36854482b0b46cf7e6f9afbbe2e2e7d606fde20bec57dbf722e7985192e8813e6b67628a6f202cf655b7d2ffce4e9dc682dd6034ae706c8e255f25e4051b9ca43f25b3ad686aac9c8f6aeb71d921c13a255c806f78a5a7b9a356c2dd274e3'
m = int.from_bytes(bytearray.fromhex(m), byteorder='big')
e = int.from_bytes(bytearray.fromhex(e), byteorder='big')
# js加密为反向,为保持一致原文应反向处理,所以这里原文实际为204dowls
plaintext = 'slwod402&#