您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关RSA加密如何在python项目中使用 ,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1、生成公钥私钥:
from Crypto import Random from Crypto.PublicKey import RSA # 伪随机数生成器 random_generator = Random.new().read # rsa算法生成实例 rsa = RSA.generate(1024, random_generator) # 私钥的生成 private_pem = rsa.exportKey() with open("private.pem", "wb") as f: f.write(private_pem) # 公钥的生成 public_pem = rsa.publickey().exportKey() with open("public.pem", "wb") as f: f.write(public_pem)
生成的公钥私钥文件在项目路径下,也可以直接指定生成文件路径。
文件样例
生成的公钥私钥格式是固定的,秘钥中间无空格无换行,秘钥末尾也空格无换行,如下:
-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEedv+5NsbqAh7pjOMKF8I7FGa et3QMUi0g5xDfQAM219qqXnoPi2hmCMjR8MWJV/zyMZ6IiRG/pvrZ2ZhfDNFdW3Z SfHczRUvabABzWAr/57/eDBjswv4RQA+gUS6t8wFY/iV+O3i9+d79iN3VhUogfI3 3Ru3+RPFeFW88tYUhwIDAQAB -----END PUBLIC KEY----- -----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQDEedv+5NsbqAh7pjOMKF8I7FGaet3QMUi0g5xDfQAM219qqXno Pi2hmCMjR8MWJV/zyMZ6IiRG/pvrZ2ZhfDNFdW3ZSfHczRUvabABzWAr/57/eDBj swv4RQA+gUS6t8wFY/iV+O3i9+d79iN3VhUogfI33Ru3+RPFeFW88tYUhwIDAQAB AoGAApzaO5QAg+gioLroEZOR2/UEisjafUPCg0ACynT1lLYwGSOCzv9QrQbwZK42 HmvF0GCZnxMoJ1eIbEN2PZKgveQ/o4o8OdhuSk8pcDY72QwQHgSh2yfdvqkulo7D vdmjz63DVSrknYRQFYSHIwUPVVTWyT80OTCYSn3JGqNKa9kCQQDZUbtlBvcPzP4T 5hRaH9XvCh4PPusQKGPzVRVLn+qZ30N2PNgttYKRMshlz1WMts2ZAKr3b3BLT6GT shE5KatfAkEA53JkuV1vUEMg5j1ClgrwTs65yLWb+NbLH84xekrkkSxCkVjE7J/N QV7Uk87na49LuztBaECBmaZyTQnFKk7P2QJAMzcc18lVbmbcNipR/49jJquWrOHi GfO64nzZwPHWIx9H0dSzCcquE7QJIF1Fhx0JxRYwNJIRv93rcVhU0MjuNwJAIgI/ JrXCC4sxpGNQC3gkA5CA4Cs/dfsp8cx8nLmwiFx2k6D1nseEg5yJpAZ9HuL5f9Of MtB3uroohYVwAV1/UQJBAIZQkryoOJxPbfWvIIGBOAlS/QqfE5kLV+3L2RUtBlac nJykMucrdDx1gVKgoREUElxpYtlWI17h9MeNXRICgIM= -----END RSA PRIVATE KEY-----
from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5 import base64 # 加密 message = "Hello,This is RSA加密" rsakey = RSA.importKey(open("public.pem").read()) cipher = Cipher_pkcs1_v1_5.new(rsakey) #创建用于执行pkcs1_v1_5加密或解密的密码 cipher_text = base64.b64encode(cipher.encrypt(message.encode('utf-8'))) print(cipher_text.decode('utf-8'))
加密结果:
Y1oivzbBDIEWX+NaXYLCJo5A226TmuemketZMUM3U80Rw3gSETjG5rHQ+S++Yao+iGQ5jSJA2yjkDtDAjdvi2VUz15LRSkdeKoliWnWy93KKl+aNEsBl3SUicATUgfNWU5ILo+DiltpF79AfIEhPptAz7+gN11KAf5LjfcQZ2+0=
这里每次使用公钥加密后的结果都不一致,跟对数据的padding即填充有关。
加密时支持的最大字节数与证书有一定关系。加密时支持的最大字节数:证书位数/8 -11(比如:2048位的证书,支持的最大加密字节数:2048/8 - 11 = 245)
1024位的证书,加密时最大支持117个字节,解密时为128;
2048位的证书,加密时最大支持245个字节,解密时为256。
如果需要加密的字节数超出证书能加密的最大字节数,此时就需要进行分段加密。
from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5 import base64 # 解密 cipher_text = "Y1oivzbBDIEWX+NaXYLCJo5A226TmuemketZMUM3U80Rw3gSETjG5rHQ+S++Yao+iGQ5jSJA2yjkDtDAjdvi2VUz15LRSkdeKoliWnWy93KKl+aNEsBl3SUicATUgfNWU5ILo+DiltpF79AfIEhPptAz7+gN11KAf5LjfcQZ2+0=" encrypt_text = cipher_text.encode('utf-8') rsakey = RSA.importKey(open("private.pem").read()) cipher = Cipher_pkcs1_v1_5.new(rsakey) #创建用于执行pkcs1_v1_5加密或解密的密码 text = cipher.decrypt(base64.b64decode(encrypt_text), "解密失败") print(text.decode('utf-8'))
解密结果,与加密前信息一致:
Hello,This is RSA加密
使用私钥加签,每次签名是一致的。
from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5 from Crypto.Hash import SHA import base64 #加签 message = "This is a request message..." rsakey = RSA.importKey(open("private.pem").read()) signer = Signature_pkcs1_v1_5.new(rsakey) digest = SHA.new() digest.update(message.encode("utf-8")) sign = signer.sign(digest) signature = base64.b64encode(sign) print(signature.decode('utf-8'))
签名结果:
fd99fQpbH48VT9YQKepyHSip9pwrJkm1PN3ZykHNrfTVk555fv392E7MtbIfcligOCWUx8nd3g+7J0Fo3x+9G1Y6MJs0CuMCbA4qulUMNGjzUpsN1URorMZfPKjPvhf22ARH9qZEnebQ7UUGO3ioy4nylZONb3Ldhga+PKyxYTM=
from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5 from Crypto.Hash import SHA import base64 #验签 message_verify = "This is a request message..." signature = "fd99fQpbH48VT9YQKepyHSip9pwrJkm1PN3ZykHNrfTVk555fv392E7MtbIfcligOCWUx8nd3g+7J0Fo3x+9G1Y6MJs0CuMCbA4qulUMNGjzUpsN1URorMZfPKjPvhf22ARH9qZEnebQ7UUGO3ioy4nylZONb3Ldhga+PKyxYTM=" rsakey = RSA.importKey(open("public.pem").read()) verifier = Signature_pkcs1_v1_5.new(rsakey) hsmsg = SHA.new() hsmsg.update(message_verify.encode("utf-8")) is_verify = verifier.verify(hsmsg, base64.b64decode(signature)) print(is_verify)
验签结果:
True
关于RSA加密如何在python项目中使用 就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。