十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
python rsa-oaep如何實(shí)現(xiàn)加密?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
代碼:
from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import base64 rsa_key_pair = RSA.generate(1024) pubkey = rsa_key_pair.publickey().export_key() privkey = rsa_key_pair.export_key() print(pubkey.decode()) print(privkey.decode()) # 公鑰加密 text = "hello world" rsa_pubkey = RSA.import_key(pubkey) cipher_pub = PKCS1_OAEP.new(rsa_pubkey) ciphervalue_enc = base64.b64encode(cipher_pub.encrypt(text.encode("utf-8"))) print("加密內(nèi)容:\n{}".format(ciphervalue_enc.decode())) #私鑰解密 rsa_privkey = RSA.import_key(privkey) cipher_priv = PKCS1_OAEP.new(rsa_privkey) ciphervalue_dec = cipher_priv.decrypt(base64.b64decode(ciphervalue_enc)) print("解密內(nèi)容:\n{}".format(ciphervalue_dec.decode()))