Python RSA加密解密

it2023-09-06  79

Python RSA加密解密

RSA是一种非对称加密算法

非对称加密需要公钥(publickey)和私钥(privatekey)

消息传递前需要先生成公钥和私钥,发送方将待发送消息用公钥加密,发送给接收方。接收方收到消息后,用私钥解密。在这个过程中,公钥负责加密,私钥负责解密,消息在传输过程中即使被截获,攻击者由于没有私钥,无法破解截获的消息。

非对称加密算法的加解密速度低于对称加密算法,但是安全性更高。

非对称加密算法:RSA、DSA、ECC等算法

RSA加密解密过程:

import rsa # rsa加密 def rsaEncrypt(str): # 生成公钥、私钥 (pubkey, privkey) = rsa.newkeys(512) print("公钥:\n%s\n私钥:\n%s" % (pubkey, privkey)) # 明文编码格式 content = str.encode("utf-8") # 公钥加密 crypto = rsa.encrypt(content, pubkey) return (crypto, privkey) # rsa解密 def rsaDecrypt(str, pk): # 私钥解密 content = rsa.decrypt(str, pk) con = content.decode("utf-8") return con if __name__ == "__main__": str, pk = rsaEncrypt("password") print("加密后密文:\n%s" % str) content = rsaDecrypt(str, pk) print("解密后明文:\n%s" % content)

输出结果:

公钥: PublicKey(9159426407102734998560294890451891757513071757088584253729648528501417390045783844324225057548342352223226493499499812727491257587964648637915558412428127, 65537) 私钥: PrivateKey(9159426407102734998560294890451891757513071757088584253729648528501417390045783844324225057548342352223226493499499812727491257587964648637915558412428127, 65537, 7190772358695741617878842980433042141321773272715637707533822824357636537829981741351623829030718781697003727767252117623450712680213111880577214133630801, 6909139008438239969203597869903711106052468607857293363487996250670397485518005063, 1325697224490082438898074923574950933686195846386160566957092835626425129) 加密后密文: b"V\xc5\x1a\xfd<\xa1\x84\xf7P\x19\xb9\xf8\x14\x8d\xc4\x9dt\x1c\xc8\xe6\xe4T\x8aJ\xe8\\\xc5\xc6\xa4\xd6\xaeH\x80\x1f('Z\xef%\x03\x05Z)\x8fvVX\xe2\xd5\xc9HtRx\xce,\x1el\xf4\xc0\x87\x95\xef;" 解密后明文: password

使用 Crypto.PublicKey.RSA 生成公钥、私钥:

import Crypto.PublicKey.RSA import Crypto.Random x = Crypto.PublicKey.RSA.generate(2048) a = x.exportKey("PEM") # 生成私钥 b = x.publickey().exportKey() # 生成公钥 with open("a.pem", "wb") as x: x.write(a) with open("b.pem", "wb") as x: x.write(b) y = Crypto.PublicKey.RSA.generate(2048, Crypto.Random.new().read) # 使用 Crypto.Random.new().read 伪随机数生成器 c = y.exportKey() # 生成私钥 d = y.publickey().exportKey() #生成公钥 with open("c.pem", "wb") as x: x.write(c) with open("d.pem", "wb") as x: x.write(d)

使用 Crypto.PublicKey.RSA.importKey(private_key) 生成公钥和证书:

import Crypto.PublicKey.RSA with open("a.pem", "rb") as x: xx = Crypto.PublicKey.RSA.importKey(x.read()) b = xx.publickey().exportKey() # 生成公钥 with open("b.pem", "wb") as x: x.write(b) a = xx.exportKey("DER") # 生成 DER 格式的证书 with open("a.der", "wb") as x: x.write(a)

使用 RSA 生成公钥、私钥:

import rsa f, e = rsa.newkeys(1024) # 生成公钥、私钥 e = e.save_pkcs1() # 保存为 .pem 格式 with open("private.pem", "wb") as x: # 保存私钥 x.write(e) f = f.save_pkcs1() # 保存为 .pem 格式 with open("public.pem", "wb") as x: # 保存公钥 x.write(f)

RSA非对称加密:

import rsa import base64 password = 'password' print('password:%s' % password) y = base64.b64encode(password.encode()) print('y:%s' % y) with open("public.pem", "rb") as x: f = x.read() f = rsa.PublicKey.load_pkcs1(f) # load 公钥 with open("private.pem", "rb") as x: e = x.read() e = rsa.PrivateKey.load_pkcs1(e) # load 私钥 cipher_text = rsa.encrypt(y, f) # 使用公钥加密 print('cipher_text:%s' % cipher_text) msg = base64.b64encode(cipher_text).decode() print('msg:%s' % msg) crypto = base64.b64decode(msg) print('crypto:%s' % crypto) text = rsa.decrypt(crypto, e).decode() # 使用私钥解密 print('text:%s' % text) password = base64.b64decode(text).decode() print('password:%s' % password)

输出结果:

password:password y:b'cGFzc3dvcmQ=' cipher_text:b" w\xc3\x80\xb5V\xdb\x95\x1eJ{\x95`\xb1\xd8\xea\xb8G\x9a1\xc6N6\xcd\x9c\r\xfa\xa5;E\xfe\xb7\xe8\xef\x93\xbd\x94\xd4H\xbe\xd5\xcb\xaa\xfd\n\x08\x12\xdd\xc0\x93\xbb\x0e\xc2\xb7S\xd3\xd5\x95\x86+\xbeJ\xd8Yv\xa8\xac\r\xb6y\xd1\xae\xeb:.XH\xa6\xc6\xce\xb5y1\xbe\xc6q\x8c\xd6 \xae\xa9\xd0\xd3$\xb6[\xbab\x99:\xdbz\xb0\x80BkK\xbc%\x19e\x06\xfbP\xa6\xc9\x10\x19\xe3'\x1bd\\\xd1\xd5\xb4Aj" msg:IHfDgLVW25UeSnuVYLHY6rhHmjHGTjbNnA36pTtF/rfo75O9lNRIvtXLqv0KCBLdwJO7DsK3U9PVlYYrvkrYWXaorA22edGu6zouWEimxs61eTG+xnGM1iCuqdDTJLZbumKZOtt6sIBCa0u8JRllBvtQpskQGeMnG2Rc0dW0QWo= crypto:b" w\xc3\x80\xb5V\xdb\x95\x1eJ{\x95`\xb1\xd8\xea\xb8G\x9a1\xc6N6\xcd\x9c\r\xfa\xa5;E\xfe\xb7\xe8\xef\x93\xbd\x94\xd4H\xbe\xd5\xcb\xaa\xfd\n\x08\x12\xdd\xc0\x93\xbb\x0e\xc2\xb7S\xd3\xd5\x95\x86+\xbeJ\xd8Yv\xa8\xac\r\xb6y\xd1\xae\xeb:.XH\xa6\xc6\xce\xb5y1\xbe\xc6q\x8c\xd6 \xae\xa9\xd0\xd3$\xb6[\xbab\x99:\xdbz\xb0\x80BkK\xbc%\x19e\x06\xfbP\xa6\xc9\x10\x19\xe3'\x1bd\\\xd1\xd5\xb4Aj" text:cGFzc3dvcmQ= password:password

参考文章: python3 RSA加解密 Python加密与解密 python 使用 with open() as 读写文件

最新回复(0)