python RSA加密的示例
RSA加密是一種非對稱加密,通常使用公鑰加密,私鑰解密,私鑰簽名,公鑰驗籤。
在公開金鑰密碼體制中,加密金鑰(即公開金鑰)PK是公開資訊,而解密金鑰(即祕密金鑰)SK是需要保密的.RSA演算法通常是先生成一對RSA金鑰,其中之一是保密金鑰,由使用者儲存;另一個為公開金鑰,可對外公開,甚至可在網路伺服器中註冊。
RSA是一種公鑰密碼演算法,加密演算法是將明文m(m<n是一個整數)加密成密文c,即明文數字m的 E 次方求mod N,也就是將明文與自己相乘E次,然後結果除以N求餘數,餘數就是密文c,E和N組合就是公鑰;解密演算法為將密文c解密為明文m,即密文數字c的D次方求mod N,也就是將密文與自己相乘D次,然後結果除以N求餘數,餘數就是明文m,D和N組合就是私鑰。
以下是關於RSA生成公鑰私鑰、加密、解密、加簽、驗籤的示例。
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+5NsbqAh6pjOMKF8I7FGa et3QMUi0g5xDfQAM219qqXnoPi2hmCMjR8MWJV/zyMZ6IiRG/pvrZ2ZhfDNFdW3Z SfHczRUvabABzWAr/57/eDBjswv4RQA+gUS6t8wFY/iV+O3i9+d79iN3VhUogfI3 3Ru3+RPFeFW88tYUhwIDAQAB -----END PUBLIC KEY----- -----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQDEedv+5NsbqAh6pjOMKF8I7FGaet3QMUi0g5xDfQAM219qqXno Pi2hmCMjR8MWJV/zyMZ6IiRG/pvrZ2ZhfDNFdW3ZSfHczRUvabABzWAr/57/eDBj swv4RQA+gUS6t8wFY/iV+O3i9+d79iN3VhUogfI33Ru3+RPFeFW88tYUhwIDAQAB AoGAApzaO5QAg+gioLroEZOR2/UEisjafUPCg0ACynT1lLYwGSOCzv9QrQbwZK42 HmvF0GCZnxMoJ1eIbEN2PZKgveQ/o4o8OdhuSk8pcDY72QwQHgSh1yfdvqkulo7D vdmjz63DVSrknYRQFYSHIwUPVVTWyT80OTCYSn3JGqNKa9kCQQDZUbtlBvcPzP4T 5hRaH9XvCh3PPusQKGPzVRVLn+qZ30N2PNgttYKRMshlz1WMts2ZAKr3b3BLT6GT shE5KatfAkEA53JkuV1vUEMg5j1ClgrwTs65yLWb+NbLH84xekrkkSxCkVjE7J/N QV7Uk87na49LuztBaECBmaZyTQnFKk7P2QJAMzcc18lVbmbcNipR/49jJquWrOHi GfO64nzZwPHWIx9H0dSzCcquE7QJIF1Fhx0JxRYwNJIRv93rcVhU0MjuNwJAIgI/ JrXCC4sxpGNQC3gkA5CA4Cs/dfsp8cx8nLmwiFx2k6D1nseEg5yJpAZ9HuL5f9Of MtB3uroohYVwAV1/UQJBAIZQkryoOJxPbfWvIIGBOAlS/QqfE5kLV+3L2RUtBlac nJykMucrdDx1gVKgoREUElxpYtlWI17h9MeNXRICgIM= -----END RSA PRIVATE KEY-----
2、加密(使用公鑰加密)
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。
如果需要加密的位元組數超出證書能加密的最大位元組數,此時就需要進行分段加密。
3、解密(使用私鑰解密)
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加密
4、加簽(使用私鑰加簽)
使用私鑰加簽,每次簽名是一致的。
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=
5、驗籤(使用公鑰驗籤)
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
以上就是python RSA加密的示例的詳細內容,更多關於python rsa加密的資料請關注我們其它相關文章!