【2021-12-10】Python 加解密练习代码

pip install pycryptodome

# -*- coding:utf-8 -*-
import time
import json
import base64
import Crypto
import hashlib
from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import AES
from Crypto.Cipher import DES
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher
from Crypto.Signature import PKCS1_v1_5 as PKCS1_signature
from Crypto.PublicKey import RSA
from binascii import b2a_hex, a2b_hex
from Crypto.Util.Padding import unpad

import pyDes
from pyDes import des,ECB,PAD_PKCS5
from base64 import b64encode,decodebytes




print(f"{'+' * 30} [RSA 公钥-私钥] {'+' * 30}")
random_generator = Random.new().read
# 生成2048位的私钥
rsa = RSA.generate(2048,random_generator)
private_key = rsa.exportKey()
print(private_key.decode('utf-8'))
with open("private_key.rsa","wb") as f:
	f.write(private_key)


# 生成公钥
public_key=rsa.publickey().exportKey()
print(public_key.decode('utf-8'))
with open("public_key.rsa","wb") as f:
	f.write(public_key)



print(f"{'+' * 30} [使用公钥对内容进行rsa加密] {'+' * 30}")
message = "需要加密的信息"
with open("public_key.rsa") as f:
	pub_key = RSA.importKey(str(f.read()))
	cipher = PKCS1_cipher.new(pub_key)
	rsa_text = base64.b64encode(cipher.encrypt(bytes(message.encode('utf-8'))))
	print(rsa_text.decode('utf-8'))

	with open("private_key.rsa") as f:
		pri_key = RSA.importKey(str(f.read()))
		cipher = PKCS1_cipher.new(pri_key)
		back_text = cipher.decrypt(base64.b64decode(rsa_text),0)
		print(back_text.decode('utf-8'))
print(f"{'+' * 30} [分割线] {'+' * 30}\n\n")



print(f"{'+' * 30} [使用私钥生成签名] {'+' * 30}")
message = "需要加密的信息"
with open("private_key.rsa") as f:
	pri_key = RSA.importKey(str(f.read()))
	signer = PKCS1_signature.new(pri_key)
	digest = SHA.new()
	digest.update(message.encode('utf-8'))
	signature = base64.b64encode(signer.sign(digest))
	print(signature.decode('utf-8'))

	with open("public_key.rsa") as f:
		pub_key = RSA.importKey(str(f.read()))
		verifier = PKCS1_signature.new(pub_key)
		digest = SHA.new()
		digest.update(message.encode('utf-8'))
		print("签名验证结果: ",verifier.verify(digest,base64.b64decode(signature)))

print(f"{'+' * 30} [分割线] {'+' * 30}\n\n")



def to_base64(data):
	return str(b64encode(data), encoding="utf-8")

from pprint import pprint
# https://www.cnblogs.com/gqv2009/p/12681996.html
class Confusion_DES_ECB():
	def __init__(self, iv, key):
		self.iv = iv # 偏移量,加密方式不是ECB的时候加密key字段必须是16位字节,秘钥不够用0补充
		self.key = key # 加密key,加密方式ECB秘钥必须是八位字节
		self.des = des(self.key, ECB, self.iv, pad=None, padmode=PAD_PKCS5)  # 初始化DES,ECB模式的实例


	def encrypt(self, encrData):  # 加密函数
		params = {}
		params['0 加密方式'] = 'DES ECB PAD_PKCS5'
		params['1 HEX          '] = str(b2a_hex(self.des.encrypt(encrData.encode('utf-8'), padmode=PAD_PKCS5)), encoding="utf-8")
		params['2 BASE64       '] = to_base64(self.des.encrypt(encrData.encode('utf-8')))
		params['3 HEX TO BASE64'] = to_base64(b2a_hex(self.des.encrypt(encrData.encode('utf-8'), padmode=PAD_PKCS5)))
		pprint(params)
		return b2a_hex(self.des.encrypt(encrData.encode('utf-8'), padmode=PAD_PKCS5))


	def decrypt(self, decrData):  # 解密函数
		return str(self.des.decrypt(a2b_hex(decrData), padmode=PAD_PKCS5), encoding="utf-8")


	def encrypt_to_base64(self, encrData):  # 加密函数
		return str(b64encode(self.des.encrypt(encrData.encode('utf-8'))), encoding="utf-8")


	def decrypt_do_base64(self, decrData):  # 解密函数
		return str(self.des.decrypt(decodebytes(decrData.encode("utf-8")), padmode=PAD_PKCS5), encoding="utf-8")


