Python加密操作 对称加密/非对称加密
安装包:
pycryptodome
https://pycryptodome.readthedocs.io/en/latest/src/installation.html#compiling-in-linux-ubuntu
1 from Crypto.Hash import SHA256 2 from Crypto.PublicKey import RSA 3 from Crypto import Random 4 from Crypto.Signature import pkcs1_15 5 from Crypto.Cipher import PKCS1_v1_5 6 7 key = RSA.generate(1024) 8 # 公钥 私钥 9 pri_key_text = key.export_key() 10 pub_key_text = key.public_key().export_key() 11 12 pub_key = RSA.import_key(pub_key_text) 13 pri_key = RSA.import_key(pri_key_text) 14 15 # 加密 16 text = b'abcdefg' 17 cipher = PKCS1_v1_5.new(pub_key) 18 etext = cipher.encrypt(text) 19 20 # 解密 21 cipher_de = PKCS1_v1_5.new(pri_key) 22 dtext = cipher_de.decrypt(etext,None) 23 24 # 签名 25 dg = SHA256.new(text) 26 sign = pkcs1_15.new(pri_key).sign(dg) 27 28 # 签名验证 29 dg2 = SHA256.new(dtext) 30 try: 31 pkcs1_15.new(pub_key).verify(dg2, sign) 32 except ValueError: 33 print('验证失败') 34 35 # 对称加密 36 37 from Crypto.Random import get_random_bytes 38 from Crypto.Cipher import AES 39 from Crypto.Util.Padding import pad, unpad 40 41 key = get_random_bytes(16) 42 cipher = AES.new(key, AES.MODE_CBC) 43 edata = cipher.encrypt(pad(text, AES.block_size)) 44 45 cipher_d = AES.new(key, AES.MODE_CBC, cipher.iv) 46 unpad(cipher_d.decrypt(edata),AES.block_size)
人工智障与神经病网络
https://www.cnblogs.com/JiangOil/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章