最佳适用版本
Python3.8以上(亲测3.9.6适用)
(一)Crypto.RSA非对称加密模块
1.RSA.generateKeys() -> publicKey_base64, privateKey_base64
2.RSA.encrypt(plaintext, publicKey) -> cipher text_base64
3.RSA.decrypt(ciphertext_base64, privateKey_base64) -> plaintext
# DPFace_Crypto_RSA
import base64
import rsa
import DPFace_Crypto_Base64
# 可加密的字符串长度,默认为512
len_rsa = 512
# RSA.generateKeys() -> (Base64公钥, Base64私钥)
def RSA_generateKeys() -> (str, str):
# 生成公钥、私钥
(pubkey, privkey) = rsa.newkeys(len_rsa)
# Base64处理并返回
return (DPFace_Crypto_Base64.encode(str(pubkey)), DPFace_Crypto_Base64.encode(str(privkey)))
# RSA.encrypt(明文, Base64公钥) -> Base64密文
def RSA_encrypt(plaintext: str, publicKey: str):
# Base64公钥 -> 公钥
publickey = DPFace_Crypto_Base64.decode(publicKey)
n_beg = 10
n_end = publickey.find(",", n_beg, len(publickey))
e_beg = publickey.find(" ", n_end, len(publickey)) + 1
e_end = len(publickey)-1
n = int(publickey[n_beg:n_end])
e = int(publickey[e_beg:e_end])
# 明文编码格式
content = plaintext.encode("utf-8")
# 明文 -> HEX bytes
ciphertext = rsa.encrypt(content, rsa.PublicKey(n, e))
# ciphertext = rsa.encrypt(content, pubkey_tmp)
# HEX bytes -> HEX str
return DPFace_Crypto_Base64.encode(str(base64.b16encode(ciphertext)))
# RSA.decrypt(Base64密文, Base64私钥) -> 明文
def RSA_decrypt(ciphertext_base: str, privateKey: str):
# Base64密文 -> 密文(HEX str)
tmp = DPFace_Crypto_Base64.decode(ciphertext_base)
ciphertext_hex = tmp[2:len(tmp)-1]
# HEX str -> HEX bytes
ciphertext = base64.b16decode(ciphertext_hex)
# Base64私钥 -> str私钥
privatekey_str = DPFace_Crypto_Base64.decode(privateKey)
# str私钥 -> 私钥
n_beg = 11
n_end = privatekey_str.find(",", n_beg, len(privatekey_str))
e_beg = privatekey_str.find(" ", n_end, len(privatekey_str)) + 1
e_end = privatekey_str.find(",", e_beg, len(privatekey_str))
d_beg = privatekey_str.find(" ", e_end, len(privatekey_str)) + 1
d_end = privatekey_str.find(",", d_beg, len(privatekey_str))
p_beg = privatekey_str.find(" ", d_end, len(privatekey_str)) + 1
p_end = privatekey_str.find(",", p_beg, len(privatekey_str))
q_beg = privatekey_str.find(" ", p_end, len(privatekey_str)) + 1
q_end = len(privatekey_str) - 1
n = int(privatekey_str[n_beg:n_end])
e = int(privatekey_str[e_beg:e_end])
d = int(privatekey_str[d_beg:d_end])
p = int(privatekey_str[p_beg:p_end])
q = int(privatekey_str[q_beg:q_end])
privatekey = rsa.PrivateKey(n, e, d, p, q)
# 私钥解密
content = rsa.decrypt(ciphertext, privatekey)
# 明文编码格式
content = content.decode("utf-8")
return content
(二)Crypto.AES对称加密模块
1.AES.generateKeyIv() -> key, iv
2.AES.encrypt(plaintext, key, iv) -> ciphertext_base64
3.AES.decrypt(ciphertext, key, iv) -> plaintext
import base64
from Cryptodome.Cipher import AES
from Cryptodome import Random
# 数据块的大小 16位
BS = 16
# CBC模式 相对安全 因为有偏移向量 iv 也是16位字节的
mode = AES.MODE_CBC
# 填充函数 因为AES加密是一段一段加密的 每段都是BS位字节,不够的话是需要自己填充的
pad = lambda s: s + (BS - len(s.encode()) % BS) * chr(BS - len(s.encode()) % BS)
# 将填充的数据剔除
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
# AES_generateKeyIv() -> (公钥, 偏移)
def AES_generateKeyIv() -> (str, int):
# CBC加密需要一个十六位的key(密钥)和一个十六位iv(偏移量)
key = 'neudpfacetuandui'
# 随机获取iv
iv = Random.new().read(AES.block_size)
return (key, iv)
def AES_encrypt(plaintext: str, key: str, iv: int) -> str:
# 输入合法化
plaintext = pad(plaintext).encode()
# 定义初始化
cipher = AES.new(base64.b16encode(key), mode, base64.b16encode(iv))
# 加密并返回
return base64.b64encode(cipher.encrypt(plaintext)).decode()
def AES_decrypt(ciphertext: str, key: str, iv: int):
# 将密文进行base64解码
ciphertext = base64.b64decode(ciphertext)
# 初始化自定义
cipher = AES.new(base64.b16encode(key), mode, base64.b16encode(iv))
# 返回utf-8格式的数据
return unpad(cipher.decrypt(ciphertext[:])).decode()
(三)Crypto.SHA256散列模块
SHA256.hash(text) -> hash
import hashlib
# SHA256_hash(str文本) -> str散列
def SHA256_hash(text: str) -> str:
return hashlib.sha256(text.encode()).hexdigest()
(四)Crypto.Base64编码模块
1.Base64.encode(text) -> text_base64
2.Base64.decode(text_base64) -> text
import base64
# Base64.encode(文本) -> base64文本
def encode(text: str) -> str:
st = text.encode()
ans = str(base64.b64encode(st))
ans = ans[2:len(ans)-1]
return ans
# Base64.decode(base64文本) -> 文本
def decode(text: str) -> str:
ans = str(base64.b64decode(text))
ans = ans[2:len(ans)-1]
return ans
(五)Test所有模块
import DPFace_Crypto_Base64
import DPFace_Crypto_RSA
import DPFace_Crypto_SHA256
import DPFace_Crypto_AES
if __name__ == "__main__":
# 测试Base64
_input0 = input("请输入要Base64加密的文本: ")
_input0_base = DPFace_Crypto_Base64.encode(_input0)
print(_input0_base)
_input_debase = DPFace_Crypto_Base64.decode(_input0_base)
print(_input_debase)
# 测试RSA
(publickey, privatekey) = DPFace_Crypto_RSA.RSA_generateKeys()
_input = input("请输入需要RSA加密的文本: ")
ciphertext = DPFace_Crypto_RSA.RSA_encrypt(_input, publickey)
print("Base64+Rsa的密文为: ", ciphertext)
plaintext = DPFace_Crypto_RSA.RSA_decrypt(ciphertext, privatekey)
print("解密得到的明文为: ", plaintext)
# 测试SHA256
_input2 = input("请输入待Hash(SHA256)的文本: ")
print(DPFace_Crypto_SHA256.SHA256_hash(_input2))
# 测试AES CBC
(key, iv) = DPFace_Crypto_AES.AES_generateKeyIv()
_input3 = input("请输入待AES加密的文本: ")
ciphertext = DPFace_Crypto_AES.AES_encrypt(_input3, key, iv)
print("AES+base64密文:" + ciphertext)
plaintext = DPFace_Crypto_AES.AES_decrypt(ciphertext, key, iv)
print("原文:" + plaintext)
原创 · 未经允许不得转载
Comments 1 条评论
博客作者 Tula Lafond
When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Cheers!