print(f"{'+' * 30} [Confusion_DES_ECB 分割线] {'+' * 30}")
des_ecb = Confusion_DES_ECB('00000000','kkk11111')
res_hex = (des_ecb.encrypt('加密内容...')).upper() # 加密之后大写
print(res_hex)
print(des_ecb.decrypt(res_hex))

res_base64 = des_ecb.encrypt_to_base64('加密内容...')
print(res_base64)
print(des_ecb.decrypt_do_base64(res_base64))
print(f"{'+' * 30} [分割线] {'+' * 30}\n\n")







# Python-AES加密-ECB模式-PKCS7填充
# pip install pycryptodome
# https://blog.csdn.net/raymond531/article/details/87879655
class Confusion_AES_ECB:
    def __init__(self, key):
        self.key = key  # 初始化密钥 【由用户输入的16位或24位或32位长的初始密码字符串】
        self.length = AES.block_size  # 初始化数据块大小
        self.aes = AES.new(self.key.encode('utf-8'), AES.MODE_ECB)  # 初始化AES,ECB模式的实例
        # 截断函数,去除填充的字符
        self.unpad = lambda date: date[0:-ord(date[-1])]      


    def pad(self, text):
        '''
        填充函数,使被加密数据的字节码长度是block_size的整数倍
        '''
        count = len(text.encode('utf-8'))
        add = self.length - (count % self.length)
        entext = text + (chr(add) * add)
        return entext


    def encrypt(self, encrData):  # 加密函数
        res = self.aes.encrypt(self.pad(encrData).encode("utf-8"))
        return str(base64.b64encode(res), encoding="utf-8")


    def decrypt(self, decrData):  # 解密函数
        res = base64.decodebytes(decrData.encode("utf-8"))
        return self.unpad(self.aes.decrypt(res).decode("utf-8"))


print(f"{'+' * 30} [Confusion_AES_ECB 分割线] {'+' * 30}")
ecb = Confusion_AES_ECB("iiiioooojjjjpppp")  # 这里密钥的长度必须是16的倍数
res = ecb.encrypt("中文测试!")
print(res)
print(ecb.decrypt(res))
print(f"{'+' * 30} [分割线] {'+' * 30}\n\n")





class Confusion_AES_OFB:
    def __init__(self, key):
        self.key = key  # 初始化密钥
        self.length = 16  # 初始化数据块大小
        self.aes_encrypt = AES.new(self.key.encode('utf-8'), AES.MODE_OFB, b'0000000000000000')  # 初始化AES,ECB模式的实例
        self.aes_decrypt = AES.new(self.key.encode('utf-8'), AES.MODE_OFB, b'0000000000000000')  # 初始化AES,ECB模式的实例


    def pad(self, text):
    	'''
    	填充函数,使被加密数据的字节码长度是block_size的整数倍
    	'''
    	add = 0
    	count = len(text)
    	if count % self.length != 0:
    		add = self.length - (count % self.length)
    	return text + ('\0' * add)


    def encrypt(self, encrData):  # 加密函数
    	ciphertext = self.aes_encrypt.encrypt(self.pad(encrData).encode("utf-8"))
    	# return b2a_hex(ciphertext)
    	return str(base64.b64encode(b2a_hex(ciphertext)), encoding="utf-8")


    def decrypt(self, decrData):  # 解密函数
    	text = self.aes_decrypt.decrypt(a2b_hex(base64.b64decode(decrData))).decode('utf-8')
    	# return text.decode('utf-8').rstrip('\0')
    	return text.rstrip('\0')


