您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。