举例1、 #生成RSA公钥和私钥 from Crypto import Random from Crypto.PublicKey import RSA random_generator=Random.new().read rsa=RSA.generate(2048,random_generator) ''' generate(bits, randfunc=None, e=65537) 参数解释: bits: 字节大小,一般为1024的整数倍 randfunc: 随机函数,默认为Crypto.Random.get_random_bytes e=65537: 是公共RSA指数,必须为整数,一般保持默认 ''' #随机值生成私钥 private_key=rsa.exportKey() print(private_key.decode('utf-8')) print('###########################') #随机值生成公钥 public_key=rsa.publickey().exportKey() print(public_key.decode('utf-8')) 举例2、 #使用公钥加密,使用私钥解密 from Crypto import Random from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher import base64 random_generator = Random.new().read rsa = RSA.generate(2048,random_generator) #私钥 private_key = rsa.exportKey() with open("private_a.rsa","wb") as f: f.write(private_key) #公钥 public_key = rsa.publickey().exportKey() with open("public_a.rsa","wb") as f: f.write(public_key) #使用公钥对内容进行加密 message = "需要加密的信息" with open("public_a.rsa","r") as f: key=f.read() pub_key = RSA.importKey(str(key)) cipher = PKCS1_cipher.new(pub_key) rsa_text = base64.b64encode(cipher.encrypt(bytes(message.encode("utf-8)")))) print(rsa_text.decode("utf-8")) #使用私钥对内容进行解密 with open("private_a.rsa","r") as f: key = f.read() pri_Key = RSA.importKey(str(key)) cipher = PKCS1_cipher.new(pri_Key) back_text = cipher.decrypt(base64.b64decode(rsa_text),0) print(back_text.decode("utf-8")) """ 结果: UxPUmbAwFEfvL12pbCljv63Gh883lJKNPPV0ukIY4XWf4U8fIC3PpYOWavkIHKn53NdTQ 19fQ6XwWnUXwV7nrgtAQdv3MjRnOW0NJ2//cf5QC6VoaobyDhNm2ETvOp+JfbS0UQftk xuhc5TTplLQW8U/2ieJXH0pAtuUE9TL9qYY14YmJT3jrz8WNBdbkh2H5tdr8pUB/+AWnP Jjcu5AUDOOmlzl6imnkqmF7ztFlPxy8WCLDvxmSH8Z2i/y+N/8CFlBonHvfsHdXD2HIzCH 1BTRbVasr2oPJ+dUF3mrvw16oY1F2nH/LhIDgrkpW7XJzfHhWDbYZ8Kry2R8/xAFhw== 需要加密的信息 """ 举例3、 #私钥生成签名,公钥验证签名 from Crypto.PublicKey import RSA from Crypto.Hash import SHA from Crypto.Signature import PKCS1_v1_5 as PKCS1_signature import base64 message = "需要加密的信息" #使用私钥生成签名 with open("private_a.rsa","r") as f: key = f.read() pri_Key = RSA.importKey(str(key)) signer = PKCS1_signature.new(pri_Key) digest = SHA.new() digest.update(message.encode("utf-8")) sign = signer.sign(digest) signature = base64.b64encode(sign) print(signature.decode("utf-8")) #使用公钥验证签名 with open("public_a.rsa","r") as f: key=f.read() pub_key = RSA.importKey(str(key)) verifier = PKCS1_signature.new(pub_key) digest = SHA.new() digest.update(message.encode("utf-8")) print(verifier.verify(digest,base64.b64decode(signature))) """ 结果: BZhqDtSVZPFpLkdoG4HOcQxYLp8OdJrniW49P1jcPK14A7kdJjiWOwNpcboK8NgQtwkQ jC4QRXSvdiPS2SecxUV7iy1Qd7zSWFyfEFNmq5n2Z3R/HsvmA3xsyb9QMTJZQbf/agPW ZNpOTH/IWVQNLqU5LtfIZ6VWuQFO031Cm4AkqgHIx9Su0aI8kT8ysao5Z+l7C4PgYZuz YJgJHkUe6gFRDL/UHasPjXHFOP5+VCMauDv1GPrugnpzBoqtVGm7WXwEpI7SuOxQM Zx/o+HeWgodu3DXw6KbWPtq0LEDMPxD0UhvInKSNh09JLzzA4pcYQp6ydaV4z7IHlBrJ vXkSA== True """ 举例4、 #AES加密和解密 from Crypto.Cipher import AES from binascii import b2a_hex,a2b_hex message = "需要加密的信息" key="aes_keysaes_keyseas_keys" mode = AES.MODE_OFB cryptor = AES.new(key.encode("utf-8"),mode,b'0000000000000000') """ 参数解释: key.encode("utf-8"):为加密和解密时使用的秘钥,长度有限制,一般为16,24,32 mode=AES.MODE_OFB : 为AES的不同模式 b'0000000000000000':为表示16进制 """ length = 16 count = len(message) if count % length !=0: add = length - (count % length) else: add = 0 message = message + ('\0' * add) #AES加密 ciphertext = cryptor.encrypt(message.encode("utf-8")) result = b2a_hex(ciphertext) #对加密结果进行16进制处理 print(result.decode("utf-8")) #AES解密 cryptor = AES.new(key.encode("utf-8"),mode,b'0000000000000000') plain_text = cryptor.decrypt(a2b_hex(result)) #对解密结果进行16进制处理 print(plain_text.decode("utf-8").rstrip('\0')) """ 结果: b6cf5298b530c778575c419e0c48c27f354c0601b6931a61b6ed5a794874 需要加密的信息 """
标签:utf,rsa,RSA,学习,key,new,import From: https://www.cnblogs.com/windyrainy/p/16749644.html