print(f"{'+' * 30} [Confusion_AES_OFB 分割线] {'+' * 30}")
ofb = Confusion_AES_OFB("aes_keysaes_keysaes_keys")  # 这里密钥的长度必须是16的倍数
res = ofb.encrypt("迷心兔")
print(res)
print(ofb.decrypt(res))
print(f"{'+' * 30} [分割线] {'+' * 30}\n\n")





class Confusion_AES_CBC:
    def __init__(self, key, iv):
        self.key = key.encode('utf-8')
        self.iv = iv.encode('utf-8')
        self.length = 16  # 初始化数据块大小
        self.aes_encrypt = cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
        self.aes_decrypt = cipher = AES.new(self.key, AES.MODE_CBC, self.iv)


    # @staticmethod
    def pkcs7padding(self, text):
        ''' 明文使用PKCS7填充 '''
        bytes_length = len(text.encode('utf-8'))
        padding = self.length - (len(text),bytes_length)[bytes_length == len(text)] % self.length
        self.coding = chr(padding)
        return text + chr(padding) * padding


    def encrypt(self, content):
        ''' AES加密 '''
        encrypt_bytes = self.aes_encrypt.encrypt(self.pkcs7padding(content).encode('utf-8'))
        # 重新编码
        return str(base64.b64encode(encrypt_bytes), encoding='utf-8')


    def decrypt(self, content):
        ''' AES解密 '''
        text = self.aes_decrypt.decrypt(base64.b64decode(content)).decode('utf-8')
        return text.rstrip(self.coding)


print(f"{'+' * 30} [Confusion_AES_CBC 分割线] {'+' * 30}")
ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
cbc = Confusion_AES_CBC(key='ONxYDyNaCoyTzsp83JoQ3YYuMPHxk3j7', iv='yNaCoyTzsp83JoQ3')
res = cbc.encrypt(json.dumps({
	"CompanyName": "testmall",
	"UserId": "test",
	"Password": "grasp@101",
	"TimeStamp": f"{ts}"
}))
print("加密:", res)
print("解密:", cbc.decrypt(res))
print(f"{'+' * 30} [分割线] {'+' * 30}\n\n")







# https://www.endata.com.cn/BoxOffice/MovieStock/movies.html
from Crypto.Cipher import DES
from Crypto.Util.Padding import unpad

data = "046398EDC8670DD44D1C58131577E58C49DDB970BD47B14518E9FF24ACA673D5B2DC12F500F7026673FD2872796640ACC85B2902CE12B52D4EECB4C0FDD30B62BB193650CED33EE0A325041B25BBD2F54991F739EE63D57A395ECBA94C38623DCBC818165A601191E268E3B1A156DDAE611663A35AFA1847A477CBF9A9133E20E0DD5EB0440B7722409C5350186EA214C36C031504674CCC4E5FA48098A49C6E65E0E572FD0C7C8E5E7C854610F6B34A63DF3D57D19593333B24CA074EE59F0C62C609AF82935F025F5E63BE77594572723DFA8CD73D809E31E5F0E1F89E0FBA72D946648ACDBCD4D41D31003D586B27D33B323756339255A83E27BE0468C37131796B55EFB237E48A1665A1329525B1365FB1FA06965B9A4A9A05A9B19593333B24CA074DFF62CA16D28B1192935F025F5E63BE78DD496D632160890B816189C98819FF751BC63BC2BFE054D3B3A45CCF50F68ED3FDB83A0880FD2E79D59C1F44831F7057B4696AF8AB6C5C7DAFB2A7A0711354EFC2961E25B32AAA9"

def d(d, g, h):
    return d[:g] + d[g + h:]


def shell(g):
	j = int(g[len(g) - 1], 16) + 9
	k = int(g[j], 16)
	g = d(g, j, 1)
	j = g[k:k + 8]
	g = d(g, k, 8);
	# key = j.encode()
	# iv = j.encode()
	crypto = DES.new(key=j.encode(), mode=DES.MODE_ECB)
	j = unpad(crypto.decrypt(bytes.fromhex(g)), DES.block_size).decode()
	return j[:j.rfind('}') + 1]

      
print(shell(data))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷心兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值